diff --git a/.travis.yml b/.travis.yml index 9af36c955..b5fb3a01c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ install: - dep ensure script: - - ./test_all.sh + - make test after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/auth/DOC.md b/auth/DOC.md index e1d6576bf..e31288aa8 100644 --- a/auth/DOC.md +++ b/auth/DOC.md @@ -4,7 +4,6 @@ * [Overview](#pkg-overview) * [Imported Packages](#pkg-imports) * [Index](#pkg-index) -* [Examples](#pkg-examples) ## Overview `grpc_auth` a generic server-side auth middleware for gRPC. @@ -21,42 +20,6 @@ It also allows for per-service implementation overrides of `AuthFunc`. See `Serv Please see examples for simple examples of use. -#### Example: - -
-Click to expand code. - -```go -token, err := grpc_auth.AuthFromMD(ctx, "bearer") -if err != nil { - return nil, err -} -tokenInfo, err := parseToken(token) -if err != nil { - return nil, grpc.Errorf(codes.Unauthenticated, "invalid auth token: %v", err) -} -grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo)) -newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo) -return newCtx, nil -``` - -
- -#### Example: - -
-Click to expand code. - -```go -server := grpc.NewServer( - grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(Example_authfunc)), - grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(Example_authfunc)), -) -return server -``` - -
- ## Imported Packages - [github.com/grpc-ecosystem/go-grpc-middleware](./..) @@ -72,10 +35,6 @@ return server * [type AuthFunc](#AuthFunc) * [type ServiceAuthFuncOverride](#ServiceAuthFuncOverride) -#### Examples -* [Package (Authfunc)](#example__authfunc) -* [Package (Serverconfig)](#example__serverconfig) - #### Package files [auth.go](./auth.go) [doc.go](./doc.go) [metadata.go](./metadata.go) diff --git a/auth/examples_test.go b/auth/examples_test.go index 20cd21971..12299da55 100644 --- a/auth/examples_test.go +++ b/auth/examples_test.go @@ -1,6 +1,3 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - package grpc_auth_test import ( @@ -11,7 +8,9 @@ import ( "google.golang.org/grpc/codes" ) -var cc *grpc.ClientConn +var ( + cc *grpc.ClientConn +) func parseToken(token string) (struct{}, error) { return struct{}{}, nil @@ -21,27 +20,24 @@ func userClaimFromToken(struct{}) string { return "foobar" } -// Simple example of an `AuthFunc` that extracts, verifies the token and sets it in the handler -// contexts. -func Example_authfunc(ctx context.Context) (context.Context, error) { - token, err := grpc_auth.AuthFromMD(ctx, "bearer") - if err != nil { - return nil, err - } - tokenInfo, err := parseToken(token) - if err != nil { - return nil, grpc.Errorf(codes.Unauthenticated, "invalid auth token: %v", err) +// Simple example of server initialization code. +func Example_ServerConfig() { + exampleAuthFunc := func(ctx context.Context) (context.Context, error) { + token, err := grpc_auth.AuthFromMD(ctx, "bearer") + if err != nil { + return nil, err + } + tokenInfo, err := parseToken(token) + if err != nil { + return nil, grpc.Errorf(codes.Unauthenticated, "invalid auth token: %v", err) + } + grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo)) + newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo) + return newCtx, nil } - grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo)) - newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo) - return newCtx, nil -} -// Simple example of server initialization code. -func Example_serverconfig() *grpc.Server { - server := grpc.NewServer( - grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(Example_authfunc)), - grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(Example_authfunc)), + _ = grpc.NewServer( + grpc.StreamInterceptor(grpc_auth.StreamServerInterceptor(exampleAuthFunc)), + grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(exampleAuthFunc)), ) - return server } diff --git a/checkup.sh b/checkup.sh deleted file mode 100755 index 9116e9e5c..000000000 --- a/checkup.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash -# Script that checks up code (govet). - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" - -function print_real_go_files { - grep --files-without-match 'DO NOT EDIT!' $(find . -iname '*.go') --exclude=./vendor/* -} - -function govet_all { - ret=0 - for i in $(print_real_go_files); do - output=$(go tool vet -all=true -tests=false ${i}) - ret=$(($ret | $?)) - echo -n ${output} - done; - return ${ret} -} - -govet_all -echo "returning $?" diff --git a/logging/logrus/DOC.md b/logging/logrus/DOC.md index dc99563ed..adbc37a13 100644 --- a/logging/logrus/DOC.md +++ b/logging/logrus/DOC.md @@ -4,7 +4,6 @@ * [Overview](#pkg-overview) * [Imported Packages](#pkg-imports) * [Index](#pkg-index) -* [Examples](#pkg-examples) ## Overview `grpc_logrus` is a gRPC logging middleware backed by Logrus loggers @@ -27,85 +26,6 @@ Logrus can also be made as a backend for gRPC library internals. For that use `R Please see examples and tests for examples of use. -#### Example: - -
-Click to expand code. - -```go -x := func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { - // Add fields the ctxtags of the request which will be added to all extracted loggers. - grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) - // Extract a single request-scoped logrus.Logger and log messages. - l := grpc_logrus.Extract(ctx) - l.Info("some ping") - l.Info("another ping") - return &pb_testproto.PingResponse{Value: ping.Value}, nil -} -return x -``` - -
- -#### Example: - -
-Click to expand code. - -```go -// Logrus entry is used, allowing pre-definition of certain fields by the user. -logrusEntry := logrus.NewEntry(logrusLogger) -// Shared options for the logger, with a custom gRPC code to log level function. -opts := []grpc_logrus.Option{ - grpc_logrus.WithLevels(customFunc), -} -// Make sure that log statements internal to gRPC library are logged using the zapLogger as well. -grpc_logrus.ReplaceGrpcLogger(logrusEntry) -// Create a server, make sure we put the grpc_ctxtags context before everything else. -server := grpc.NewServer( - grpc_middleware.WithUnaryServerChain( - grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), - grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...), - ), - grpc_middleware.WithStreamServerChain( - grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), - grpc_logrus.StreamServerInterceptor(logrusEntry, opts...), - ), -) -return server -``` - -
- -#### Example: - -
-Click to expand code. - -```go -// Logrus entry is used, allowing pre-definition of certain fields by the user. -logrusEntry := logrus.NewEntry(logrusLogger) -// Shared options for the logger, with a custom duration to log field function. -opts := []grpc_logrus.Option{ - grpc_logrus.WithDurationField(func(duration time.Duration) (key string, value interface{}) { - return "grpc.time_ns", duration.Nanoseconds() - }), -} -server := grpc.NewServer( - grpc_middleware.WithUnaryServerChain( - grpc_ctxtags.UnaryServerInterceptor(), - grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...), - ), - grpc_middleware.WithStreamServerChain( - grpc_ctxtags.StreamServerInterceptor(), - grpc_logrus.StreamServerInterceptor(logrusEntry, opts...), - ), -) -return server -``` - -
- ## Imported Packages - [github.com/golang/protobuf/jsonpb](https://godoc.org/github.com/golang/protobuf/jsonpb) @@ -143,11 +63,6 @@ return server * [func WithDurationField(f DurationToField) Option](#WithDurationField) * [func WithLevels(f CodeToLevel) Option](#WithLevels) -#### Examples -* [Package (HandlerUsageUnaryPing)](#example__handlerUsageUnaryPing) -* [Package (Initialization)](#example__initialization) -* [Package (InitializationWithDurationFieldOverride)](#example__initializationWithDurationFieldOverride) - #### Package files [client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [noop.go](./noop.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go) diff --git a/logging/logrus/examples_test.go b/logging/logrus/examples_test.go index 414ec2443..94c9989d0 100644 --- a/logging/logrus/examples_test.go +++ b/logging/logrus/examples_test.go @@ -1,6 +1,3 @@ -// Copyright 2017 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - package grpc_logrus_test import ( @@ -16,8 +13,13 @@ import ( "google.golang.org/grpc" ) +var ( + logrusLogger *logrus.Logger + customFunc grpc_logrus.CodeToLevel +) + // Initialization shows a relatively complex initialization sequence. -func Example_initialization(logrusLogger *logrus.Logger, customFunc grpc_logrus.CodeToLevel) *grpc.Server { +func Example_Initialization() { // Logrus entry is used, allowing pre-definition of certain fields by the user. logrusEntry := logrus.NewEntry(logrusLogger) // Shared options for the logger, with a custom gRPC code to log level function. @@ -27,7 +29,7 @@ func Example_initialization(logrusLogger *logrus.Logger, customFunc grpc_logrus. // Make sure that log statements internal to gRPC library are logged using the zapLogger as well. grpc_logrus.ReplaceGrpcLogger(logrusEntry) // Create a server, make sure we put the grpc_ctxtags context before everything else. - server := grpc.NewServer( + _ = grpc.NewServer( grpc_middleware.WithUnaryServerChain( grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...), @@ -37,10 +39,9 @@ func Example_initialization(logrusLogger *logrus.Logger, customFunc grpc_logrus. grpc_logrus.StreamServerInterceptor(logrusEntry, opts...), ), ) - return server } -func Example_initializationWithDurationFieldOverride(logrusLogger *logrus.Logger) *grpc.Server { +func Example_InitializationWithDurationFieldOverride() { // Logrus entry is used, allowing pre-definition of certain fields by the user. logrusEntry := logrus.NewEntry(logrusLogger) // Shared options for the logger, with a custom duration to log field function. @@ -49,7 +50,7 @@ func Example_initializationWithDurationFieldOverride(logrusLogger *logrus.Logger return "grpc.time_ns", duration.Nanoseconds() }), } - server := grpc.NewServer( + _ = grpc.NewServer( grpc_middleware.WithUnaryServerChain( grpc_ctxtags.UnaryServerInterceptor(), grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...), @@ -59,12 +60,11 @@ func Example_initializationWithDurationFieldOverride(logrusLogger *logrus.Logger grpc_logrus.StreamServerInterceptor(logrusEntry, opts...), ), ) - return server } // Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements. -func Example_handlerUsageUnaryPing() interface{} { - x := func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { +func Example_HandlerUsageUnaryPing() { + _ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { // Add fields the ctxtags of the request which will be added to all extracted loggers. grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) // Extract a single request-scoped logrus.Logger and log messages. @@ -73,5 +73,4 @@ func Example_handlerUsageUnaryPing() interface{} { l.Info("another ping") return &pb_testproto.PingResponse{Value: ping.Value}, nil } - return x } diff --git a/logging/zap/DOC.md b/logging/zap/DOC.md index bf180e140..23984977c 100644 --- a/logging/zap/DOC.md +++ b/logging/zap/DOC.md @@ -4,7 +4,6 @@ * [Overview](#pkg-overview) * [Imported Packages](#pkg-imports) * [Index](#pkg-index) -* [Examples](#pkg-examples) ## Overview `grpc_zap` is a gRPC logging middleware backed by ZAP loggers @@ -27,82 +26,6 @@ ZAP can also be made as a backend for gRPC library internals. For that use `Repl Please see examples and tests for examples of use. -#### Example: - -
-Click to expand code. - -```go -x := func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { - // Add fields the ctxtags of the request which will be added to all extracted loggers. - grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) - // Extract a single request-scoped zap.Logger and log messages. - l := grpc_zap.Extract(ctx) - l.Info("some ping") - l.Info("another ping") - return &pb_testproto.PingResponse{Value: ping.Value}, nil -} -return x -``` - -
- -#### Example: - -
-Click to expand code. - -```go -// Shared options for the logger, with a custom gRPC code to log level function. -opts := []grpc_zap.Option{ - grpc_zap.WithLevels(customFunc), -} -// Make sure that log statements internal to gRPC library are logged using the zapLogger as well. -grpc_zap.ReplaceGrpcLogger(zapLogger) -// Create a server, make sure we put the grpc_ctxtags context before everything else. -server := grpc.NewServer( - grpc_middleware.WithUnaryServerChain( - grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), - grpc_zap.UnaryServerInterceptor(zapLogger, opts...), - ), - grpc_middleware.WithStreamServerChain( - grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), - grpc_zap.StreamServerInterceptor(zapLogger, opts...), - ), -) -return server -``` - -
- -#### Example: - -
-Click to expand code. - -```go -opts := []grpc_zap.Option{ - grpc_zap.WithDurationField(func(duration time.Duration) zapcore.Field { - return zap.Int64("grpc.time_ns", duration.Nanoseconds()) - }), -} - -server := grpc.NewServer( - grpc_middleware.WithUnaryServerChain( - grpc_ctxtags.UnaryServerInterceptor(), - grpc_zap.UnaryServerInterceptor(zapLogger, opts...), - ), - grpc_middleware.WithStreamServerChain( - grpc_ctxtags.StreamServerInterceptor(), - grpc_zap.StreamServerInterceptor(zapLogger, opts...), - ), -) - -return server -``` - -
- ## Imported Packages - [github.com/golang/protobuf/jsonpb](https://godoc.org/github.com/golang/protobuf/jsonpb) @@ -141,11 +64,6 @@ return server * [func WithDurationField(f DurationToField) Option](#WithDurationField) * [func WithLevels(f CodeToLevel) Option](#WithLevels) -#### Examples -* [Package (HandlerUsageUnaryPing)](#example__handlerUsageUnaryPing) -* [Package (Initialization)](#example__initialization) -* [Package (InitializationWithDurationFieldOverride)](#example__initializationWithDurationFieldOverride) - #### Package files [client_interceptors.go](./client_interceptors.go) [context.go](./context.go) [doc.go](./doc.go) [grpclogger.go](./grpclogger.go) [options.go](./options.go) [payload_interceptors.go](./payload_interceptors.go) [server_interceptors.go](./server_interceptors.go) diff --git a/logging/zap/examples_test.go b/logging/zap/examples_test.go index 9ae5f1841..926b75f47 100644 --- a/logging/zap/examples_test.go +++ b/logging/zap/examples_test.go @@ -1,6 +1,3 @@ -// Copyright 2017 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - package grpc_zap_test import ( @@ -17,8 +14,13 @@ import ( "google.golang.org/grpc" ) +var ( + zapLogger *zap.Logger + customFunc grpc_zap.CodeToLevel +) + // Initialization shows a relatively complex initialization sequence. -func Example_initialization(zapLogger *zap.Logger, customFunc grpc_zap.CodeToLevel) *grpc.Server { +func Example_Initialization() { // Shared options for the logger, with a custom gRPC code to log level function. opts := []grpc_zap.Option{ grpc_zap.WithLevels(customFunc), @@ -26,7 +28,7 @@ func Example_initialization(zapLogger *zap.Logger, customFunc grpc_zap.CodeToLev // Make sure that log statements internal to gRPC library are logged using the zapLogger as well. grpc_zap.ReplaceGrpcLogger(zapLogger) // Create a server, make sure we put the grpc_ctxtags context before everything else. - server := grpc.NewServer( + _ = grpc.NewServer( grpc_middleware.WithUnaryServerChain( grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), grpc_zap.UnaryServerInterceptor(zapLogger, opts...), @@ -36,18 +38,17 @@ func Example_initialization(zapLogger *zap.Logger, customFunc grpc_zap.CodeToLev grpc_zap.StreamServerInterceptor(zapLogger, opts...), ), ) - return server } // Initialization shows an initialization sequence with the duration field generation overridden. -func Example_initializationWithDurationFieldOverride(zapLogger *zap.Logger) *grpc.Server { +func Example_InitializationWithDurationFieldOverride() { opts := []grpc_zap.Option{ grpc_zap.WithDurationField(func(duration time.Duration) zapcore.Field { return zap.Int64("grpc.time_ns", duration.Nanoseconds()) }), } - server := grpc.NewServer( + _ = grpc.NewServer( grpc_middleware.WithUnaryServerChain( grpc_ctxtags.UnaryServerInterceptor(), grpc_zap.UnaryServerInterceptor(zapLogger, opts...), @@ -57,13 +58,11 @@ func Example_initializationWithDurationFieldOverride(zapLogger *zap.Logger) *grp grpc_zap.StreamServerInterceptor(zapLogger, opts...), ), ) - - return server } // Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements. -func Example_handlerUsageUnaryPing() interface{} { - x := func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { +func Example_HandlerUsageUnaryPing() { + _ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { // Add fields the ctxtags of the request which will be added to all extracted loggers. grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) // Extract a single request-scoped zap.Logger and log messages. @@ -72,5 +71,4 @@ func Example_handlerUsageUnaryPing() interface{} { l.Info("another ping") return &pb_testproto.PingResponse{Value: ping.Value}, nil } - return x } diff --git a/makefile b/makefile new file mode 100644 index 000000000..4ad54c373 --- /dev/null +++ b/makefile @@ -0,0 +1,19 @@ +SHELL="/bin/bash" + +GOFILES_NOVENDOR = $(shell go list ./... | grep -v /vendor/) + +all: vet fmt docs test + +docs: + ./scripts/fixup.sh + +fmt: + go fmt $(GOFILES_NOVENDOR) + +vet: + go vet $(GOFILES_NOVENDOR) + +test: vet + ./scripts/test_all.sh + +.PHONY: all docs validate test diff --git a/recovery/DOC.md b/recovery/DOC.md index 743263dbd..d4ef78af5 100644 --- a/recovery/DOC.md +++ b/recovery/DOC.md @@ -22,21 +22,35 @@ Please see examples for simple examples of use. Click to expand code. ```go -// Shared options for the logger, with a custom gRPC code to log level function. -opts := []grpc_recovery.Option{ - grpc_recovery.WithRecoveryHandler(customFunc), -} -// Create a server. Recovery handlers should typically be last in the chain so that other middleware -// (e.g. logging) can operate on the recovered state instead of being directly affected by any panic -server := grpc.NewServer( - grpc_middleware.WithUnaryServerChain( - grpc_recovery.UnaryServerInterceptor(opts...), - ), - grpc_middleware.WithStreamServerChain( - grpc_recovery.StreamServerInterceptor(opts...), - ), +package grpc_recovery_test + +import ( + "github.com/grpc-ecosystem/go-grpc-middleware" + "github.com/grpc-ecosystem/go-grpc-middleware/recovery" + "google.golang.org/grpc" +) + +var ( + customFunc grpc_recovery.RecoveryHandlerFunc ) -return server + +// Initialization shows an initialization sequence with a custom recovery handler func. +func Example_initialization() { + // Shared options for the logger, with a custom gRPC code to log level function. + opts := []grpc_recovery.Option{ + grpc_recovery.WithRecoveryHandler(customFunc), + } + // Create a server. Recovery handlers should typically be last in the chain so that other middleware + // (e.g. logging) can operate on the recovered state instead of being directly affected by any panic + _ = grpc.NewServer( + grpc_middleware.WithUnaryServerChain( + grpc_recovery.UnaryServerInterceptor(opts...), + ), + grpc_middleware.WithStreamServerChain( + grpc_recovery.StreamServerInterceptor(opts...), + ), + ) +} ``` diff --git a/recovery/examples_test.go b/recovery/examples_test.go index 691ef1b3c..b07af41d4 100644 --- a/recovery/examples_test.go +++ b/recovery/examples_test.go @@ -9,15 +9,19 @@ import ( "google.golang.org/grpc" ) +var ( + customFunc grpc_recovery.RecoveryHandlerFunc +) + // Initialization shows an initialization sequence with a custom recovery handler func. -func Example_initialization(customFunc grpc_recovery.RecoveryHandlerFunc) *grpc.Server { +func Example_initialization() { // Shared options for the logger, with a custom gRPC code to log level function. opts := []grpc_recovery.Option{ grpc_recovery.WithRecoveryHandler(customFunc), } // Create a server. Recovery handlers should typically be last in the chain so that other middleware // (e.g. logging) can operate on the recovered state instead of being directly affected by any panic - server := grpc.NewServer( + _ = grpc.NewServer( grpc_middleware.WithUnaryServerChain( grpc_recovery.UnaryServerInterceptor(opts...), ), @@ -25,5 +29,4 @@ func Example_initialization(customFunc grpc_recovery.RecoveryHandlerFunc) *grpc. grpc_recovery.StreamServerInterceptor(opts...), ), ) - return server } diff --git a/retry/DOC.md b/retry/DOC.md index a2c33df28..e6dc5121e 100644 --- a/retry/DOC.md +++ b/retry/DOC.md @@ -4,7 +4,6 @@ * [Overview](#pkg-overview) * [Imported Packages](#pkg-imports) * [Index](#pkg-index) -* [Examples](#pkg-examples) ## Overview `grpc_retry` provides client-side request retry logic for gRPC. @@ -23,84 +22,6 @@ linear backoff with 10% jitter. Please see examples for more advanced use. -#### Example: - -
-Click to expand code. - -```go -client := pb_testproto.NewTestServiceClient(cc) -pong, err := client.Ping( - newCtx(5*time.Second), - &pb_testproto.PingRequest{}, - grpc_retry.WithMax(3), - grpc_retry.WithPerRetryTimeout(1*time.Second)) -if err != nil { - return err -} -fmt.Printf("got pong: %v", pong) -return nil -``` - -
- -#### Example: - -
-Click to expand code. - -```go -opts := []grpc_retry.CallOption{ - grpc_retry.WithBackoff(grpc_retry.BackoffLinear(100 * time.Millisecond)), - grpc_retry.WithCodes(codes.NotFound, codes.Aborted), -} -return grpc.Dial("myservice.example.com", - grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)), - grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(opts...)), -) -``` - -
- -#### Example: - -
-Click to expand code. - -```go -return grpc.Dial("myservice.example.com", - grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor()), - grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor()), -) -``` - -
- -#### Example: - -
-Click to expand code. - -```go -client := pb_testproto.NewTestServiceClient(cc) -stream, err := client.PingList(newCtx(1*time.Second), &pb_testproto.PingRequest{}, grpc_retry.WithMax(3)) -if err != nil { - return err -} -for { - pong, err := stream.Recv() // retries happen here - if err == io.EOF { - break - } else if err != nil { - return err - } - fmt.Printf("got pong: %v", pong) -} -return nil -``` - -
- ## Imported Packages - [github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils](./../util/backoffutils) @@ -126,12 +47,6 @@ return nil * [func WithMax(maxRetries uint) CallOption](#WithMax) * [func WithPerRetryTimeout(timeout time.Duration) CallOption](#WithPerRetryTimeout) -#### Examples -* [Package (Deadlinecall)](#example__deadlinecall) -* [Package (Dialcomplex)](#example__dialcomplex) -* [Package (Dialsimple)](#example__dialsimple) -* [Package (Simplecall)](#example__simplecall) - #### Package files [backoff.go](./backoff.go) [doc.go](./doc.go) [options.go](./options.go) [retry.go](./retry.go) diff --git a/retry/examples_test.go b/retry/examples_test.go index 560d8aff7..953bbd13c 100644 --- a/retry/examples_test.go +++ b/retry/examples_test.go @@ -4,13 +4,13 @@ package grpc_retry_test import ( - "context" "fmt" "io" "time" "github.com/grpc-ecosystem/go-grpc-middleware/retry" pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto" + "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/codes" ) @@ -23,42 +23,39 @@ func newCtx(timeout time.Duration) context.Context { } // Simple example of using the default interceptor configuration. -func Example_dialsimple() (*grpc.ClientConn, error) { - return grpc.Dial("myservice.example.com", +func Example_DialSimple() { + grpc.Dial("myservice.example.com", grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor()), grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor()), ) } // Complex example with a 100ms linear backoff interval, and retry only on NotFound and Unavailable. -func Example_dialcomplex() (*grpc.ClientConn, error) { +func Example_DialComplex() { opts := []grpc_retry.CallOption{ grpc_retry.WithBackoff(grpc_retry.BackoffLinear(100 * time.Millisecond)), grpc_retry.WithCodes(codes.NotFound, codes.Aborted), } - return grpc.Dial("myservice.example.com", + grpc.Dial("myservice.example.com", grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)), grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(opts...)), ) } // Simple example of an idempotent `ServerStream` call, that will be retried automatically 3 times. -func Example_simplecall() error { +func Example_SimpleCall() { client := pb_testproto.NewTestServiceClient(cc) - stream, err := client.PingList(newCtx(1*time.Second), &pb_testproto.PingRequest{}, grpc_retry.WithMax(3)) - if err != nil { - return err - } + stream, _ := client.PingList(newCtx(1*time.Second), &pb_testproto.PingRequest{}, grpc_retry.WithMax(3)) + for { pong, err := stream.Recv() // retries happen here if err == io.EOF { break } else if err != nil { - return err + return } fmt.Printf("got pong: %v", pong) } - return nil } // This is an example of an `Unary` call that will also retry on deadlines. @@ -70,16 +67,13 @@ func Example_simplecall() error { // // `WithPerRetryTimeout` allows you to shorten the deadline of each retry call, allowing you to fit // multiple retries in the single parent deadline. -func Example_deadlinecall() error { +func Example_DeadlineCall() { client := pb_testproto.NewTestServiceClient(cc) - pong, err := client.Ping( + pong, _ := client.Ping( newCtx(5*time.Second), &pb_testproto.PingRequest{}, grpc_retry.WithMax(3), grpc_retry.WithPerRetryTimeout(1*time.Second)) - if err != nil { - return err - } + fmt.Printf("got pong: %v", pong) - return nil } diff --git a/fixup.sh b/scripts/fixup.sh similarity index 78% rename from fixup.sh rename to scripts/fixup.sh index 3e423f698..a71811851 100755 --- a/fixup.sh +++ b/scripts/fixup.sh @@ -1,7 +1,6 @@ #!/bin/bash # Script that checks the code for errors. -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" GOBIN=${GOBIN:="$GOPATH/bin"} function print_real_go_files { @@ -23,15 +22,6 @@ function generate_markdown { done; } -function goimports_all { - echo "Running goimports" - goimports -l -w $(print_real_go_files) - return $? -} - go get github.com/devnev/godoc2ghmd - - generate_markdown -goimports_all echo "returning $?" diff --git a/test_all.sh b/scripts/test_all.sh similarity index 100% rename from test_all.sh rename to scripts/test_all.sh diff --git a/tags/DOC.md b/tags/DOC.md index 5f21da7cc..4faa60f97 100644 --- a/tags/DOC.md +++ b/tags/DOC.md @@ -4,7 +4,6 @@ * [Overview](#pkg-overview) * [Imported Packages](#pkg-imports) * [Index](#pkg-index) -* [Examples](#pkg-examples) ## Overview `grpc_ctxtags` adds a Tag object to the context that can be used by other middleware to add context about a request. @@ -26,24 +25,6 @@ will be no-ops. This is to ensure that code doesn't panic if the interceptors we Tags fields are typed, and shallow and should follow the OpenTracing semantics convention: https://github.com/opentracing/specification/blob/master/semantic_conventions.md -#### Example: - -
-Click to expand code. - -```go -opts := []grpc_ctxtags.Option{ - grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.TagBasedRequestFieldExtractor("log_fields")), -} -server := grpc.NewServer( - grpc.StreamInterceptor(grpc_ctxtags.StreamServerInterceptor(opts...)), - grpc.UnaryInterceptor(grpc_ctxtags.UnaryServerInterceptor(opts...)), -) -return server -``` - -
- ## Imported Packages - [github.com/grpc-ecosystem/go-grpc-middleware](./..) @@ -66,9 +47,6 @@ return server * [func (t \*Tags) Set(key string, value interface{}) \*Tags](#Tags.Set) * [func (t \*Tags) Values() map[string]interface{}](#Tags.Values) -#### Examples -* [Package (Initialization)](#example__initialization) - #### Package files [context.go](./context.go) [doc.go](./doc.go) [fieldextractor.go](./fieldextractor.go) [interceptors.go](./interceptors.go) [options.go](./options.go) diff --git a/tags/examples_test.go b/tags/examples_test.go index e699aa81c..10c443d68 100644 --- a/tags/examples_test.go +++ b/tags/examples_test.go @@ -1,6 +1,3 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - package grpc_ctxtags_test import ( @@ -9,26 +6,23 @@ import ( ) // Simple example of server initialization code, with data automatically populated from `log_fields` Golang tags. -func Example_initialization() *grpc.Server { +func Example_Initialization() { opts := []grpc_ctxtags.Option{ grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.TagBasedRequestFieldExtractor("log_fields")), } - server := grpc.NewServer( + _ = grpc.NewServer( grpc.StreamInterceptor(grpc_ctxtags.StreamServerInterceptor(opts...)), grpc.UnaryInterceptor(grpc_ctxtags.UnaryServerInterceptor(opts...)), ) - return server } // Example using WithFieldExtractorForInitialReq -func Example_initialization_forInitialReq() *grpc.Server { +func Example_InitializationForInitialReq() { opts := []grpc_ctxtags.Option{ grpc_ctxtags.WithFieldExtractorForInitialReq(grpc_ctxtags.TagBasedRequestFieldExtractor("log_fields")), } - server := grpc.NewServer( + _ = grpc.NewServer( grpc.StreamInterceptor(grpc_ctxtags.StreamServerInterceptor(opts...)), grpc.UnaryInterceptor(grpc_ctxtags.UnaryServerInterceptor(opts...)), ) - - return server }