trpc.group/trpc-go/trpc-go@v1.0.3/transport/server_transport_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  	"runtime"
    18  	"time"
    19  )
    20  
    21  const (
    22  	defaultRecvMsgChannelSize      = 100
    23  	defaultSendMsgChannelSize      = 100
    24  	defaultRecvUDPPacketBufferSize = 65536
    25  	defaultIdleTimeout             = time.Minute
    26  )
    27  
    28  // ServerTransportOptions is options of the server transport.
    29  type ServerTransportOptions struct {
    30  	RecvMsgChannelSize      int
    31  	SendMsgChannelSize      int
    32  	RecvUDPPacketBufferSize int
    33  	RecvUDPRawSocketBufSize int
    34  	IdleTimeout             time.Duration
    35  	KeepAlivePeriod         time.Duration
    36  	ReusePort               bool
    37  }
    38  
    39  // ServerTransportOption modifies the ServerTransportOptions.
    40  type ServerTransportOption func(*ServerTransportOptions)
    41  
    42  // WithRecvMsgChannelSize returns a ServerTransportOption which sets the size of receive buf of
    43  // ServerTransport TCP.
    44  func WithRecvMsgChannelSize(size int) ServerTransportOption {
    45  	return func(options *ServerTransportOptions) {
    46  		options.RecvMsgChannelSize = size
    47  	}
    48  }
    49  
    50  // WithReusePort returns a ServerTransportOption which enable reuse port or not.
    51  func WithReusePort(reuse bool) ServerTransportOption {
    52  	return func(options *ServerTransportOptions) {
    53  		options.ReusePort = reuse
    54  		if runtime.GOOS == "windows" {
    55  			options.ReusePort = false
    56  		}
    57  	}
    58  }
    59  
    60  // WithSendMsgChannelSize returns a ServerTransportOption which sets the size of sendCh of
    61  // ServerTransport TCP.
    62  func WithSendMsgChannelSize(size int) ServerTransportOption {
    63  	return func(options *ServerTransportOptions) {
    64  		options.SendMsgChannelSize = size
    65  	}
    66  }
    67  
    68  // WithRecvUDPPacketBufferSize returns a ServerTransportOption which sets the pre-allocated buffer
    69  // size of ServerTransport UDP.
    70  func WithRecvUDPPacketBufferSize(size int) ServerTransportOption {
    71  	return func(options *ServerTransportOptions) {
    72  		options.RecvUDPPacketBufferSize = size
    73  	}
    74  }
    75  
    76  // WithRecvUDPRawSocketBufSize returns a ServerTransportOption which sets the size of the operating
    77  // system's receive buffer associated with the UDP connection.
    78  func WithRecvUDPRawSocketBufSize(size int) ServerTransportOption {
    79  	return func(options *ServerTransportOptions) {
    80  		options.RecvUDPRawSocketBufSize = size
    81  	}
    82  }
    83  
    84  // WithIdleTimeout returns a ServerTransportOption which sets the server connection idle timeout.
    85  func WithIdleTimeout(timeout time.Duration) ServerTransportOption {
    86  	return func(options *ServerTransportOptions) {
    87  		options.IdleTimeout = timeout
    88  	}
    89  }
    90  
    91  // WithKeepAlivePeriod returns a ServerTransportOption which sets the period to keep TCP connection
    92  // alive.
    93  // It's not available for TLS, since TLS neither use net.TCPConn nor net.Conn.
    94  func WithKeepAlivePeriod(d time.Duration) ServerTransportOption {
    95  	return func(options *ServerTransportOptions) {
    96  		options.KeepAlivePeriod = d
    97  	}
    98  }
    99  
   100  func defaultServerTransportOptions() *ServerTransportOptions {
   101  	return &ServerTransportOptions{
   102  		RecvMsgChannelSize:      defaultRecvMsgChannelSize,
   103  		SendMsgChannelSize:      defaultSendMsgChannelSize,
   104  		RecvUDPPacketBufferSize: defaultRecvUDPPacketBufferSize,
   105  	}
   106  }