Skip to content

Commit 5e293e2

Browse files
committed
Fixed v2 after merge.
Signed-off-by: bwplotka <[email protected]>
1 parent 7a02d29 commit 5e293e2

File tree

7 files changed

+11
-47
lines changed

7 files changed

+11
-47
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44

55
This repository holds [gRPC Go](https://github.com/grpc/grpc-go) Middlewares: interceptors, helpers and utilities.
66

7-
## ⚠️ Status
8-
9-
Version [v2](https://github.com/grpc-ecosystem/go-grpc-middleware/tree/v2) is about to be released, with migration guide, which will replace v1. Try v2 and give us feedback!
10-
11-
Version v1 is currently in deprecation mode, which means only critical and safety bug fixes will be merged.
12-
13-
147
## Middleware
158

169
[gRPC Go](https://github.com/grpc/grpc-go) has support for "interceptors", i.e. [middleware](https://medium.com/@matryer/writing-middleware-in-golang-and-how-go-makes-it-so-much-fun-4375c1246e81#.gv7tdlghs) that is executed either on the gRPC Server before the request is passed onto the user's application logic, or on the gRPC client either around the user call. It is a perfect way to implement common patterns: auth, logging, tracing, metrics, validation, retries, rate limiting and more, which can be a great generic building blocks that make it easy to build multiple microservices easily.

interceptors/recovery/interceptors.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
2626
o := evaluateOptions(opts)
2727
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (_ any, err error) {
2828
defer func() {
29-
if r := recover(); r != nil || panicked {
29+
if r := recover(); r != nil {
3030
err = recoverFrom(ctx, r, o.recoveryHandlerFunc)
3131
}
3232
}()
3333

34-
resp, err := handler(ctx, req)
35-
panicked = false
36-
return resp, err
34+
return handler(ctx, req)
3735
}
3836
}
3937

@@ -42,14 +40,12 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
4240
o := evaluateOptions(opts)
4341
return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
4442
defer func() {
45-
if r := recover(); r != nil || panicked {
43+
if r := recover(); r != nil {
4644
err = recoverFrom(stream.Context(), r, o.recoveryHandlerFunc)
4745
}
4846
}()
4947

50-
err = handler(srv, stream)
51-
panicked = false
52-
return err
48+
return handler(srv, stream)
5349
}
5450
}
5551

interceptors/recovery/interceptors_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,13 @@ func (s *recoveryAssertService) Ping(ctx context.Context, ping *testpb.PingReque
2828
if ping.Value == "panic" {
2929
panic("very bad thing happened")
3030
}
31-
if ping.Value == "nilpanic" {
32-
panic(nil)
33-
}
3431
return s.TestServiceServer.Ping(ctx, ping)
3532
}
3633

3734
func (s *recoveryAssertService) PingList(ping *testpb.PingListRequest, stream testpb.TestService_PingListServer) error {
3835
if ping.Value == "panic" {
3936
panic("very bad thing happened")
4037
}
41-
if ping.Value == "nilpanic" {
42-
panic(nil)
43-
}
4438
return s.TestServiceServer.PingList(ping, stream)
4539
}
4640

@@ -76,13 +70,6 @@ func (s *RecoverySuite) TestUnary_PanickingRequest() {
7670
assert.Contains(s.T(), status.Convert(err).Message(), "recovery.recoverFrom", "must include stack trace")
7771
}
7872

79-
func (s *RecoverySuite) TestUnary_NilPanickingRequest() {
80-
_, err := s.Client.Ping(s.SimpleCtx(), nilPanicPing)
81-
require.Error(s.T(), err, "there must be an error")
82-
assert.Equal(s.T(), codes.Internal, status.Code(err), "must error with internal")
83-
assert.Equal(s.T(), "<nil>", status.Convert(err).Message(), "must error with <nil>")
84-
}
85-
8673
func (s *RecoverySuite) TestStream_SuccessfulReceive() {
8774
stream, err := s.Client.PingList(s.SimpleCtx(), testpb.GoodPingList)
8875
require.NoError(s.T(), err, "should not fail on establishing the stream")
@@ -101,15 +88,6 @@ func (s *RecoverySuite) TestStream_PanickingReceive() {
10188
assert.Contains(s.T(), status.Convert(err).Message(), "recovery.recoverFrom", "must include stack trace")
10289
}
10390

104-
func (s *RecoverySuite) TestStream_NilPanickingReceive() {
105-
stream, err := s.Client.PingList(s.SimpleCtx(), nilPanicPing)
106-
require.NoError(s.T(), err, "should not fail on establishing the stream")
107-
_, err = stream.Recv()
108-
require.Error(s.T(), err, "there must be an error")
109-
assert.Equal(s.T(), codes.Internal, status.Code(err), "must error with internal")
110-
assert.Equal(s.T(), "<nil>", status.Convert(err).Message(), "must error with <nil>")
111-
}
112-
11391
func TestRecoveryOverrideSuite(t *testing.T) {
11492
opts := []recovery.Option{
11593
recovery.WithRecoveryHandler(func(p any) (err error) {

interceptors/retry/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Other default options are: retry on `ResourceExhausted` and `Unavailable` gRPC c
1818
linear backoff with 10% jitter.
1919
2020
For chained interceptors, the retry interceptor will call every interceptor that follows it
21-
whenever a retry happens.
21+
whenever when a retry happens.
2222
2323
Please see examples for more advanced use.
2424
*/

interceptors/retry/retry.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package retry
55

66
import (
77
"context"
8+
"fmt"
89
"io"
9-
"strconv"
1010
"sync"
1111
"time"
1212

@@ -228,9 +228,7 @@ func (s *serverStreamingRetryingStream) receiveMsgAndIndicateRetry(m any) (bool,
228228
return isRetriable(err, s.callOpts), err
229229
}
230230

231-
func (s *serverStreamingRetryingStream) reestablishStreamAndResendBuffer(
232-
callCtx context.Context,
233-
) (grpc.ClientStream, error) {
231+
func (s *serverStreamingRetryingStream) reestablishStreamAndResendBuffer(callCtx context.Context) (grpc.ClientStream, error) {
234232
s.mu.RLock()
235233
bufferedSends := s.bufferedSends
236234
s.mu.RUnlock()
@@ -306,11 +304,11 @@ func perCallContext(parentCtx context.Context, callOpts *options, attempt uint)
306304
func contextErrToGrpcErr(err error) error {
307305
switch err {
308306
case context.DeadlineExceeded:
309-
return status.Error(codes.DeadlineExceeded, err.Error())
307+
return status.Errorf(codes.DeadlineExceeded, err.Error())
310308
case context.Canceled:
311-
return status.Error(codes.Canceled, err.Error())
309+
return status.Errorf(codes.Canceled, err.Error())
312310
default:
313-
return status.Error(codes.Unknown, err.Error())
311+
return status.Errorf(codes.Unknown, err.Error())
314312
}
315313
}
316314

interceptors/validator/interceptors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,5 @@ func (s *recvWrapper) RecvMsg(m any) error {
7171
if err := validate(s.Context(), m, s.shouldFailFast, s.onValidationErrCallback); err != nil {
7272
return err
7373
}
74-
7574
return nil
7675
}

metadata/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ExtractOutgoing(ctx context.Context) MD {
3939

4040
// Clone performs a *deep* copy of the grpcMetadata.MD.
4141
//
42-
// You can specify the lower-case copiedKeys to only copy certain allow-listed keys. If no keys are explicitly allow-listed
42+
// You can specify the lower-case copiedKeys to only copy certain whitelisted keys. If no keys are explicitly whitelisted
4343
// all keys get copied.
4444
func (m MD) Clone(copiedKeys ...string) MD {
4545
newMd := MD(grpcMetadata.Pairs())

0 commit comments

Comments
 (0)