-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
58 lines (47 loc) · 1.35 KB
/
option.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
package grpc
import (
"time"
"github.com/phogolabs/plex/grpc/interceptor"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
// KeepaliveOption represents a keep alive option
// For more information have a look:
//
// https://stackoverflow.com/questions/52993259/problem-with-grpc-setup-getting-an-intermittent-rpc-unavailable-error
var KeepaliveOption = grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: 5 * time.Minute,
})
// GatewayOption represents a gateway option
type GatewayOption interface {
Apply(*Gateway)
}
// GatewayOptionFunc represnts a gateway option func
type GatewayOptionFunc func(*Gateway)
// Apply the option
func (fn GatewayOptionFunc) Apply(gw *Gateway) {
fn(gw)
}
// WithDefault sets the default option
func WithDefault() GatewayOption {
chain := ChainInterceptor{
interceptor.Logger,
interceptor.Recoverer,
interceptor.Defaulter,
interceptor.Transformer,
interceptor.Validator,
}
fn := func(gw *Gateway) {
interceptor := WithChain(chain)
interceptor.Apply(gw)
}
return GatewayOptionFunc(fn)
}
// WithChain sets the interceptor chain
func WithChain(chain ChainInterceptor) GatewayOption {
fn := func(gw *Gateway) {
gw.Options = append(gw.Options, grpc.UnaryInterceptor(chain.Unary))
gw.Options = append(gw.Options, grpc.StreamInterceptor(chain.Stream))
}
return GatewayOptionFunc(fn)
}