16
16
using System . Threading . Tasks ;
17
17
using Microsoft . AspNetCore . Connections ;
18
18
using Microsoft . AspNetCore . Http ;
19
+ using Nethermind . Config ;
19
20
using Nethermind . Core . Collections ;
20
21
using Nethermind . Core . Extensions ;
21
22
using Nethermind . Core . Resettables ;
@@ -30,15 +31,18 @@ public class JsonRpcProcessor : IJsonRpcProcessor
30
31
private readonly ILogger _logger ;
31
32
private readonly IJsonRpcService _jsonRpcService ;
32
33
private readonly Recorder _recorder ;
34
+ private readonly IProcessExitSource ? _processExitSource ;
33
35
34
- public JsonRpcProcessor ( IJsonRpcService jsonRpcService , IJsonRpcConfig jsonRpcConfig , IFileSystem fileSystem , ILogManager logManager )
36
+ public JsonRpcProcessor ( IJsonRpcService jsonRpcService , IJsonRpcConfig jsonRpcConfig , IFileSystem fileSystem , ILogManager logManager , IProcessExitSource ? processExitSource = null )
35
37
{
36
38
_logger = logManager ? . GetClassLogger ( ) ?? throw new ArgumentNullException ( nameof ( logManager ) ) ;
37
39
ArgumentNullException . ThrowIfNull ( fileSystem ) ;
38
40
39
41
_jsonRpcService = jsonRpcService ?? throw new ArgumentNullException ( nameof ( jsonRpcService ) ) ;
40
42
_jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException ( nameof ( jsonRpcConfig ) ) ;
41
43
44
+ _processExitSource = processExitSource ;
45
+
42
46
if ( _jsonRpcConfig . RpcRecorderState != RpcRecorderState . None )
43
47
{
44
48
if ( _logger . IsWarn ) _logger . Warn ( "Enabling JSON RPC diagnostics recorder - this will affect performance and should be only used in a diagnostics mode." ) ;
@@ -47,6 +51,9 @@ public JsonRpcProcessor(IJsonRpcService jsonRpcService, IJsonRpcConfig jsonRpcCo
47
51
}
48
52
}
49
53
54
+ public CancellationToken ProcessExit
55
+ => _processExitSource ? . Token ?? default ;
56
+
50
57
private ( JsonRpcRequest ? Model , ArrayPoolList < JsonRpcRequest > ? Collection ) DeserializeObjectOrArray ( JsonDocument doc )
51
58
{
52
59
JsonValueKind type = doc . RootElement . ValueKind ;
@@ -120,6 +127,12 @@ private ArrayPoolList<JsonRpcRequest> DeserializeArray(JsonElement element) =>
120
127
121
128
public async IAsyncEnumerable < JsonRpcResult > ProcessAsync ( PipeReader reader , JsonRpcContext context )
122
129
{
130
+ if ( ProcessExit . IsCancellationRequested )
131
+ {
132
+ JsonRpcErrorResponse response = _jsonRpcService . GetErrorResponse ( ErrorCodes . ResourceUnavailable , "Shutting down" ) ;
133
+ yield return JsonRpcResult . Single ( RecordResponse ( response , new RpcReport ( "Shutdown" , 0 , false ) ) ) ;
134
+ }
135
+
123
136
reader = await RecordRequest ( reader ) ;
124
137
long startTime = Stopwatch . GetTimestamp ( ) ;
125
138
CancellationTokenSource timeoutSource = new ( _jsonRpcConfig . Timeout ) ;
0 commit comments