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