dialer.go 803 B

12345678910111213141516171819202122232425262728293031323334
  1. package dialer
  2. import (
  3. "context"
  4. "net"
  5. )
  6. // Dialers have the network locked in.
  7. type T interface {
  8. Dial(_ context.Context, addr string) (net.Conn, error)
  9. DialerNetwork() string
  10. }
  11. // An interface to ease wrapping dialers that explicitly include a network parameter.
  12. type WithContext interface {
  13. DialContext(ctx context.Context, network, addr string) (net.Conn, error)
  14. }
  15. // Used by wrappers of standard library network types.
  16. var Default = &net.Dialer{}
  17. // Adapts a WithContext to the Dial interface in this package.
  18. type WithNetwork struct {
  19. Network string
  20. Dialer WithContext
  21. }
  22. func (me WithNetwork) DialerNetwork() string {
  23. return me.Network
  24. }
  25. func (me WithNetwork) Dial(ctx context.Context, addr string) (_ net.Conn, err error) {
  26. return me.Dialer.DialContext(ctx, me.Network, addr)
  27. }