static async Task Main(string[] args) { var cancellation = new CancellationTokenSource(); Stopwatch timer = Stopwatch.StartNew(); try { var options = new BufferedChannelOptions { BufferSize = 1000, FlushInterval = TimeSpan.FromSeconds(1) }; using var collection = new BufferedChannel <Message>(options); collection.RegisterConsumer(Consume); const int producerCount = 11; for (int i = 0; i < producerCount; i++) { #pragma warning disable 4014 Task.Factory.StartNew(() => Produce(collection, cancellation.Token), CancellationToken.None); #pragma warning restore 4014 } await Task.Delay(TimeSpan.FromSeconds(10), CancellationToken.None); Console.WriteLine("Cancelling producer task.."); cancellation.Cancel(); cancellation.Dispose(); } catch (Exception e) { Console.WriteLine(e); Console.ReadLine(); } Console.WriteLine($"Time spent: {timer.Elapsed}"); Console.WriteLine($"Produced: {_producedCount}"); Console.WriteLine($"Consumed: {_consumedCount}"); Console.WriteLine($"Operations: {_consumerOps}"); Console.ReadLine(); }
public static IServiceCollection AddInfluxDbBufferedWriter(this IServiceCollection services, InfluxDBClientOptions clientOptions, BufferedChannelOptions options = null) { if (clientOptions == null) { throw new ArgumentNullException(nameof(clientOptions)); } if (options == null) { options = new BufferedChannelOptions { BufferSize = BufferedChannelOptions.DefaultBufferSize, FlushInterval = BufferedChannelOptions.DefaultFlushInterval }; } return(services .AddTransient <IDefaultMetrics, DefaultMetrics>() .AddSingleton <IInfluxDbClientWriter>(p => { var client = InfluxDBClientFactory.Create(clientOptions); return new InfluxDbClientWriter(client, options); })); }
public static IServiceCollection AddInfluxDbBufferedWriter(this IServiceCollection services, string url, string token, string bucket, string org, BufferedChannelOptions options = null) { var influxDbOptionsBuilder = InfluxDBClientOptions.Builder .CreateNew() .Url(url) .AuthenticateToken(token.ToCharArray()) .Bucket(bucket) .Org(org); var clientOptions = influxDbOptionsBuilder.Build(); return(AddInfluxDbBufferedWriter(services, clientOptions, options)); }
public InfluxDbClientWriter(InfluxDBClient client, BufferedChannelOptions options) { _api = client.GetWriteApi(); _channel = new BufferedChannel <PointData>(options); _channel.RegisterConsumer(SendDataPoints); }