trpc.group/trpc-go/trpc-go@v1.0.3/pool/multiplexed/pool_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 multiplexed
    15  
    16  import "time"
    17  
    18  // PoolOptions represents some settings for the connection pool.
    19  type PoolOptions struct {
    20  	connectNumberPerHost int           // Set the number of connections per address.
    21  	sendQueueSize        int           // Set the length of each Connection send queue.
    22  	dropFull             bool          // Whether the queue is full or not.
    23  	dialTimeout          time.Duration // Connection timeout, default 1s.
    24  	maxVirConnsPerConn   int           // Max number of virtual connections per real connection, 0 means no limit.
    25  	maxIdleConnsPerHost  int           // Set the maximum number of idle connections for each peer ip:port.
    26  }
    27  
    28  // PoolOption is the Options helper.
    29  type PoolOption func(*PoolOptions)
    30  
    31  // WithConnectNumber returns an Option which sets the number of connections for each peer in the connection pool.
    32  func WithConnectNumber(number int) PoolOption {
    33  	return func(opts *PoolOptions) {
    34  		opts.connectNumberPerHost = number
    35  	}
    36  }
    37  
    38  // WithQueueSize returns an Option which sets the length of each Connection sending queue in the connection pool.
    39  func WithQueueSize(n int) PoolOption {
    40  	return func(opts *PoolOptions) {
    41  		opts.sendQueueSize = n
    42  	}
    43  }
    44  
    45  // WithDropFull returns an Option which sets whether to drop the request when the queue is full.
    46  func WithDropFull(drop bool) PoolOption {
    47  	return func(opts *PoolOptions) {
    48  		opts.dropFull = drop
    49  	}
    50  }
    51  
    52  // WithDialTimeout returns an Option which sets the connection timeout.
    53  func WithDialTimeout(d time.Duration) PoolOption {
    54  	return func(opts *PoolOptions) {
    55  		opts.dialTimeout = d
    56  	}
    57  }
    58  
    59  // WithMaxVirConnsPerConn returns an Option which sets the maximum number of virtual
    60  // connections per real connection, 0 means no limit.
    61  func WithMaxVirConnsPerConn(n int) PoolOption {
    62  	return func(opts *PoolOptions) {
    63  		opts.maxVirConnsPerConn = n
    64  	}
    65  }
    66  
    67  // WithMaxIdleConnsPerHost returns an Option which sets the maximum number of idle connections
    68  // for each peer ip:port, this value should not be less than ConnectNumber,
    69  // This option is usually used with MaxVirConnsPerConn in streaming scenarios
    70  // to dynamically adjust the number of connections,
    71  // This option takes effect only when MaxVirConnsPerConn is set, 0 means no limit.
    72  func WithMaxIdleConnsPerHost(n int) PoolOption {
    73  	return func(opts *PoolOptions) {
    74  		opts.maxIdleConnsPerHost = n
    75  	}
    76  }