trpc.group/trpc-go/trpc-go@v1.0.3/transport/client_roundtrip_options.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package transport
    15  
    16  import (
    17  	"time"
    18  
    19  	"trpc.group/trpc-go/trpc-go/codec"
    20  	"trpc.group/trpc-go/trpc-go/pool/connpool"
    21  	"trpc.group/trpc-go/trpc-go/pool/multiplexed"
    22  )
    23  
    24  // RoundTripOptions is the options for one roundtrip.
    25  type RoundTripOptions struct {
    26  	Address               string // IP:Port. Note: address has been resolved from naming service.
    27  	Password              string
    28  	Network               string // tcp/udp
    29  	LocalAddr             string // a random selected local address when accept a connection.
    30  	DialTimeout           time.Duration
    31  	Pool                  connpool.Pool // client connection pool
    32  	ReqType               RequestType   // SendAndRecv, SendOnly
    33  	FramerBuilder         codec.FramerBuilder
    34  	ConnectionMode        ConnectionMode
    35  	DisableConnectionPool bool // disable connection pool
    36  	EnableMultiplexed     bool // enable multiplexed
    37  	Multiplexed           multiplexed.Pool
    38  	Msg                   codec.Msg
    39  	Protocol              string // protocol type
    40  
    41  	CACertFile    string // CA certificate file
    42  	TLSCertFile   string // client certificate file
    43  	TLSKeyFile    string // client key file
    44  	TLSServerName string // the name when client verifies the server, default as HTTP hostname
    45  }
    46  
    47  // ConnectionMode is the connection mode, either Connected or NotConnected.
    48  type ConnectionMode bool
    49  
    50  // ConnectionMode of UDP.
    51  const (
    52  	Connected    = false // UDP which isolates packets from non-same path
    53  	NotConnected = true  // UDP which allows returning packets from non-same path
    54  )
    55  
    56  // RequestType is the client request type, such as SendAndRecv or SendOnly.
    57  type RequestType = codec.RequestType
    58  
    59  // Request types.
    60  const (
    61  	SendAndRecv RequestType = codec.SendAndRecv // send and receive
    62  	SendOnly    RequestType = codec.SendOnly    // send only
    63  )
    64  
    65  // RoundTripOption modifies the RoundTripOptions.
    66  type RoundTripOption func(*RoundTripOptions)
    67  
    68  // WithDialAddress returns a RoundTripOption which sets dial address.
    69  func WithDialAddress(address string) RoundTripOption {
    70  	return func(opts *RoundTripOptions) {
    71  		opts.Address = address
    72  	}
    73  }
    74  
    75  // WithDialPassword returns a RoundTripOption which sets dial password.
    76  func WithDialPassword(password string) RoundTripOption {
    77  	return func(opts *RoundTripOptions) {
    78  		opts.Password = password
    79  	}
    80  }
    81  
    82  // WithDialNetwork returns a RoundTripOption which sets dial network.
    83  func WithDialNetwork(network string) RoundTripOption {
    84  	return func(opts *RoundTripOptions) {
    85  		opts.Network = network
    86  	}
    87  }
    88  
    89  // WithDialPool returns a RoundTripOption which sets dial pool.
    90  func WithDialPool(pool connpool.Pool) RoundTripOption {
    91  	return func(opts *RoundTripOptions) {
    92  		opts.Pool = pool
    93  	}
    94  }
    95  
    96  // WithClientFramerBuilder returns a RoundTripOption which sets FramerBuilder.
    97  func WithClientFramerBuilder(builder codec.FramerBuilder) RoundTripOption {
    98  	return func(opts *RoundTripOptions) {
    99  		opts.FramerBuilder = builder
   100  	}
   101  }
   102  
   103  // WithReqType returns a RoundTripOption which sets request type.
   104  func WithReqType(reqType RequestType) RoundTripOption {
   105  	return func(opts *RoundTripOptions) {
   106  		opts.ReqType = reqType
   107  	}
   108  }
   109  
   110  // WithConnectionMode returns a RoundTripOption which sets UDP connection mode.
   111  func WithConnectionMode(connMode ConnectionMode) RoundTripOption {
   112  	return func(opts *RoundTripOptions) {
   113  		opts.ConnectionMode = connMode
   114  	}
   115  }
   116  
   117  // WithDialTLS returns a RoundTripOption which sets UDP TLS relatives.
   118  func WithDialTLS(certFile, keyFile, caFile, serverName string) RoundTripOption {
   119  	return func(opts *RoundTripOptions) {
   120  		opts.TLSCertFile = certFile
   121  		opts.TLSKeyFile = keyFile
   122  		opts.CACertFile = caFile
   123  		opts.TLSServerName = serverName
   124  	}
   125  }
   126  
   127  // WithDisableConnectionPool returns a RoundTripOption which disables connection pool.
   128  func WithDisableConnectionPool() RoundTripOption {
   129  	return func(opts *RoundTripOptions) {
   130  		opts.DisableConnectionPool = true
   131  	}
   132  }
   133  
   134  // WithMultiplexed returns a RoundTripOption which enables multiplexed.
   135  func WithMultiplexed(enable bool) RoundTripOption {
   136  	return func(opts *RoundTripOptions) {
   137  		opts.EnableMultiplexed = enable
   138  	}
   139  }
   140  
   141  // WithMultiplexedPool returns a RoundTripOption which sets multiplexed pool.
   142  // This function also enables multiplexed.
   143  func WithMultiplexedPool(p multiplexed.Pool) RoundTripOption {
   144  	return func(opts *RoundTripOptions) {
   145  		opts.EnableMultiplexed = true
   146  		opts.Multiplexed = p
   147  	}
   148  }
   149  
   150  // WithMsg returns a RoundTripOption which sets msg.
   151  func WithMsg(msg codec.Msg) RoundTripOption {
   152  	return func(opts *RoundTripOptions) {
   153  		opts.Msg = msg
   154  	}
   155  }
   156  
   157  // WithLocalAddr returns a RoundTripOption which sets local address.
   158  // Random selection by default when there are multiple NICs.
   159  func WithLocalAddr(addr string) RoundTripOption {
   160  	return func(o *RoundTripOptions) {
   161  		o.LocalAddr = addr
   162  	}
   163  }
   164  
   165  // WithDialTimeout returns a RoundTripOption which sets dial timeout.
   166  func WithDialTimeout(dur time.Duration) RoundTripOption {
   167  	return func(o *RoundTripOptions) {
   168  		o.DialTimeout = dur
   169  	}
   170  }
   171  
   172  // WithProtocol returns a RoundTripOption which sets protocol name, such as trpc.
   173  func WithProtocol(s string) RoundTripOption {
   174  	return func(o *RoundTripOptions) {
   175  		o.Protocol = s
   176  	}
   177  }