interceptor.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ttrpc
  14. import "context"
  15. // UnaryServerInfo provides information about the server request
  16. type UnaryServerInfo struct {
  17. FullMethod string
  18. }
  19. // UnaryClientInfo provides information about the client request
  20. type UnaryClientInfo struct {
  21. FullMethod string
  22. }
  23. // StreamServerInfo provides information about the server request
  24. type StreamServerInfo struct {
  25. FullMethod string
  26. StreamingClient bool
  27. StreamingServer bool
  28. }
  29. // Unmarshaler contains the server request data and allows it to be unmarshaled
  30. // into a concrete type
  31. type Unmarshaler func(interface{}) error
  32. // Invoker invokes the client's request and response from the ttrpc server
  33. type Invoker func(context.Context, *Request, *Response) error
  34. // UnaryServerInterceptor specifies the interceptor function for server request/response
  35. type UnaryServerInterceptor func(context.Context, Unmarshaler, *UnaryServerInfo, Method) (interface{}, error)
  36. // UnaryClientInterceptor specifies the interceptor function for client request/response
  37. type UnaryClientInterceptor func(context.Context, *Request, *Response, *UnaryClientInfo, Invoker) error
  38. func defaultServerInterceptor(ctx context.Context, unmarshal Unmarshaler, _ *UnaryServerInfo, method Method) (interface{}, error) {
  39. return method(ctx, unmarshal)
  40. }
  41. func defaultClientInterceptor(ctx context.Context, req *Request, resp *Response, _ *UnaryClientInfo, invoker Invoker) error {
  42. return invoker(ctx, req, resp)
  43. }
  44. type StreamServerInterceptor func(context.Context, StreamServer, *StreamServerInfo, StreamHandler) (interface{}, error)
  45. func defaultStreamServerInterceptor(ctx context.Context, ss StreamServer, _ *StreamServerInfo, stream StreamHandler) (interface{}, error) {
  46. return stream(ctx, ss)
  47. }
  48. type StreamClientInterceptor func(context.Context)