handshake.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 (
  15. "context"
  16. "net"
  17. )
  18. // Handshaker defines the interface for connection handshakes performed on the
  19. // server or client when first connecting.
  20. type Handshaker interface {
  21. // Handshake should confirm or decorate a connection that may be incoming
  22. // to a server or outgoing from a client.
  23. //
  24. // If this returns without an error, the caller should use the connection
  25. // in place of the original connection.
  26. //
  27. // The second return value can contain credential specific data, such as
  28. // unix socket credentials or TLS information.
  29. //
  30. // While we currently only have implementations on the server-side, this
  31. // interface should be sufficient to implement similar handshakes on the
  32. // client-side.
  33. Handshake(ctx context.Context, conn net.Conn) (net.Conn, interface{}, error)
  34. }
  35. type handshakerFunc func(ctx context.Context, conn net.Conn) (net.Conn, interface{}, error)
  36. func (fn handshakerFunc) Handshake(ctx context.Context, conn net.Conn) (net.Conn, interface{}, error) {
  37. return fn(ctx, conn)
  38. }
  39. func noopHandshake(_ context.Context, conn net.Conn) (net.Conn, interface{}, error) {
  40. return conn, nil, nil
  41. }