Comments (14)
The goal of my libraries is to be as easy to use as possible, but deeply customizable if required.
As I understand it, I need to add a way to tell Polly about bad events by adding new exceptions or custom events. And also be able to turn off the default behavior.
from h.pipes.
var retryPolicy = Policy
.Handle<InvalidOperationException>()
.RetryAsync(3, async (exception, count) =>
{
await client.ConnectAsync();
}
await retryPolicy.ExecuteAsync(async () =>
await client.WriteAsync(message));
For now, this code will work because WriteAsync
returns InvalidOperationException
if client.AutoReconnect == false
and the connection to the server is not established or lost.
from h.pipes.
Or do you think it's still worth throwing a special
NotConnectedException
?
For a meaningful response, I do believe that it would be better to have NotConnectedException
, ConnectionFailedException
, or whatever you like. It's better by principle. But for practical reasons, I need to experiment on it. I am not as familiar as you to estimate the results. 😊 I will comment after some benchmarks.
But still, thank you for your work for solving such a niche problem that Windows developers are struggling with for years and in a simplified manner. And also, thank you for the quick response.
from h.pipes.
My bad. The default behavior is to retry infinitely, right? Then, for instance, when the named pipe client tries to connect and it fails, it throws an InvalidOperationException
. In case of high latency, there's no timeout. So, we have to implement this with Polly. Is that the case? It's my misunderstanding then.
So, except for the exception name suggestion, I guess everything is OK from my side. And there's a solid Polly example for future requirements.
Thank you.
from h.pipes.
Hello.
I have studied what Polly has to offer, but unfortunately I have never used it.
At the moment, this code is used:
ReconnectionInterval = reconnectionInterval ?? TimeSpan.FromMilliseconds(100);
ReconnectionTimer = new System.Timers.Timer(ReconnectionInterval.TotalMilliseconds);
ReconnectionTimer.Elapsed += async (_, _) =>
{
try
{
if (!IsConnected && !IsConnecting)
{
using var cancellationTokenSource = new CancellationTokenSource(ReconnectionInterval);
try
{
await ConnectAsync(cancellationTokenSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
}
}
catch (Exception exception)
{
ReconnectionTimer.Stop();
OnExceptionOccurred(exception);
}
};
the timer is started after a call to ConnectAsync
or WriteAsync
if AutoReconnect == true
(default)
from h.pipes.
catch (TaskCanceledException)
Please study to avoid future mistakes: https://stackoverflow.com/a/34359530/4792221
from h.pipes.
Polly looks like a way to make the built-in methods much more flexible and appropriate for a particular situation. As I understand it, this can work with any client, not just HttpClient
?
from h.pipes.
catch (TaskCanceledException)
Please study to avoid future mistakes: https://stackoverflow.com/a/34359530/4792221
Well, I managed to find this one out by experiencing it, unfortunately. Thank you.
And yeah, Polly can be considered a wrapper around mostly async operations (a huge oversimplification but it has a point) and adding a few exceptions would most likely be enough to make these work together.
Edit: grammar
from h.pipes.
Or do you think it's still worth throwing a special NotConnectedException
?
from h.pipes.
Also, adding the reconnectionInterval
parameter in the sample would be better to explain the Polly sample. I saw them and they are crystal clear.
Would you consider initiating AutoReconnect
in the constructor?
from h.pipes.
Would you consider initiating
AutoReconnect
in the constructor?
Not sure, because I consider this an optional parameter, which should be passed through:
new PipeClient<T>()
{
AutoReconnect = false,
}
because there are already many parameters in the constructor. Perhaps you have some example where autoReconnect in the parameter list is needed?
from h.pipes.
Also, adding the
reconnectionInterval
parameter in the sample would be better to explain the Polly sample. I saw them and they are crystal clear.
Did not understand this moment, can you explain in more detail?
from h.pipes.
Would you consider initiating
AutoReconnect
in the constructor?Not sure, because I consider this an optional parameter, which should be passed through:
new PipeClient<T>() { AutoReconnect = false, }because there are already many parameters in the constructor. Perhaps you have some example where autoReconnect in the parameter list is needed?
Reasonable.
Also, adding the
reconnectionInterval
parameter in the sample would be better to explain the Polly sample. I saw them and they are crystal clear.Did not understand this moment, can you explain in more detail?
I meant something like this.
// default reconnectionInterval is 100 ms
var timeout = TimeSpan.FromMilliseconds(1000);
await using var client = new PipeClient<MyMessage>(pipeName, reconnectionInterval: timeout);
// Set explicitly to trigger an exception
AutoReconnect = false;
var retryPolicy = Policy
.Handle<InvalidOperationException>()
.RetryAsync(3, async (exception, count) =>
{
await client.ConnectAsync();
}
await retryPolicy.ExecuteAsync(async () =>
await client.WriteAsync(message));
But in polly docs, I saw a combination of retry and timeout without the requirement of wrapped function timeout capability. But without an exception, we need to have a response message just like HTTP response so that we can check the success result.
var retryPolicy =
Policy.HandleResult<MyMessage>(m => !m.IsSuccessful) // Without the exception, we can handle the response if exists
.Or<TimeoutRejectedException>() // This exception is thrown by the timeoutPolicy
.RetryAsync(3);
var timeoutPolicy = Policy.TimeoutAsync(1);
var message = await
retryPolicy.ExecuteAsync(() =>
timeoutPolicy.ExecuteAsync(async token =>
await client.WriteAsync(new MyMessage() { Text = "Some message" } , token), CancellationToken.None));
from h.pipes.
reconnectionInterval
will not work if AutoReconnect == false
. Why specify it?
from h.pipes.
Related Issues (20)
- I can't transmit a simple message [question] HOT 9
- some windows 10 and windows 11 clients can't connect to the PipeServer if hosted in a service HOT 17
- Dependencies Inferno on will fail if not on windows and on supported .net versions HOT 15
- Interesting Bug...version 2.0.34 HOT 6
- ReadAsync length HOT 4
- Generic message type <T> HOT 4
- Returned by server pipeName is null HOT 2
- Large messages on .NET 6 fail to send HOT 9
- PipeServer crashing with many connections, IOException: 'Pipe is broken' HOT 13
- SemaphoreFullException when CancellationToken is cancelled before semaphore is entered
- Feature request: Have a connection identifier on the server based on a client connection HOT 3
- Application getting terminated HOT 5
- The server stops working HOT 8
- Feature request: Support CreatePipeStreamFunc on the client as well
- Handles are not disposed HOT 1
- Implementation questions HOT 1
- Different Send and Receive Types Info Request HOT 1
- Compiler Errors: Tests, 4.8
- Pipe name handling on client side HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from h.pipes.