mirror of https://github.com/status-im/consul.git
Update to get server interceptor
This commit is contained in:
parent
6387bf102e
commit
212e7c83a1
2
go.mod
2
go.mod
|
@ -38,7 +38,7 @@ require (
|
|||
github.com/hashicorp/go-discover v0.0.0-20200501174627-ad1e96bde088
|
||||
github.com/hashicorp/go-hclog v0.14.1
|
||||
github.com/hashicorp/go-memdb v1.3.1
|
||||
github.com/hashicorp/go-msgpack v0.5.6-0.20210707190205-815e88f18cdf
|
||||
github.com/hashicorp/go-msgpack v0.5.6-0.20210707211311-617cf4904e31
|
||||
github.com/hashicorp/go-multierror v1.1.1
|
||||
github.com/hashicorp/go-raftchunking v0.6.1
|
||||
github.com/hashicorp/go-retryablehttp v0.6.7 // indirect
|
||||
|
|
3
go.sum
3
go.sum
|
@ -241,8 +241,9 @@ github.com/hashicorp/go-memdb v1.3.1 h1:EIj6L28rTL41BDHBvwq1VJXzcmY2R2JBrxpWxF7E
|
|||
github.com/hashicorp/go-memdb v1.3.1/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g=
|
||||
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-msgpack v0.5.6-0.20210707190205-815e88f18cdf h1:R3iTeuV9+CAGu1vDEX6DnMxM29ldoco1IF8EPbBrqNU=
|
||||
github.com/hashicorp/go-msgpack v0.5.6-0.20210707190205-815e88f18cdf/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-msgpack v0.5.6-0.20210707211311-617cf4904e31 h1:j9p4eZr44FuQ4oMYzVV9fHRjAYWbuXRm8wXGajfsV/M=
|
||||
github.com/hashicorp/go-msgpack v0.5.6-0.20210707211311-617cf4904e31/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
|
|
|
@ -191,15 +191,39 @@ type Server struct {
|
|||
freeReq *Request
|
||||
respLock sync.Mutex // protects freeResp
|
||||
freeResp *Response
|
||||
|
||||
interceptor Interceptor
|
||||
}
|
||||
|
||||
// NewServer returns a new Server.
|
||||
func NewServer() *Server {
|
||||
return &Server{}
|
||||
func NewServer(interceptor Interceptor) *Server {
|
||||
if interceptor == nil {
|
||||
interceptor = noopInterceptor{}
|
||||
}
|
||||
return &Server{interceptor: interceptor}
|
||||
}
|
||||
|
||||
// Interceptor is called by Server to allow the caller to intercept requests/
|
||||
type Interceptor interface {
|
||||
// ServiceCall is call for each request. The fn function MUST be called
|
||||
// by ServiceCall to perform the actual RPC. ServiceCall may optionally
|
||||
// do something before and/or after calling fn.
|
||||
// serviceMethod is the "Service.Method" string for the request, and argv and
|
||||
// replyv are the request and response values.
|
||||
//
|
||||
// ServiceCall may be called concurrently by multiple requests, and so must
|
||||
// be safe for concurrent use.
|
||||
ServiceCall(fn func(), serviceMethod string, argv, replyv reflect.Value)
|
||||
}
|
||||
|
||||
type noopInterceptor struct{}
|
||||
|
||||
func (n noopInterceptor) ServiceCall(call func(), serviceMethod string, argv, replyv reflect.Value) {
|
||||
call()
|
||||
}
|
||||
|
||||
// DefaultServer is the default instance of *Server.
|
||||
var DefaultServer = NewServer()
|
||||
var DefaultServer = NewServer(noopInterceptor{})
|
||||
|
||||
// Is this type exported or a builtin?
|
||||
func isExportedOrBuiltinType(t reflect.Type) bool {
|
||||
|
@ -471,7 +495,12 @@ func (server *Server) ServeCodec(codec ServerCodec) {
|
|||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
go service.call(server, sending, wg, mtype, req, argv, replyv, codec)
|
||||
fn := func() {
|
||||
service, mtype, req, argv, replyv := service, mtype, req, argv, replyv // capture
|
||||
// TODO: remove wg arg to service.call, handle it here instead.
|
||||
service.call(server, sending, wg, mtype, req, argv, replyv, codec)
|
||||
}
|
||||
go server.interceptor.ServiceCall(fn, req.ServiceMethod, argv, replyv)
|
||||
}
|
||||
// We've seen that there are no more requests.
|
||||
// Wait for responses to be sent before closing codec.
|
||||
|
@ -495,7 +524,12 @@ func (server *Server) ServeRequest(codec ServerCodec) error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
service.call(server, sending, nil, mtype, req, argv, replyv, codec)
|
||||
|
||||
fn := func() {
|
||||
service.call(server, sending, nil, mtype, req, argv, replyv, codec)
|
||||
}
|
||||
server.interceptor.ServiceCall(fn, req.ServiceMethod, argv, replyv)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -438,7 +438,7 @@ github.com/hashicorp/go-hclog
|
|||
github.com/hashicorp/go-immutable-radix
|
||||
# github.com/hashicorp/go-memdb v1.3.1
|
||||
github.com/hashicorp/go-memdb
|
||||
# github.com/hashicorp/go-msgpack v0.5.6-0.20210707190205-815e88f18cdf
|
||||
# github.com/hashicorp/go-msgpack v0.5.6-0.20210707211311-617cf4904e31
|
||||
github.com/hashicorp/go-msgpack/codec
|
||||
github.com/hashicorp/go-msgpack/net/rpc
|
||||
# github.com/hashicorp/go-multierror v1.1.1
|
||||
|
|
Loading…
Reference in New Issue