Merge pull request #41 from crosbymichael/interceptors
Add client and server unary interceptors
This commit is contained in:
commit
d134fe75a4
17
client.go
17
client.go
@ -36,6 +36,7 @@ import (
|
||||
// closed.
|
||||
var ErrClosed = errors.New("ttrpc: closed")
|
||||
|
||||
// Client for a ttrpc server
|
||||
type Client struct {
|
||||
codec codec
|
||||
conn net.Conn
|
||||
@ -47,16 +48,26 @@ type Client struct {
|
||||
closeFunc func()
|
||||
done chan struct{}
|
||||
err error
|
||||
interceptor UnaryClientInterceptor
|
||||
}
|
||||
|
||||
// ClientOpts configures a client
|
||||
type ClientOpts func(c *Client)
|
||||
|
||||
// WithOnClose sets the close func whenever the client's Close() method is called
|
||||
func WithOnClose(onClose func()) ClientOpts {
|
||||
return func(c *Client) {
|
||||
c.closeFunc = onClose
|
||||
}
|
||||
}
|
||||
|
||||
// WithUnaryClientInterceptor sets the provided client interceptor
|
||||
func WithUnaryClientInterceptor(i UnaryClientInterceptor) ClientOpts {
|
||||
return func(c *Client) {
|
||||
c.interceptor = i
|
||||
}
|
||||
}
|
||||
|
||||
func NewClient(conn net.Conn, opts ...ClientOpts) *Client {
|
||||
c := &Client{
|
||||
codec: codec{},
|
||||
@ -66,6 +77,7 @@ func NewClient(conn net.Conn, opts ...ClientOpts) *Client {
|
||||
closed: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
closeFunc: func() {},
|
||||
interceptor: defaultClientInterceptor,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
@ -107,7 +119,10 @@ func (c *Client) Call(ctx context.Context, service, method string, req, resp int
|
||||
creq.TimeoutNano = dl.Sub(time.Now()).Nanoseconds()
|
||||
}
|
||||
|
||||
if err := c.dispatch(ctx, creq, cresp); err != nil {
|
||||
info := &UnaryClientInfo{
|
||||
FullMethod: fullPath(service, method),
|
||||
}
|
||||
if err := c.interceptor(ctx, creq, cresp, info, c.dispatch); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
13
config.go
13
config.go
@ -20,8 +20,10 @@ import "github.com/pkg/errors"
|
||||
|
||||
type serverConfig struct {
|
||||
handshaker Handshaker
|
||||
interceptor UnaryServerInterceptor
|
||||
}
|
||||
|
||||
// ServerOpt for configuring a ttrpc server
|
||||
type ServerOpt func(*serverConfig) error
|
||||
|
||||
// WithServerHandshaker can be passed to NewServer to ensure that the
|
||||
@ -37,3 +39,14 @@ func WithServerHandshaker(handshaker Handshaker) ServerOpt {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithUnaryServerInterceptor sets the provided interceptor on the server
|
||||
func WithUnaryServerInterceptor(i UnaryServerInterceptor) ServerOpt {
|
||||
return func(c *serverConfig) error {
|
||||
if c.interceptor != nil {
|
||||
return errors.New("only one interceptor allowed per server")
|
||||
}
|
||||
c.interceptor = i
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
135
example/cmd/main.go
Normal file
135
example/cmd/main.go
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
Copyright The containerd Authors.
|
||||
|
||||
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
|
||||
|
||||
http://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.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
context "context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
ttrpc "github.com/containerd/ttrpc"
|
||||
"github.com/containerd/ttrpc/example"
|
||||
"github.com/gogo/protobuf/types"
|
||||
)
|
||||
|
||||
const socket = "example-ttrpc-server"
|
||||
|
||||
func main() {
|
||||
if err := handle(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func handle() error {
|
||||
command := os.Args[1]
|
||||
switch command {
|
||||
case "server":
|
||||
return server()
|
||||
case "client":
|
||||
return client()
|
||||
default:
|
||||
return errors.New("invalid command")
|
||||
}
|
||||
}
|
||||
|
||||
func serverIntercept(ctx context.Context, um ttrpc.Unmarshaler, i *ttrpc.UnaryServerInfo, m ttrpc.Method) (interface{}, error) {
|
||||
log.Println("server interceptor")
|
||||
dumpMetadata(ctx)
|
||||
return m(ctx, um)
|
||||
}
|
||||
|
||||
func clientIntercept(ctx context.Context, req *ttrpc.Request, resp *ttrpc.Response, i *ttrpc.UnaryClientInfo, invoker ttrpc.Invoker) error {
|
||||
log.Println("client interceptor")
|
||||
dumpMetadata(ctx)
|
||||
return invoker(ctx, req, resp)
|
||||
}
|
||||
|
||||
func dumpMetadata(ctx context.Context) {
|
||||
md, ok := ttrpc.GetMetadata(ctx)
|
||||
if !ok {
|
||||
panic("no metadata")
|
||||
}
|
||||
if err := json.NewEncoder(os.Stdout).Encode(md); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func server() error {
|
||||
s, err := ttrpc.NewServer(
|
||||
ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()),
|
||||
ttrpc.WithUnaryServerInterceptor(serverIntercept),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer s.Close()
|
||||
example.RegisterExampleService(s, &exampleServer{})
|
||||
|
||||
l, err := net.Listen("unix", socket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
l.Close()
|
||||
os.Remove(socket)
|
||||
}()
|
||||
return s.Serve(context.Background(), l)
|
||||
}
|
||||
|
||||
func client() error {
|
||||
conn, err := net.Dial("unix", socket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
tc := ttrpc.NewClient(conn, ttrpc.WithUnaryClientInterceptor(clientIntercept))
|
||||
client := example.NewExampleClient(tc)
|
||||
|
||||
r := &example.Method1Request{
|
||||
Foo: os.Args[2],
|
||||
Bar: os.Args[3],
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
md := ttrpc.Metadata{}
|
||||
md.Set("name", "koye")
|
||||
ctx = ttrpc.WithMetadata(ctx, md)
|
||||
|
||||
resp, err := client.Method1(ctx, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(resp)
|
||||
}
|
||||
|
||||
type exampleServer struct {
|
||||
}
|
||||
|
||||
func (s *exampleServer) Method1(ctx context.Context, r *example.Method1Request) (*example.Method1Response, error) {
|
||||
return &example.Method1Response{
|
||||
Foo: r.Foo,
|
||||
Bar: r.Bar,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *exampleServer) Method2(ctx context.Context, r *example.Method1Request) (*types.Empty, error) {
|
||||
return &types.Empty{}, nil
|
||||
}
|
50
interceptor.go
Normal file
50
interceptor.go
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Copyright The containerd Authors.
|
||||
|
||||
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
|
||||
|
||||
http://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.
|
||||
*/
|
||||
|
||||
package ttrpc
|
||||
|
||||
import "context"
|
||||
|
||||
// UnaryServerInfo provides information about the server request
|
||||
type UnaryServerInfo struct {
|
||||
FullMethod string
|
||||
}
|
||||
|
||||
// UnaryClientInfo provides information about the client request
|
||||
type UnaryClientInfo struct {
|
||||
FullMethod string
|
||||
}
|
||||
|
||||
// Unmarshaler contains the server request data and allows it to be unmarshaled
|
||||
// into a concrete type
|
||||
type Unmarshaler func(interface{}) error
|
||||
|
||||
// Invoker invokes the client's request and response from the ttrpc server
|
||||
type Invoker func(context.Context, *Request, *Response) error
|
||||
|
||||
// UnaryServerInterceptor specifies the interceptor function for server request/response
|
||||
type UnaryServerInterceptor func(context.Context, Unmarshaler, *UnaryServerInfo, Method) (interface{}, error)
|
||||
|
||||
// UnaryClientInterceptor specifies the interceptor function for client request/response
|
||||
type UnaryClientInterceptor func(context.Context, *Request, *Response, *UnaryClientInfo, Invoker) error
|
||||
|
||||
func defaultServerInterceptor(ctx context.Context, unmarshal Unmarshaler, info *UnaryServerInfo, method Method) (interface{}, error) {
|
||||
return method(ctx, unmarshal)
|
||||
}
|
||||
|
||||
func defaultClientInterceptor(ctx context.Context, req *Request, resp *Response, _ *UnaryClientInfo, invoker Invoker) error {
|
||||
return invoker(ctx, req, resp)
|
||||
}
|
@ -53,10 +53,13 @@ func NewServer(opts ...ServerOpt) (*Server, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if config.interceptor == nil {
|
||||
config.interceptor = defaultServerInterceptor
|
||||
}
|
||||
|
||||
return &Server{
|
||||
config: config,
|
||||
services: newServiceSet(),
|
||||
services: newServiceSet(config.interceptor),
|
||||
done: make(chan struct{}),
|
||||
listeners: make(map[net.Listener]struct{}),
|
||||
connections: make(map[*serverConn]struct{}),
|
||||
|
10
services.go
10
services.go
@ -38,11 +38,13 @@ type ServiceDesc struct {
|
||||
|
||||
type serviceSet struct {
|
||||
services map[string]ServiceDesc
|
||||
interceptor UnaryServerInterceptor
|
||||
}
|
||||
|
||||
func newServiceSet() *serviceSet {
|
||||
func newServiceSet(interceptor UnaryServerInterceptor) *serviceSet {
|
||||
return &serviceSet{
|
||||
services: make(map[string]ServiceDesc),
|
||||
interceptor: interceptor,
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,7 +86,11 @@ func (s *serviceSet) dispatch(ctx context.Context, serviceName, methodName strin
|
||||
return nil
|
||||
}
|
||||
|
||||
resp, err := method(ctx, unmarshal)
|
||||
info := &UnaryServerInfo{
|
||||
FullMethod: fullPath(serviceName, methodName),
|
||||
}
|
||||
|
||||
resp, err := s.interceptor(ctx, unmarshal, info, method)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user