Cysharp / YetAnotherHttpHandler

YetAnotherHttpHandler brings the power of HTTP/2 (and gRPC) to Unity and .NET Standard.
MIT License
360 stars 32 forks source link

Soft Restart for YetAnotherHttpHandler.cs #99

Open okankayhan opened 3 hours ago

okankayhan commented 3 hours ago

I create your handler and pass it into several transports on game launch. During soft restarts, which I do for network errors, network mode switches(wifi, cellular) etc. , I would like to be able to kill all previous requests because they will eventually fail in an awkward state.

var serviceHost = $"https://{args.Host}:{args.Port}";
var channel = GrpcChannel.ForAddress(serviceHost, new GrpcChannelOptions
{
    HttpHandler = _httpHandler,
});

_grpcContext = new GrpcContext(channel, _cancellationTokenSource.Token);
_healthCheckServiceTransport = new HealthCheckServiceTransport(_grpcContext);
_authSrv = new AuthServiceTransport(_grpcContext);

Since I still want to reuse the GrpcChannel, it only makes sense to dispose and reinstantiate the handler. However current structure of YetAnotherHttpHandler does not allow that.

To fix this issue I had to clone the repo into my project and added this small tweak to YetAnotherHttpHandler.cs. It was impossible to solve this with extending due to private fields.

protected override void Dispose(bool disposing)
{
    _handler?.Dispose();
    _handler = null;
    // _disposed = true;
  >>_disposed = disposing;
}

Calling Dispose(false) during soft restarts works completely fine now, even though it feels a bit dirty.

I can open a MR for this or if you have a cleaner idea to like maybe adding a void Restart into handler it would be great. I would rather keep this repo as a package.

okankayhan commented 3 hours ago

Pull Request https://github.com/Cysharp/YetAnotherHttpHandler/pull/100