Skip to content

Spawn new thread for processing echo requests (#396) #398

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

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ public async Task<IActionResult> CEcho(string name)
return NotFound();
}

var request = new ScuWorkRequest(traceId, RequestType.CEcho, destinationApplicationEntity.HostIp, destinationApplicationEntity.Port, destinationApplicationEntity.AeTitle);
var request = new ScuWorkRequest(
traceId,
RequestType.CEcho,
destinationApplicationEntity.HostIp,
destinationApplicationEntity.Port,
destinationApplicationEntity.AeTitle,
HttpContext.RequestAborted
);
var response = await _scuQueue.Queue(request, HttpContext.RequestAborted).ConfigureAwait(false);

if (response.Status != ResponseStatus.Success)
Expand Down
10 changes: 9 additions & 1 deletion src/InformaticsGateway/Services/Scu/ScuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ private async Task BackgroundProcessingAsync(CancellationToken cancellationToken
try
{
var item = _workQueue.Dequeue(cancellationToken);
await Process(item, cancellationToken).ConfigureAwait(false);

var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, item.CancellationToken);

ProcessThread(item, linkedCancellationToken.Token);
}
catch (ObjectDisposedException ex)
{
Expand All @@ -82,6 +85,11 @@ private async Task BackgroundProcessingAsync(CancellationToken cancellationToken
_logger.ServiceCancelled(ServiceName);
}

private void ProcessThread(ScuWorkRequest request, CancellationToken cancellationToken)
{
Task.Run(() => Process(request, cancellationToken));
}

private async Task Process(ScuWorkRequest request, CancellationToken cancellationToken)
{
ScuWorkResponse response = null;
Expand Down
4 changes: 3 additions & 1 deletion src/InformaticsGateway/Services/Scu/ScuWorkRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public class ScuWorkRequest : IDisposable
public string HostIp { get; }
public int Port { get; }
public string AeTitle { get; }
public CancellationToken CancellationToken { get; }

public ScuWorkRequest(string correlationId, RequestType requestType, string hostIp, int port, string aeTitle)
public ScuWorkRequest(string correlationId, RequestType requestType, string hostIp, int port, string aeTitle, CancellationToken cancellationToken)
{
Guard.Against.NullOrWhiteSpace(correlationId);
Guard.Against.NullOrWhiteSpace(hostIp);
Expand All @@ -45,6 +46,7 @@ public ScuWorkRequest(string correlationId, RequestType requestType, string host
HostIp = hostIp;
Port = port;
AeTitle = aeTitle;
CancellationToken = cancellationToken;

_awaiter = new AsyncManualResetEvent(false);
}
Expand Down
8 changes: 4 additions & 4 deletions src/InformaticsGateway/Test/Services/Scu/ScuServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task GivenAValidDicomEntity_WhenRequestToCEcho_ExpectToReturnSucess

Assert.Equal(ServiceStatus.Running, svc.Status);

var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "localhost", _port, DicomScpFixture.s_aETITLE);
var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "localhost", _port, DicomScpFixture.s_aETITLE, CancellationToken.None);

var response = await _scuQueue.Queue(request, _cancellationTokenSource.Token);

Expand All @@ -113,7 +113,7 @@ public async Task GivenACEchoRequest_WhenRejected_ReturnStatusAssociationRejecte

Assert.Equal(ServiceStatus.Running, svc.Status);

var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "localhost", _port, "BADAET");
var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "localhost", _port, "BADAET", CancellationToken.None);

var response = await _scuQueue.Queue(request, _cancellationTokenSource.Token);

Expand All @@ -130,7 +130,7 @@ public async Task GivenACEchoRequest_WhenAborted_ReturnStatusAssociationAborted(

Assert.Equal(ServiceStatus.Running, svc.Status);

var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "localhost", _port, "ABORT");
var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "localhost", _port, "ABORT", CancellationToken.None);

var response = await _scuQueue.Queue(request, _cancellationTokenSource.Token);

Expand All @@ -147,7 +147,7 @@ public async Task GivenACEchoRequest_WhenRemoteServerIsUnreachable_ReturnStatusA

Assert.Equal(ServiceStatus.Running, svc.Status);

var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "UNKNOWNHOST123456789", _port, DicomScpFixture.s_aETITLE);
var request = new ScuWorkRequest(Guid.NewGuid().ToString(), RequestType.CEcho, "UNKNOWNHOST123456789", _port, DicomScpFixture.s_aETITLE, CancellationToken.None);

var response = await _scuQueue.Queue(request, _cancellationTokenSource.Token);

Expand Down