77using System . Threading ;
88using System . Threading . Channels ;
99using System . Threading . Tasks ;
10+ using Elastic . Channels . Buffers ;
1011
1112namespace Elastic . Channels ;
1213
@@ -48,10 +49,10 @@ protected BufferedChannelBase(TChannelOptions options)
4849 {
4950 TokenSource = new CancellationTokenSource ( ) ;
5051 Options = options ;
51- var maxConsumers = Math . Max ( 1 , BufferOptions . ConcurrentConsumers ) ;
52+ var maxConsumers = Math . Max ( 1 , BufferOptions . ExportMaxConcurrency ) ;
5253 _throttleTasks = new SemaphoreSlim ( maxConsumers , maxConsumers ) ;
5354 _signal = options . BufferOptions . WaitHandle ;
54- var maxIn = Math . Max ( 1 , BufferOptions . MaxInFlightMessages ) ;
55+ var maxIn = Math . Max ( 1 , BufferOptions . InboundBufferMaxSize ) ;
5556 InChannel = Channel . CreateBounded < TEvent > ( new BoundedChannelOptions ( maxIn )
5657 {
5758 SingleReader = false ,
@@ -63,8 +64,8 @@ protected BufferedChannelBase(TChannelOptions options)
6364 // DropWrite will make `TryWrite` always return true, which is not what we want.
6465 FullMode = BoundedChannelFullMode . Wait
6566 } ) ;
66- // The minimum out buffer the max of (1 or MaxConsumerBufferSize ) as long as it does not exceed MaxInFlightMessages
67- var maxOut = Math . Min ( BufferOptions . MaxInFlightMessages , Math . Max ( 1 , BufferOptions . MaxConsumerBufferSize ) ) ;
67+ // The minimum out buffer the max of (1 or OutboundBufferMaxSize ) as long as it does not exceed InboundBufferMaxSize
68+ var maxOut = Math . Min ( BufferOptions . InboundBufferMaxSize , Math . Max ( 1 , BufferOptions . OutboundBufferMaxSize ) ) ;
6869 OutChannel = Channel . CreateBounded < IOutboundBuffer < TEvent > > (
6970 new BoundedChannelOptions ( maxOut )
7071 {
@@ -78,12 +79,12 @@ protected BufferedChannelBase(TChannelOptions options)
7879 FullMode = BoundedChannelFullMode . Wait
7980 } ) ;
8081
81- InboundBuffer = new InboundBuffer < TEvent > ( maxOut , BufferOptions . MaxConsumerBufferLifetime ) ;
82+ InboundBuffer = new InboundBuffer < TEvent > ( maxOut , BufferOptions . OutboundBufferMaxLifetime ) ;
8283
8384 _outThread = Task . Factory . StartNew ( async ( ) => await ConsumeOutboundEvents ( ) . ConfigureAwait ( false ) ,
8485 TaskCreationOptions . LongRunning | TaskCreationOptions . PreferFairness ) ;
8586 _inThread = Task . Factory . StartNew ( async ( ) =>
86- await ConsumeInboundEvents ( maxOut , BufferOptions . MaxConsumerBufferLifetime )
87+ await ConsumeInboundEvents ( maxOut , BufferOptions . OutboundBufferMaxLifetime )
8788 . ConfigureAwait ( false )
8889 , TaskCreationOptions . LongRunning | TaskCreationOptions . PreferFairness
8990 ) ;
@@ -105,9 +106,12 @@ await ConsumeInboundEvents(maxOut, BufferOptions.MaxConsumerBufferLifetime)
105106
106107 public override bool TryWrite ( TEvent item )
107108 {
108- if ( InChannel . Writer . TryWrite ( item ) ) return true ;
109-
110- Options . PublishRejectionCallback ? . Invoke ( item ) ;
109+ if ( InChannel . Writer . TryWrite ( item ) )
110+ {
111+ Options . PublishToInboundChannelCallback ? . Invoke ( ) ;
112+ return true ;
113+ }
114+ Options . PublishToInboundChannelFailureCallback ? . Invoke ( ) ;
111115 return false ;
112116 }
113117
@@ -117,16 +121,14 @@ public virtual async Task<bool> WaitToWriteAsync(TEvent item, CancellationToken
117121 if ( await InChannel . Writer . WaitToWriteAsync ( ctx ) . ConfigureAwait ( false ) &&
118122 InChannel . Writer . TryWrite ( item ) )
119123 {
120- Options . PublishToInboundChannel ? . Invoke ( ) ;
124+ Options . PublishToInboundChannelCallback ? . Invoke ( ) ;
121125 return true ;
122126 }
123- Options . PublishToInboundChannelFailure ? . Invoke ( ) ;
124-
125- Options . PublishRejectionCallback ? . Invoke ( item ) ;
127+ Options . PublishToInboundChannelFailureCallback ? . Invoke ( ) ;
126128 return false ;
127129 }
128130
129- protected abstract Task < TResponse > Send ( IReadOnlyCollection < TEvent > buffer , CancellationToken ctx = default ) ;
131+ protected abstract Task < TResponse > Export ( IReadOnlyCollection < TEvent > buffer , CancellationToken ctx = default ) ;
130132
131133 private static readonly IReadOnlyCollection < TEvent > DefaultRetryBuffer = new TEvent [ ] { } ;
132134
@@ -138,9 +140,9 @@ IWriteTrackingBuffer statistics
138140
139141 private async Task ConsumeOutboundEvents ( )
140142 {
141- Options . OutboundChannelStarted ? . Invoke ( ) ;
143+ Options . OutboundChannelStartedCallback ? . Invoke ( ) ;
142144
143- var maxConsumers = Options . BufferOptions . ConcurrentConsumers ;
145+ var maxConsumers = Options . BufferOptions . ExportMaxConcurrency ;
144146 var taskList = new List < Task > ( maxConsumers ) ;
145147
146148 while ( await OutChannel . Reader . WaitToReadAsync ( ) . ConfigureAwait ( false ) )
@@ -165,12 +167,12 @@ private async Task ConsumeOutboundEvents()
165167 }
166168 }
167169 await Task . WhenAll ( taskList ) . ConfigureAwait ( false ) ;
168- Options . OutboundChannelExited ? . Invoke ( ) ;
170+ Options . OutboundChannelExitedCallback ? . Invoke ( ) ;
169171 }
170172
171173 private async Task ExportBuffer ( IReadOnlyCollection < TEvent > items , IWriteTrackingBuffer buffer )
172174 {
173- var maxRetries = Options . BufferOptions . MaxRetries ;
175+ var maxRetries = Options . BufferOptions . ExportMaxRetries ;
174176 for ( var i = 0 ; i <= maxRetries && items . Count > 0 ; i ++ )
175177 {
176178 if ( TokenSource . Token . IsCancellationRequested ) break ;
@@ -180,12 +182,12 @@ private async Task ExportBuffer(IReadOnlyCollection<TEvent> items, IWriteTrackin
180182 TResponse ? response ;
181183 try
182184 {
183- response = await Send ( items , TokenSource . Token ) . ConfigureAwait ( false ) ;
185+ response = await Export ( items , TokenSource . Token ) . ConfigureAwait ( false ) ;
184186 Options . ExportResponseCallback ? . Invoke ( response , buffer ) ;
185187 }
186188 catch ( Exception e )
187189 {
188- Options . ExceptionCallback ? . Invoke ( e ) ;
190+ Options . ExportExceptionCallback ? . Invoke ( e ) ;
189191 break ;
190192 }
191193
@@ -195,21 +197,21 @@ private async Task ExportBuffer(IReadOnlyCollection<TEvent> items, IWriteTrackin
195197 var atEndOfRetries = i == maxRetries ;
196198 if ( items . Count > 0 && ! atEndOfRetries )
197199 {
198- await Task . Delay ( Options . BufferOptions . BackoffPeriod ( i ) , TokenSource . Token ) . ConfigureAwait ( false ) ;
200+ await Task . Delay ( Options . BufferOptions . ExportBackoffPeriod ( i ) , TokenSource . Token ) . ConfigureAwait ( false ) ;
199201 Options . ExportRetryCallback ? . Invoke ( items ) ;
200202 }
201203 // otherwise if retryable items still exist and the user wants to be notified notify the user
202204 else if ( items . Count > 0 && atEndOfRetries )
203205 Options . ExportMaxRetriesCallback ? . Invoke ( items ) ;
204206 }
205- Options . BufferOptions . BufferExportedCallback ? . Invoke ( ) ;
207+ Options . BufferOptions . ExportBufferCallback ? . Invoke ( ) ;
206208 if ( _signal is { IsSet : false } )
207209 _signal . Signal ( ) ;
208210 }
209211
210212 private async Task ConsumeInboundEvents ( int maxQueuedMessages , TimeSpan maxInterval )
211213 {
212- Options . InboundChannelStarted ? . Invoke ( ) ;
214+ Options . InboundChannelStartedCallback ? . Invoke ( ) ;
213215 while ( await InboundBuffer . WaitToReadAsync ( InChannel . Reader ) . ConfigureAwait ( false ) )
214216 {
215217 if ( TokenSource . Token . IsCancellationRequested ) break ;
@@ -223,7 +225,7 @@ private async Task ConsumeInboundEvents(int maxQueuedMessages, TimeSpan maxInter
223225 break ;
224226 }
225227
226- Options . PublishToOutboundChannel ? . Invoke ( ) ;
228+ Options . PublishToOutboundChannelCallback ? . Invoke ( ) ;
227229 if ( InboundBuffer . NoThresholdsHit ) continue ;
228230
229231 //:w
@@ -234,24 +236,21 @@ private async Task ConsumeInboundEvents(int maxQueuedMessages, TimeSpan maxInter
234236
235237 if ( await PublishAsync ( outboundBuffer ) . ConfigureAwait ( false ) )
236238 continue ;
237-
238- foreach ( var e in InboundBuffer . Buffer )
239- Options . PublishRejectionCallback ? . Invoke ( e ) ;
239+ Options . PublishToOutboundChannelFailureCallback ? . Invoke ( ) ;
240240 }
241241 }
242242
243243 private ValueTask < bool > PublishAsync ( IOutboundBuffer < TEvent > buffer )
244244 {
245245 async Task < bool > AsyncSlowPath ( IOutboundBuffer < TEvent > b )
246246 {
247- var maxRetries = Options . BufferOptions . MaxRetries ;
247+ var maxRetries = Options . BufferOptions . ExportMaxRetries ;
248248 for ( var i = 0 ; i <= maxRetries ; i ++ )
249249 while ( await OutChannel . Writer . WaitToWriteAsync ( ) . ConfigureAwait ( false ) )
250250 {
251251 if ( OutChannel . Writer . TryWrite ( b ) )
252252 return true ;
253253 }
254- Options . PublishToOutboundChannelFailure ? . Invoke ( ) ;
255254 return false ;
256255 }
257256
0 commit comments