From ecfff06c5c1be4dbc5e6f22d40a8d1cb69ef0e95 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Fri, 14 Feb 2025 20:28:29 -0800 Subject: [PATCH] Fix lint errors --- examples/go.mod | 10 ++++------ examples/go.sum | 12 ++++++------ interceptors/auth/auth_test.go | 6 +++--- interceptors/auth/examples_test.go | 4 ++-- interceptors/client_test.go | 7 +------ interceptors/logging/examples/go.mod | 10 ++++------ interceptors/logging/examples/go.sum | 12 ++++++------ interceptors/protovalidate/protovalidate_test.go | 2 +- interceptors/ratelimit/examples_test.go | 6 ++---- interceptors/retry/examples_test.go | 6 +++--- interceptors/selector/selector_example_test.go | 4 ++-- interceptors/server_test.go | 5 +---- interceptors/timeout/examples_test.go | 2 +- metadata/metadata_test.go | 6 +++--- testing/testpb/interceptor_suite.go | 9 +++------ testing/testpb/pingservice_test.go | 8 +------- wrappers_test.go | 10 +++++----- 17 files changed, 48 insertions(+), 71 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index bed8df42e..314e716ae 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -1,8 +1,6 @@ module github.com/grpc-ecosystem/go-grpc-middleware/examples/v2 -go 1.22 - -toolchain go1.23.1 +go 1.23 require ( github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0 @@ -33,9 +31,9 @@ require ( github.com/prometheus/procfs v0.15.1 // indirect github.com/stretchr/testify v1.10.0 // indirect go.opentelemetry.io/otel/metric v1.31.0 // indirect - golang.org/x/net v0.30.0 // indirect - golang.org/x/sys v0.26.0 // indirect - golang.org/x/text v0.21.0 // indirect + golang.org/x/net v0.35.0 // indirect + golang.org/x/sys v0.30.0 // indirect + golang.org/x/text v0.22.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect google.golang.org/protobuf v1.36.4 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/examples/go.sum b/examples/go.sum index c0ac3702c..3c7581939 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -54,14 +54,14 @@ go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0= go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys= go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A= -golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= -golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= +golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= +golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA= golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= -golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= -golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 h1:QCqS/PdaHTSWGvupk2F/ehwHtGc0/GYkT+3GAcR1CCc= google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI= google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= diff --git a/interceptors/auth/auth_test.go b/interceptors/auth/auth_test.go index d30cc8a57..375922076 100644 --- a/interceptors/auth/auth_test.go +++ b/interceptors/auth/auth_test.go @@ -23,7 +23,7 @@ import ( "google.golang.org/grpc/status" ) -var authedMarker struct{} +type authedMarker struct{} var ( commonAuthToken = "some_good_token" @@ -41,12 +41,12 @@ func buildDummyAuthFunction(expectedScheme string, expectedToken string) func(ct if token != expectedToken { return nil, status.Error(codes.PermissionDenied, "buildDummyAuthFunction bad token") } - return context.WithValue(ctx, authedMarker, "marker_exists"), nil + return context.WithValue(ctx, authedMarker{}, "marker_exists"), nil } } func assertAuthMarkerExists(t *testing.T, ctx context.Context) { - assert.Equal(t, "marker_exists", ctx.Value(authedMarker).(string), "auth marker from buildDummyAuthFunction must be passed around") + assert.Equal(t, "marker_exists", ctx.Value(authedMarker{}).(string), "auth marker from buildDummyAuthFunction must be passed around") } type assertingPingService struct { diff --git a/interceptors/auth/examples_test.go b/interceptors/auth/examples_test.go index 8e41fd301..5ac50ba14 100644 --- a/interceptors/auth/examples_test.go +++ b/interceptors/auth/examples_test.go @@ -15,7 +15,7 @@ import ( "google.golang.org/grpc/status" ) -var tokenInfoKey struct{} +type tokenInfoKey struct{} func parseToken(token string) (struct{}, error) { return struct{}{}, nil @@ -40,7 +40,7 @@ func exampleAuthFunc(ctx context.Context) (context.Context, error) { ctx = logging.InjectFields(ctx, logging.Fields{"auth.sub", userClaimFromToken(tokenInfo)}) // WARNING: In production define your own type to avoid context collisions. - return context.WithValue(ctx, tokenInfoKey, tokenInfo), nil + return context.WithValue(ctx, tokenInfoKey{}, tokenInfo), nil } // Simple example of server initialization code. diff --git a/interceptors/client_test.go b/interceptors/client_test.go index 252cb0bb5..e41f57367 100644 --- a/interceptors/client_test.go +++ b/interceptors/client_test.go @@ -159,15 +159,10 @@ func (s *ClientInterceptorTestSuite) SetupSuite() { s.stopped <- s.server.Serve(s.serverListener) }() - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - // This is the point where we hook up the interceptor. - s.clientConn, err = grpc.DialContext( - ctx, + s.clientConn, err = grpc.NewClient( s.serverListener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()), - grpc.WithBlock(), grpc.WithUnaryInterceptor(UnaryClientInterceptor(s.mock)), grpc.WithStreamInterceptor(StreamClientInterceptor(s.mock)), ) diff --git a/interceptors/logging/examples/go.mod b/interceptors/logging/examples/go.mod index c54874d64..92e2af4d1 100644 --- a/interceptors/logging/examples/go.mod +++ b/interceptors/logging/examples/go.mod @@ -1,8 +1,6 @@ module github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples -go 1.21.1 - -toolchain go1.21.13 +go 1.23 require ( github.com/go-kit/log v0.2.1 @@ -21,9 +19,9 @@ require ( github.com/mattn/go-isatty v0.0.14 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect - golang.org/x/net v0.28.0 // indirect - golang.org/x/sys v0.24.0 // indirect - golang.org/x/text v0.21.0 // indirect + golang.org/x/net v0.35.0 // indirect + golang.org/x/sys v0.30.0 // indirect + golang.org/x/text v0.22.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 // indirect google.golang.org/protobuf v1.36.4 // indirect ) diff --git a/interceptors/logging/examples/go.sum b/interceptors/logging/examples/go.sum index ca21fa000..9c2824c42 100644 --- a/interceptors/logging/examples/go.sum +++ b/interceptors/logging/examples/go.sum @@ -40,15 +40,15 @@ go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= -golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= -golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= +golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 h1:2035KHhUv+EpyB+hWgJnaWKJOdX1E95w2S8Rr4uWKTs= google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= diff --git a/interceptors/protovalidate/protovalidate_test.go b/interceptors/protovalidate/protovalidate_test.go index 85fee10ca..ea1e0e993 100644 --- a/interceptors/protovalidate/protovalidate_test.go +++ b/interceptors/protovalidate/protovalidate_test.go @@ -135,7 +135,7 @@ func startGrpcServer(t *testing.T, called *bool, ignoreMessages ...protoreflect. return lis.Dial() } - conn, err := grpc.DialContext(context.Background(), + conn, err := grpc.NewClient( "bufnet", grpc.WithContextDialer(dialer), grpc.WithTransportCredentials(insecure.NewCredentials()), diff --git a/interceptors/ratelimit/examples_test.go b/interceptors/ratelimit/examples_test.go index 8689600d9..73fa96272 100644 --- a/interceptors/ratelimit/examples_test.go +++ b/interceptors/ratelimit/examples_test.go @@ -59,8 +59,7 @@ func ExampleUnaryClientInterceptor() { // Create stream rateLimiter, based on token bucket here. // You can implement your own rate-limiter for the interface. limiter := &alwaysPassLimiter{} - _, _ = grpc.DialContext( - context.Background(), + _, _ = grpc.NewClient( ":8080", grpc.WithUnaryInterceptor( ratelimit.UnaryClientInterceptor(limiter), @@ -73,8 +72,7 @@ func ExampleStreamClientInterceptor() { // Create stream rateLimiter, based on token bucket here. // You can implement your own rate-limiter for the interface. limiter := &alwaysPassLimiter{} - _, _ = grpc.DialContext( - context.Background(), + _, _ = grpc.NewClient( ":8080", grpc.WithChainStreamInterceptor( ratelimit.StreamClientInterceptor(limiter), diff --git a/interceptors/retry/examples_test.go b/interceptors/retry/examples_test.go index e65593caf..4b34dd299 100644 --- a/interceptors/retry/examples_test.go +++ b/interceptors/retry/examples_test.go @@ -17,7 +17,7 @@ var cc *grpc.ClientConn // Simple example of using the default interceptor configuration. func Example_initialization() { - _, _ = grpc.Dial("myservice.example.com", + _, _ = grpc.NewClient("myservice.example.com", grpc.WithStreamInterceptor(StreamClientInterceptor()), grpc.WithUnaryInterceptor(UnaryClientInterceptor()), ) @@ -29,7 +29,7 @@ func Example_initializationWithOptions() { WithBackoff(BackoffLinear(100 * time.Millisecond)), WithCodes(codes.NotFound, codes.Aborted), } - _, _ = grpc.Dial("myservice.example.com", + _, _ = grpc.NewClient("myservice.example.com", grpc.WithStreamInterceptor(StreamClientInterceptor(opts...)), grpc.WithUnaryInterceptor(UnaryClientInterceptor(opts...)), ) @@ -42,7 +42,7 @@ func Example_initializationWithExponentialBackoff() { opts := []CallOption{ WithBackoff(BackoffExponential(100 * time.Millisecond)), } - _, _ = grpc.Dial("myservice.example.com", + _, _ = grpc.NewClient("myservice.example.com", grpc.WithStreamInterceptor(StreamClientInterceptor(opts...)), grpc.WithUnaryInterceptor(UnaryClientInterceptor(opts...)), ) diff --git a/interceptors/selector/selector_example_test.go b/interceptors/selector/selector_example_test.go index e115dd2c1..7349ddb43 100644 --- a/interceptors/selector/selector_example_test.go +++ b/interceptors/selector/selector_example_test.go @@ -40,7 +40,7 @@ func Example_ratelimit() { ) } -var tokenInfoKey struct{} +type tokenInfoKey struct{} func parseToken(token string) (struct{}, error) { return struct{}{}, nil @@ -65,7 +65,7 @@ func exampleAuthFunc(ctx context.Context) (context.Context, error) { ctx = logging.InjectFields(ctx, logging.Fields{"auth.sub", userClaimFromToken(tokenInfo)}) // WARNING: In production define your own type to avoid context collisions. - return context.WithValue(ctx, tokenInfoKey, tokenInfo), nil + return context.WithValue(ctx, tokenInfoKey{}, tokenInfo), nil } func loginSkip(_ context.Context, c interceptors.CallMeta) bool { diff --git a/interceptors/server_test.go b/interceptors/server_test.go index b749ab4e0..87d46263d 100644 --- a/interceptors/server_test.go +++ b/interceptors/server_test.go @@ -56,10 +56,7 @@ func (s *ServerInterceptorTestSuite) SetupSuite() { _ = s.server.Serve(s.serverListener) }() - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - - s.clientConn, err = grpc.DialContext(ctx, s.serverListener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) + s.clientConn, err = grpc.NewClient(s.serverListener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials())) require.NoError(s.T(), err, "must not error on client Dial") s.testClient = testpb.NewTestServiceClient(s.clientConn) } diff --git a/interceptors/timeout/examples_test.go b/interceptors/timeout/examples_test.go index 72796fe01..e7110e795 100644 --- a/interceptors/timeout/examples_test.go +++ b/interceptors/timeout/examples_test.go @@ -15,7 +15,7 @@ import ( // Initialization shows an initialization sequence with a custom client request timeout. func Example_initialization() { - clientConn, err := grpc.Dial( + clientConn, err := grpc.NewClient( "ServerAddr", grpc.WithUnaryInterceptor( // Set your client request timeout. diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 17fd13598..30811ad4a 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -12,15 +12,15 @@ import ( grpcMetadata "google.golang.org/grpc/metadata" ) -var parentKey struct{} +type parentKey struct{} var ( testPairs = []string{"singlekey", "uno", "multikey", "one", "multikey", "two", "multikey", "three"} - parentCtx = context.WithValue(context.TODO(), parentKey, "parentValue") + parentCtx = context.WithValue(context.TODO(), parentKey{}, "parentValue") ) func assertRetainsParentContext(t *testing.T, ctx context.Context) { - x := ctx.Value(parentKey) + x := ctx.Value(parentKey{}) assert.EqualValues(t, "parentValue", x, "context must contain parentCtx") } diff --git a/testing/testpb/interceptor_suite.go b/testing/testpb/interceptor_suite.go index 91f81f733..311b92b10 100644 --- a/testing/testpb/interceptor_suite.go +++ b/testing/testpb/interceptor_suite.go @@ -116,7 +116,6 @@ func (s *InterceptorTestSuite) RestartServer(delayedStart time.Duration) <-chan } func (s *InterceptorTestSuite) NewClient(dialOpts ...grpc.DialOption) TestServiceClient { - newDialOpts := append(dialOpts, grpc.WithBlock()) var err error if *flagTls { cp := x509.NewCertPool() @@ -124,13 +123,11 @@ func (s *InterceptorTestSuite) NewClient(dialOpts ...grpc.DialOption) TestServic s.T().Fatal("failed to append certificate") } creds := credentials.NewTLS(&tls.Config{ServerName: "localhost", RootCAs: cp}) - newDialOpts = append(newDialOpts, grpc.WithTransportCredentials(creds)) + dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds)) } else { - newDialOpts = append(newDialOpts, grpc.WithTransportCredentials(insecure.NewCredentials())) + dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials())) } - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - s.clientConn, err = grpc.DialContext(ctx, s.ServerAddr(), newDialOpts...) + s.clientConn, err = grpc.NewClient(s.ServerAddr(), dialOpts...) require.NoError(s.T(), err, "must not error on client Dial") return NewTestServiceClient(s.clientConn) } diff --git a/testing/testpb/pingservice_test.go b/testing/testpb/pingservice_test.go index 784d7e340..547999cda 100644 --- a/testing/testpb/pingservice_test.go +++ b/testing/testpb/pingservice_test.go @@ -8,7 +8,6 @@ import ( "fmt" "net" "testing" - "time" "github.com/stretchr/testify/require" "google.golang.org/grpc" @@ -34,15 +33,10 @@ func TestPingServiceOnWire(t *testing.T) { <-stopped }() - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - // This is the point where we hook up the interceptor. - clientConn, err := grpc.DialContext( - ctx, + clientConn, err := grpc.NewClient( serverListener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()), - grpc.WithBlock(), ) require.NoError(t, err, "must not error on client Dial") diff --git a/wrappers_test.go b/wrappers_test.go index 9ec520c2d..c686f352d 100644 --- a/wrappers_test.go +++ b/wrappers_test.go @@ -13,18 +13,18 @@ import ( "google.golang.org/grpc/status" ) -var ( +type ( someKey struct{} other struct{} ) func TestWrapServerStream(t *testing.T) { - ctx := context.WithValue(context.TODO(), someKey, 1) + ctx := context.WithValue(context.TODO(), someKey{}, 1) fake := &fakeServerStream{ctx: ctx} wrapped := WrapServerStream(fake) - assert.NotNil(t, wrapped.Context().Value(someKey), "values from fake must propagate to wrapper") - wrapped.WrappedContext = context.WithValue(wrapped.Context(), other, 2) - assert.NotNil(t, wrapped.Context().Value(other), "values from wrapper must be set") + assert.NotNil(t, wrapped.Context().Value(someKey{}), "values from fake must propagate to wrapper") + wrapped.WrappedContext = context.WithValue(wrapped.Context(), other{}, 2) + assert.NotNil(t, wrapped.Context().Value(other{}), "values from wrapper must be set") } type fakeServerStream struct {