11namespace Proto . Remote ;
22
33using System ;
4- using System . Diagnostics ;
5- using System . Linq ;
64using System . Threading ;
75using System . Threading . Tasks ;
86using Grpc . Core ;
@@ -25,6 +23,8 @@ public sealed class ConnectionRunner
2523 private readonly Action _onDisconnected ;
2624 private readonly Action < double > _recordWriteDuration ;
2725 private readonly ILogger _logger ;
26+ private readonly ConnectionWriter _writer ;
27+ private readonly ConnectionReader _reader ;
2828
2929 public ConnectionRunner ( string address , IEndpoint endpoint , ActorSystem system , RemoteConfig remoteConfig ,
3030 IConnectionMode mode , TimeSpan backoff , int maxRetries , Random random , CancellationToken stopToken ,
@@ -43,6 +43,9 @@ public ConnectionRunner(string address, IEndpoint endpoint, ActorSystem system,
4343 _onDisconnected = onDisconnected ;
4444 _recordWriteDuration = recordWriteDuration ;
4545 _logger = logger ;
46+
47+ _writer = new ConnectionWriter ( address , endpoint , system , remoteConfig , recordWriteDuration , logger ) ;
48+ _reader = new ConnectionReader ( address , system , mode , logger ) ;
4649 }
4750
4851 public async Task RunAsync ( )
@@ -96,8 +99,8 @@ public async Task RunAsync()
9699
97100 var combinedToken = CancellationTokenSource . CreateLinkedTokenSource ( _stopToken , cts . Token ) . Token ;
98101
99- var writer = RunWriterAsync ( combinedToken , call , cts ) ;
100- var reader = RunReaderAsync ( combinedToken , call , actorSystemId , cts ) ;
102+ var writer = _writer . RunAsync ( combinedToken , call , cts ) ;
103+ var reader = _reader . RunAsync ( combinedToken , call , actorSystemId , cts ) ;
101104
102105 _logger . Connected ( _system . Address , _address ) ;
103106
@@ -150,92 +153,6 @@ public async Task RunAsync()
150153 }
151154 }
152155
153- private async Task RunWriterAsync ( CancellationToken token , AsyncDuplexStreamingCall < RemoteMessage , RemoteMessage > call , CancellationTokenSource cts )
154- {
155- while ( ! token . IsCancellationRequested )
156- {
157- while ( _endpoint . OutgoingStash . TryPop ( out var messages ) )
158- {
159- var batch = MessageBatchFactory . CreateBatch ( _system , _remoteConfig , messages ) ;
160- try
161- {
162- var sw = Stopwatch . StartNew ( ) ;
163- await call . RequestStream . WriteAsync ( new RemoteMessage { MessageBatch = batch } , token ) . ConfigureAwait ( false ) ;
164- sw . Stop ( ) ;
165- _recordWriteDuration ( sw . Elapsed . TotalSeconds ) ;
166- }
167- catch ( Exception )
168- {
169- _ = _endpoint . OutgoingStash . Append ( messages ) ;
170- cts . Cancel ( ) ;
171- throw ;
172- }
173- }
174-
175- try
176- {
177- await foreach ( var messages in _endpoint . Outgoing . Reader . ReadAllAsync ( token ) . ConfigureAwait ( false ) )
178- {
179- var batch = MessageBatchFactory . CreateBatch ( _system , _remoteConfig , messages ) ;
180- try
181- {
182- var sw = Stopwatch . StartNew ( ) ;
183- await call . RequestStream . WriteAsync ( new RemoteMessage { MessageBatch = batch } , token ) . ConfigureAwait ( false ) ;
184- sw . Stop ( ) ;
185- _recordWriteDuration ( sw . Elapsed . TotalSeconds ) ;
186- }
187- catch ( Exception )
188- {
189- _ = _endpoint . OutgoingStash . Append ( messages ) ;
190- cts . Cancel ( ) ;
191- throw ;
192- }
193- }
194- }
195- catch ( OperationCanceledException )
196- {
197- _logger . WriterCancelled ( _system . Address , _address ) ;
198- }
199- }
200- }
201-
202- private async Task RunReaderAsync ( CancellationToken token , AsyncDuplexStreamingCall < RemoteMessage , RemoteMessage > call , string actorSystemId , CancellationTokenSource cts )
203- {
204- try
205- {
206- while ( await call . ResponseStream . MoveNext ( token ) . ConfigureAwait ( false ) )
207- {
208- var currentMessage = call . ResponseStream . Current ;
209- switch ( currentMessage . MessageTypeCase )
210- {
211- case RemoteMessage . MessageTypeOneofCase . DisconnectRequest :
212- _logger . ReceivedDisconnectionRequest ( _system . Address , _address ) ;
213- var terminated = new EndpointTerminatedEvent ( false , _address , actorSystemId ) ;
214- _system . EventStream . Publish ( terminated ) ;
215- break ;
216- default :
217- _mode . HandleMessage ( currentMessage , _address ) ;
218- break ;
219- }
220- }
221-
222- _logger . ReaderFinished ( _system . Address , _address ) ;
223- }
224- catch ( OperationCanceledException )
225- {
226- _logger . ReaderCancelledDebug ( _system . Address , _address ) ;
227- }
228- catch ( RpcException e ) when ( e . StatusCode == StatusCode . Cancelled )
229- {
230- _logger . ReaderCancelledWarning ( _system . Address , _address ) ;
231- }
232- catch ( Exception e )
233- {
234- _logger . ReaderError ( _system . Address , _address , e . GetType ( ) . Name ) ;
235- cts . Cancel ( ) ;
236- throw ;
237- }
238- }
239156
240157 private bool ShouldStop ( RestartStatistics rs )
241158 {
0 commit comments