-
Notifications
You must be signed in to change notification settings - Fork 121
Add RecoverInterceptor as alternative to WithRecover #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…erving http.ErrAbortHandler, is more easily composed with other interceptors, and can recover from panics in clients and client interceptors Signed-off-by: Josh Humphries <2035234+jhump@users.noreply.github.com>
//nolint:containedctx // must memoize the stream context to pass to recover handler | ||
ctx context.Context | ||
handle func(context.Context, AnyRequest, any) error | ||
req atomic.Pointer[any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
atomic.Pointer
I think should be just atomic.Value
here as you are using any
anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair. I'm generally allergic to atomic.Value
because it panics if the value is already set and a later attempt tries to CompareAndSwap
with a different concrete type (example). In many cases (particularly for error values), the concrete type can trivially differ, resulting in a panic instead of a failed CAS.
But in this case, there is a generic type parameter for the request type requiring it to always be a particular concrete type, so there should be no such risk. So it should be safe to just use sync.Value
.
There is a slight risk that a caller could call stream.Conn()
, which provides untyped Send(any) error
and Receive(any) error
methods. If they tried to call these with mismatched concrete types, it would panic. But it's possible it would already panic elsewhere due to a failed type conversion to the strongly-typed stream wrapper's concrete request type. I'll try it out and see.
var msg any | ||
if msgPtr := r.req.Load(); msgPtr != nil { | ||
msg = *msgPtr | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var msg any | |
if msgPtr := r.req.Load(); msgPtr != nil { | |
msg = *msgPtr | |
} | |
msg := r.req.Load() |
Passing the |
Oh, good idea. That would also completely eliminate any I can update this PR to include a tentative It might be slightly confusing for the handler to get a value that ostensibly includes response headers and trailers, but that is probably less awkward/confusing than using |
This adds things to the handler signature (the other properties of
AnyRequest
, such asPeer
and HTTP method) and also provides the interceptor separately, so it can be more easily combined with other interceptors. Finally, the value of ignoringhttp.ErrAbortHandler
is unclear and adds a potential footgun in that a handler that panics with that value may inadvertently prevent capturing metrics or other side effects that are desirable for error-tracking in the face of a panic.WithRecover
remains backwards-compatible. But it is deprecated; users should prefer the newRecoverInterceptor
method and use it withWithInterceptors
.The most controversial parts of this change:
AnyRequest
as the way to pass request properties, even for streaming RPCs. Does this seem like a bad idea? This was done to avoid adding a new exported type and to avoid a long parameter list. One awkwardness with this approach is having areq.Any()
method that isn't useful for client and bidi streams and is awkward to populate for server streams.Thoughts?
Resolves #816.