8000 Ability to cancel the store request in C-Get · Issue #1810 · fo-dicom/fo-dicom · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Ability to cancel the store request in C-Get #1810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
enasbond opened this issue Jun 9, 2024 · 4 comments
Open

Ability to cancel the store request in C-Get #1810

enasbond opened this issue Jun 9, 2024 · 4 comments
Labels

Comments

@enasbond
Copy link
enasbond commented Jun 9, 2024

I implemented the Dicom-Get operation exactly as demonstrated in your SDK. Additionally, my application utilizes the NativeTranscoderManager. When a store request sends a large DICOM file that requires compression and takes about 2 or 3 minutes, my server does not receive the connection request until the compression is complete if the remote host suddenly closes the connection.

I recommend supporting a cancellation token to be sent with the SendRequestAsync method of the DicomService. This would allow the operation to be terminated when the remote host closes the connection, thereby saving server resources.

@enasbond enasbond added the new label Jun 9, 2024
@amoerie amoerie added enhancement and removed new labels Jun 19, 2024
@amoerie
Copy link
Collaborator
amoerie commented Jun 19, 2024

DicomServer and DicomService are indeed lacking in cooperative cancellation support. I think we should do better here.
I might take a stab at this if I find the time.

@amoerie
Copy link
Collaborator
amoerie commented Jun 19, 2024

I took a quick stab at it. It's far from finished though, and this will need to be discussed. See the PR for more details!

@gofal
Copy link
Contributor
gofal commented Apr 16, 2025

You can setup your own cancellation token if you need one.
Your class implementing the c-get is inheriting from DicomService, this means that you always have access to properties that tell you about the state of the underlying connection.

For example there is the property IsConnected. In Order to avoid errors your OnCGetRequestAsync could look like that:

   public async IAsyncEnumerable<DicomCGetResponse> OnCGetRequestAsync(DicomCGetRequest request)
   {
       // some logic to evaluate the request params and to find the matchinf files

       foreach (var matchingFile in matchingFiles)
       {
           // add this line of code, to quit this loop if the connection was closed, to avoid errors in your log when trying to send data
           if (!IsConnected) break;

           var storeRequest = new DicomCStoreRequest(matchingFile);
           await SendRequestAsync(storeRequest);
       }

       yield return new DicomCGetResponse(request, DicomStatus.Success);
   }

@gofal
Copy link
Contributor
gofal commented Apr 16, 2025

But as I understand, you have some long-lasting compression method, that only can be canceled by a cancellationToken?
Whenever the connection is closed (intentionally, or not), the virtual method OnConnectionClosed is injected. So you could add your own cancellation token source, that you use for your compression method, and that you can inject if the OnConnectionClosed is injected.

    private CancellationTokenSource _connectionClosedToken = new CancellationTokenSource();

    public void OnConnectionClosed(Exception exception)
    {
        // whenever the connection is closed, you can trigger the cancellationtokensource
        _connectionClosedToken.Cancel();
        Clean();
    }

    public async IAsyncEnumerable<DicomCGetResponse> OnCGetRequestAsync(DicomCGetRequest request)
    {
         // some logic to evaluate the request params and to find the matchinf files

        foreach (var matchingFile in matchingFiles)
        {
            // here in this long-lasting operation you can pass this cancellationtoken
           //  depending how your method reacs on cancellation token (if it throws an exception, or if it terminates fine but returns null) you have to add more code, to quit the foreach loop
            var compressed = await DoLongLastingCompression(matchingFile, _connectionClosedToken.Token);

            var storeRequest = new DicomCStoreRequest(compressed);
            await SendRequestAsync(storeRequest);
        }

        yield return new DicomCGetResponse(request, DicomStatus.Success);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0