Skip to content

Commit 70bd3da

Browse files
authored
Spawn new thread for processing echo requests (#396)
Signed-off-by: Mindaugas Baltrimas <[email protected]>
1 parent d27c8e3 commit 70bd3da

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/InformaticsGateway/Services/Http/DestinationAeTitleController.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ public async Task<IActionResult> CEcho(string name)
115115
return NotFound();
116116
}
117117

118-
var request = new ScuWorkRequest(traceId, RequestType.CEcho, destinationApplicationEntity.HostIp, destinationApplicationEntity.Port, destinationApplicationEntity.AeTitle);
118+
var request = new ScuWorkRequest(
119+
traceId,
120+
RequestType.CEcho,
121+
destinationApplicationEntity.HostIp,
122+
destinationApplicationEntity.Port,
123+
destinationApplicationEntity.AeTitle,
124+
HttpContext.RequestAborted
125+
);
119126
var response = await _scuQueue.Queue(request, HttpContext.RequestAborted).ConfigureAwait(false);
120127

121128
if (response.Status != ResponseStatus.Success)

src/InformaticsGateway/Services/Scu/ScuService.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ private async Task BackgroundProcessingAsync(CancellationToken cancellationToken
6464
try
6565
{
6666
var item = _workQueue.Dequeue(cancellationToken);
67-
await Process(item, cancellationToken).ConfigureAwait(false);
67+
68+
var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, item.CancellationToken);
69+
70+
ProcessThread(item, linkedCancellationToken.Token);
6871
}
6972
catch (ObjectDisposedException ex)
7073
{
@@ -82,6 +85,11 @@ private async Task BackgroundProcessingAsync(CancellationToken cancellationToken
8285
_logger.ServiceCancelled(ServiceName);
8386
}
8487

88+
private void ProcessThread(ScuWorkRequest request, CancellationToken cancellationToken)
89+
{
90+
Task.Run(() => Process(request, cancellationToken));
91+
}
92+
8593
private async Task Process(ScuWorkRequest request, CancellationToken cancellationToken)
8694
{
8795
ScuWorkResponse response = null;

src/InformaticsGateway/Services/Scu/ScuWorkRequest.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public class ScuWorkRequest : IDisposable
3333
public string HostIp { get; }
3434
public int Port { get; }
3535
public string AeTitle { get; }
36+
public CancellationToken CancellationToken { get; }
3637

37-
public ScuWorkRequest(string correlationId, RequestType requestType, string hostIp, int port, string aeTitle)
38+
public ScuWorkRequest(string correlationId, RequestType requestType, string hostIp, int port, string aeTitle, CancellationToken cancellationToken)
3839
{
3940
Guard.Against.NullOrWhiteSpace(correlationId);
4041
Guard.Against.NullOrWhiteSpace(hostIp);
@@ -45,6 +46,7 @@ public ScuWorkRequest(string correlationId, RequestType requestType, string host
4546
HostIp = hostIp;
4647
Port = port;
4748
AeTitle = aeTitle;
49+
CancellationToken = cancellationToken;
4850

4951
_awaiter = new AsyncManualResetEvent(false);
5052
}

src/InformaticsGateway/Test/Services/Scu/ScuServiceTest.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async Task GivenAValidDicomEntity_WhenRequestToCEcho_ExpectToReturnSucess
9696

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

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

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

@@ -113,7 +113,7 @@ public async Task GivenACEchoRequest_WhenRejected_ReturnStatusAssociationRejecte
113113

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

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

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

@@ -130,7 +130,7 @@ public async Task GivenACEchoRequest_WhenAborted_ReturnStatusAssociationAborted(
130130

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

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

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

@@ -147,7 +147,7 @@ public async Task GivenACEchoRequest_WhenRemoteServerIsUnreachable_ReturnStatusA
147147

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

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

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

0 commit comments

Comments
 (0)