github.com/thanos-io/thanos@v0.32.5/pkg/server/grpc/option.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package grpc 5 6 import ( 7 "crypto/tls" 8 "time" 9 10 "google.golang.org/grpc" 11 ) 12 13 type options struct { 14 registerServerFuncs []registerServerFunc 15 16 gracePeriod time.Duration 17 maxConnAge time.Duration 18 listen string 19 network string 20 21 tlsConfig *tls.Config 22 23 grpcOpts []grpc.ServerOption 24 } 25 26 // Option overrides behavior of Server. 27 type Option interface { 28 apply(*options) 29 } 30 31 type optionFunc func(*options) 32 33 func (f optionFunc) apply(o *options) { 34 f(o) 35 } 36 37 type registerServerFunc func(s *grpc.Server) 38 39 // WithServer calls the passed gRPC registration functions on the created 40 // grpc.Server. 41 func WithServer(f registerServerFunc) Option { 42 return optionFunc(func(o *options) { 43 o.registerServerFuncs = append(o.registerServerFuncs, f) 44 }) 45 } 46 47 // WithGRPCServerOption allows adding raw grpc.ServerOption's to the 48 // instantiated gRPC server. 49 func WithGRPCServerOption(opt grpc.ServerOption) Option { 50 return optionFunc(func(o *options) { 51 o.grpcOpts = append(o.grpcOpts, opt) 52 }) 53 } 54 55 // WithGracePeriod sets shutdown grace period for gRPC server. 56 // Server waits connections to drain for specified amount of time. 57 func WithGracePeriod(t time.Duration) Option { 58 return optionFunc(func(o *options) { 59 o.gracePeriod = t 60 }) 61 } 62 63 // WithListen sets address to listen for gRPC server. 64 // Server accepts incoming connections on given address. 65 func WithListen(s string) Option { 66 return optionFunc(func(o *options) { 67 o.listen = s 68 }) 69 } 70 71 // WithNetwork sets network to listen for gRPC server e.g tcp, udp or unix. 72 func WithNetwork(s string) Option { 73 return optionFunc(func(o *options) { 74 o.network = s 75 }) 76 } 77 78 // WithTLSConfig sets TLS configuration for gRPC server. 79 func WithTLSConfig(cfg *tls.Config) Option { 80 return optionFunc(func(o *options) { 81 o.tlsConfig = cfg 82 }) 83 } 84 85 // WithMaxConnAge sets the maximum connection age for gRPC server. 86 func WithMaxConnAge(t time.Duration) Option { 87 return optionFunc(func(o *options) { 88 o.maxConnAge = t 89 }) 90 }