github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/grpc/options.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/server/grpc/options.go 16 17 package grpc 18 19 import ( 20 "context" 21 "crypto/tls" 22 "net" 23 24 "github.com/improbable-eng/grpc-web/go/grpcweb" 25 "github.com/tickoalcantara12/micro/v3/service/broker/memory" 26 memReg "github.com/tickoalcantara12/micro/v3/service/registry/memory" 27 "github.com/tickoalcantara12/micro/v3/service/server" 28 "github.com/tickoalcantara12/micro/v3/util/codec" 29 "google.golang.org/grpc" 30 "google.golang.org/grpc/encoding" 31 ) 32 33 type codecsKey struct{} 34 type grpcOptions struct{} 35 type netListener struct{} 36 type maxMsgSizeKey struct{} 37 type maxRecvMsgSizeKey struct{} 38 type maxSendMsgSizeKey struct{} 39 type maxConnKey struct{} 40 type tlsAuth struct{} 41 type grpcWebOptions struct{} 42 type grpcWebPort struct{} 43 44 // gRPC Codec to be used to encode/decode requests for a given content type 45 func Codec(contentType string, c encoding.Codec) server.Option { 46 return func(o *server.Options) { 47 codecs := make(map[string]encoding.Codec) 48 if o.Context == nil { 49 o.Context = context.Background() 50 } 51 if v, ok := o.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && v != nil { 52 codecs = v 53 } 54 codecs[contentType] = c 55 o.Context = context.WithValue(o.Context, codecsKey{}, codecs) 56 } 57 } 58 59 // AuthTLS should be used to setup a secure authentication using TLS 60 func AuthTLS(t *tls.Config) server.Option { 61 return setServerOption(tlsAuth{}, t) 62 } 63 64 // MaxConn specifies maximum number of max simultaneous connections to server 65 func MaxConn(n int) server.Option { 66 return setServerOption(maxConnKey{}, n) 67 } 68 69 // Listener specifies the net.Listener to use instead of the default 70 func Listener(l net.Listener) server.Option { 71 return setServerOption(netListener{}, l) 72 } 73 74 // Options to be used to configure gRPC options 75 func Options(opts ...grpc.ServerOption) server.Option { 76 return setServerOption(grpcOptions{}, opts) 77 } 78 79 // GRPCWebOptions to be used to start a gRPC Web server 80 func GRPCWebOptions(opts ...grpcweb.Option) server.Option { 81 return setServerOption(grpcWebOptions{}, opts) 82 } 83 84 // GRPCWebPort to be used to start a gRPC Web server 85 func GRPCWebPort(addr string) server.Option { 86 return setServerOption(grpcWebPort{}, addr) 87 } 88 89 // 90 // Deprecated: use MaxRecvMsgSize or MaxSendMsgSize instead 91 // MaxMsgSize set the maximum message in bytes the server can receive and 92 // send. Default maximum message size is 4 MB. 93 func MaxMsgSize(s int) server.Option { 94 return setServerOption(maxMsgSizeKey{}, s) 95 } 96 97 // 98 // MaxRecvMsgSize set the maximum size of message that server can receive. 99 // 100 func MaxRecvMsgSize(s int) server.Option { 101 return func(o *server.Options) { 102 if o.Context == nil { 103 o.Context = context.Background() 104 } 105 o.Context = context.WithValue(o.Context, maxRecvMsgSizeKey{}, s) 106 } 107 } 108 109 // 110 // MaxSendMsgSize set the maximum size of message that server can send. 111 // 112 func MaxSendMsgSize(s int) server.Option { 113 return func(o *server.Options) { 114 if o.Context == nil { 115 o.Context = context.Background() 116 } 117 o.Context = context.WithValue(o.Context, maxSendMsgSizeKey{}, s) 118 } 119 } 120 121 func newOptions(opt ...server.Option) server.Options { 122 opts := server.Options{ 123 Codecs: make(map[string]codec.NewCodec), 124 Metadata: map[string]string{}, 125 Broker: memory.NewBroker(), 126 Registry: memReg.NewRegistry(), 127 Address: server.DefaultAddress, 128 Name: server.DefaultName, 129 Id: server.DefaultId, 130 Version: server.DefaultVersion, 131 RegisterInterval: server.DefaultRegisterInterval, 132 RegisterTTL: server.DefaultRegisterTTL, 133 } 134 135 for _, o := range opt { 136 o(&opts) 137 } 138 139 return opts 140 }