influxdb-csharp
influxdb-csharp copied to clipboard
Socket issues when re-creating LineProtocolClient a lot
This issue arises when re-creating the LineProtocolClient a lot.
ERROR [24-07-2019 15:10:21]: System.Net.Http.HttpRequestException: Address already in use ---> System.Net.Sockets.SocketException: Address already in use at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask
1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask
1 creationTask)
at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task
1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at InfluxDB.LineProtocol.Client.LineProtocolClient.SendAsync(String payload, Precision precision, CancellationToken cancellationToken)
at EnergyHost.Services.Services.InfluxService.Write(String db, String measurement, IReadOnlyDictionary2 data, IReadOnlyDictionary
2 tags, Nullable1 utcTimeStamp) in /app/EnergyHost.Services/Services/InfluxService.cs:line 82 ERROR [24-07-2019 15:10:21]: System.Net.Http.HttpRequestException: Address already in use ---> System.Net.Sockets.SocketException: Address already in use
It appears related to this: https://github.com/dotnet/corefx/issues/37044.
A solution may be this: https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests - although at this time it does not appear to be usable in netstandard2.0 without the addition of dependency injection in to the project.
It's hard to share a single LineProtocolClient across a lot of requests as the constructor takes the database name. The developer could create a little factory to handle this, which I plan to do - but it would be nice if the underlying usage of HttpClient handled this.
This appears to be a common problem when running in Docker containers I should note.
I suspect that this is because the underlying socket is not disposed/closed correctly. See #75.