trpc.group/trpc-go/trpc-go@v1.0.3/pool/connpool/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 connpool
    15  
    16  import (
    17  	"time"
    18  )
    19  
    20  // Options indicates pool configuration.
    21  type Options struct {
    22  	MinIdle   int // Initialize the number of connections, ready for the next io.
    23  	MaxIdle   int // Maximum number of idle connections, 0 means no idle.
    24  	MaxActive int // Maximum number of active connections, 0 means no limit.
    25  	// Whether to wait when the maximum number of active connections is reached.
    26  	Wait               bool
    27  	IdleTimeout        time.Duration // idle connection timeout.
    28  	MaxConnLifetime    time.Duration // Maximum lifetime of the connection.
    29  	DialTimeout        time.Duration // Connection establishment timeout.
    30  	ForceClose         bool
    31  	Dial               DialFunc
    32  	Checker            HealthChecker
    33  	PushIdleConnToTail bool          // connection to ip will be push tail when ConnectionPool.put method is called
    34  	PoolIdleTimeout    time.Duration // ConnectionPool idle timeout
    35  }
    36  
    37  // Option is the Options helper.
    38  type Option func(*Options)
    39  
    40  // WithMinIdle returns an Option which sets the number of initialized connections.
    41  func WithMinIdle(n int) Option {
    42  	return func(o *Options) {
    43  		o.MinIdle = n
    44  	}
    45  }
    46  
    47  // WithMaxIdle returns an Option which sets the maximum number of idle connections.
    48  func WithMaxIdle(m int) Option {
    49  	return func(o *Options) {
    50  		o.MaxIdle = m
    51  	}
    52  }
    53  
    54  // WithMaxActive returns an Option which sets the maximum number of active connections.
    55  func WithMaxActive(s int) Option {
    56  	return func(o *Options) {
    57  		o.MaxActive = s
    58  	}
    59  }
    60  
    61  // WithWait returns an Option which sets whether to wait when the number of connections reaches the limit.
    62  func WithWait(w bool) Option {
    63  	return func(o *Options) {
    64  		o.Wait = w
    65  	}
    66  }
    67  
    68  // WithIdleTimeout returns an Option which sets the idle connection time, after which it may be closed.
    69  func WithIdleTimeout(t time.Duration) Option {
    70  	return func(o *Options) {
    71  		o.IdleTimeout = t
    72  	}
    73  }
    74  
    75  // WithMaxConnLifetime returns an Option which sets the maximum lifetime of
    76  // the connection, after which it may be closed.
    77  func WithMaxConnLifetime(t time.Duration) Option {
    78  	return func(o *Options) {
    79  		o.MaxConnLifetime = t
    80  	}
    81  }
    82  
    83  // WithDialTimeout returns an Option which sets the default timeout time for
    84  // the connection pool to establish a connection.
    85  func WithDialTimeout(t time.Duration) Option {
    86  	return func(o *Options) {
    87  		o.DialTimeout = t
    88  	}
    89  }
    90  
    91  // WithForceClose returns an Option which sets whether to force the connection to be closed.
    92  func WithForceClose(f bool) Option {
    93  	return func(o *Options) {
    94  		o.ForceClose = f
    95  	}
    96  }
    97  
    98  // WithDialFunc returns an Option which sets dial function.
    99  func WithDialFunc(d DialFunc) Option {
   100  	return func(o *Options) {
   101  		o.Dial = d
   102  	}
   103  }
   104  
   105  // WithHealthChecker returns an Option which sets health checker.
   106  func WithHealthChecker(c HealthChecker) Option {
   107  	return func(o *Options) {
   108  		o.Checker = c
   109  	}
   110  }
   111  
   112  // WithPushIdleConnToTail returns an Option which sets PushIdleConnToTail flag.
   113  func WithPushIdleConnToTail(c bool) Option {
   114  	return func(o *Options) {
   115  		o.PushIdleConnToTail = c
   116  	}
   117  }
   118  
   119  // WithPoolIdleTimeout returns an Option which sets pool idle timeout.
   120  // after the timeout, ConnectionPool resource may be cleaned up.
   121  func WithPoolIdleTimeout(t time.Duration) Option {
   122  	return func(o *Options) {
   123  		o.PoolIdleTimeout = t
   124  	}
   125  }