-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_option.go
More file actions
228 lines (201 loc) · 7.44 KB
/
server_option.go
File metadata and controls
228 lines (201 loc) · 7.44 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package srpc
import (
"compress/gzip"
"io"
"math"
"net/http"
"time"
"github.com/opensraph/srpc/compress"
"github.com/opensraph/srpc/encoding"
"github.com/opensraph/srpc/mem"
"google.golang.org/grpc/credentials"
_ "github.com/opensraph/srpc/protocol/connect" // register connect protocol
_ "github.com/opensraph/srpc/protocol/grpc" // register grpc protocol
_ "github.com/opensraph/srpc/encoding/protobinary" // register protobuf codec
_ "github.com/opensraph/srpc/encoding/protojson" // register json codec
)
const (
defaultServerMaxConcurrentStreams = math.MaxUint32
defaultServerMaxReceiveMessageSize = 4 * 1024 * 1024 // 4MB
defaultServerMaxSendMessageSize = math.MaxInt32
)
type ServerOption func(o *serverOptions)
type serverOptions struct {
enableTracing bool
readTimeout time.Duration
writeTimeout time.Duration
idleTimeout time.Duration
maxConcurrentStreams uint32
maxReceiveMessageSize int
maxSendMessageSize int
creds credentials.TransportCredentials
interceptor Interceptor
globalHandler func(next http.Handler) http.Handler
unknownHandler http.Handler
compressionNames []string
compressionPools map[string]*compress.CompressionPool
compressMinBytes int
codecs encoding.ReadOnlyCodecs
bufferPool mem.BufferPool
}
var defaultServerOptions = serverOptions{
enableTracing: false,
maxConcurrentStreams: defaultServerMaxConcurrentStreams,
maxReceiveMessageSize: defaultServerMaxReceiveMessageSize,
maxSendMessageSize: defaultServerMaxSendMessageSize,
bufferPool: mem.DefaultBufferPool(),
interceptor: Interceptor{
chainUnaryServerInts: make([]UnaryServerInterceptor, 0),
chainStreamServerInts: make([]StreamServerInterceptor, 0),
},
unknownHandler: http.NotFoundHandler(),
}
var globalServerOptions []ServerOption = []ServerOption{
gzipCompression(),
Codec(
encoding.GetCodec(encoding.CodecNameProto),
encoding.GetCodec(encoding.CodecNameJSON),
encoding.GetCodec(encoding.CodecNameJSONCharsetUTF8),
),
}
// EnableTracing returns a ServerOption that enables tracing for the server.
// This is useful for debugging and monitoring the server's performance.
// If not set, tracing is disabled by default.
func EnableTracing() ServerOption {
return func(o *serverOptions) {
o.enableTracing = true
}
}
// ReadTimeout returns a ServerOption that sets the read timeout for the server.
func ReadTimeout(d time.Duration) ServerOption {
return func(o *serverOptions) { o.readTimeout = d }
}
// WriteTimeout returns a ServerOption that sets the write timeout for the server.
func WriteTimeout(d time.Duration) ServerOption {
return func(o *serverOptions) {
o.writeTimeout = d
}
}
// IdleTimeout returns a ServerOption that sets the idle timeout for the server.
func IdleTimeout(d time.Duration) ServerOption {
return func(o *serverOptions) {
o.idleTimeout = d
}
}
// MaxConcurrentStreams returns a ServerOption that will apply a limit on the number
// of concurrent streams to each ServerTransport.
func MaxConcurrentStreams(n uint32) ServerOption {
return func(o *serverOptions) {
if n == 0 {
n = defaultServerMaxConcurrentStreams
}
o.maxConcurrentStreams = n
}
}
// MaxRecvMsgSize returns a ServerOption to set the max message size in bytes the server can receive.
// If this is not set, gRPC uses the default 4MB.
func MaxRecvMsgSize(n int) ServerOption {
return func(o *serverOptions) {
o.maxReceiveMessageSize = n
}
}
// MaxSendMsgSize returns a ServerOption to set the max message size in bytes the server can send.
// If this is not set, gRPC uses the default `math.MaxInt32`.
func MaxSendMsgSize(n int) ServerOption {
return func(o *serverOptions) {
o.maxSendMessageSize = n
}
}
// Creds returns a ServerOption that sets credentials for server connections.
func Creds(c credentials.TransportCredentials) ServerOption {
return func(o *serverOptions) {
o.creds = c
}
}
// GlobalHandler returns a ServerOption that sets the global handler for the server.
// This handler will be used for all requests that do not match any registered service or method.
func GlobalHandler(h func(next http.Handler) http.Handler) ServerOption {
return func(o *serverOptions) {
o.globalHandler = h
}
}
// UnknownHandler returns a ServerOption that sets the handler for unknown
// requests. This is useful for handling requests that do not match any
// registered service or method. The handler should be a http.Handler that
// can handle the request and return a response. If not set, the server will
// return a 404 Not Found response for unknown requests.
func UnknownHandler(h http.Handler) ServerOption {
return func(o *serverOptions) {
o.unknownHandler = h
}
}
// Compression returns a ServerOption that sets the compression algorithm
func Compression(name string, decompressor compress.Decompressor, compressor compress.Compressor) ServerOption {
return func(o *serverOptions) {
if o.compressionPools == nil {
o.compressionPools = make(map[string]*compress.CompressionPool)
}
o.compressionNames = append(o.compressionNames, name)
o.compressionPools[name] = compress.NewCompressionPool(compressor, decompressor)
}
}
func gzipCompression() ServerOption {
return Compression(compress.CompressionGzip, &gzip.Reader{}, gzip.NewWriter(io.Discard))
}
// CompressionMinBytes returns a ServerOption that sets the minimum number of bytes
// for compression to be applied. This is useful for tuning the performance of the
// server. If not set, the default value is 0, which means no minimum size.
// Compression will be applied to all messages.
func CompressionMinBytes(n int) ServerOption {
return func(o *serverOptions) {
o.compressMinBytes = n
}
}
// Codec returns a ServerOption that sets the codecs for the server.
func Codec(codecs ...encoding.Codec) ServerOption {
return func(o *serverOptions) {
if o.codecs == nil {
o.codecs = make(map[string]encoding.Codec)
}
for _, codec := range codecs {
if codec == nil {
continue
}
name := codec.Name()
if name == "" {
continue
}
o.codecs[name] = codec
}
}
}
// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the server.
func UnaryInterceptor(i UnaryServerInterceptor) ServerOption {
return func(o *serverOptions) {
o.interceptor.ChainUnaryInterceptor(i)
}
}
// ChainUnaryInterceptor returns a ServerOption that specifies the chained interceptor
// for unary RPCs. The first interceptor will be the outer most,
// while the last interceptor will be the inner most wrapper around the real call.
// All unary interceptors added by this method will be chained.
func ChainUnaryInterceptor(ints ...UnaryServerInterceptor) ServerOption {
return func(o *serverOptions) {
o.interceptor.ChainUnaryInterceptor(ints...)
}
}
// StreamInterceptor returns a ServerOption that sets the StreamServerInterceptor for the server.
func StreamInterceptor(i StreamServerInterceptor) ServerOption {
return func(o *serverOptions) {
o.interceptor.ChainStreamInterceptor(i)
}
}
// ChainStreamInterceptor returns a ServerOption that specifies the chained interceptor
// for stream RPCs. The first interceptor will be the outer most,
// while the last interceptor will be the inner most wrapper around the real call.
// All stream interceptors added by this method will be chained.
func ChainStreamInterceptor(ints ...StreamServerInterceptor) ServerOption {
return func(o *serverOptions) {
o.interceptor.ChainStreamInterceptor(ints...)
}
}