我用https://github.com/gomodule/redigo美元买我的红线连接,

redis.Pool struct 如下所示

type Pool struct {
    // Dial is an application supplied function for creating and configuring a
    // connection.
    //
    // The connection returned from Dial must not be in a special state
    // (subscribed to pubsub channel, transaction started, ...).
    Dial func() (Conn, error)

    // DialContext is an application supplied function for creating and configuring a
    // connection with the given context.
    //
    // The connection returned from Dial must not be in a special state
    // (subscribed to pubsub channel, transaction started, ...).
    DialContext func(ctx context.Context) (Conn, error)

    // TestOnBorrow is an optional application supplied function for checking
    // the health of an idle connection before the connection is used again by
    // the application. Argument t is the time that the connection was returned
    // to the pool. If the function returns an error, then the connection is
    // closed.
    TestOnBorrow func(c Conn, t time.Time) error

    // Maximum number of idle connections in the pool.
    MaxIdle int

    // Maximum number of connections allocated by the pool at a given time.
    // When zero, there is no limit on the number of connections in the pool.
    MaxActive int

    // Close connections after remaining idle for this duration. If the value
    // is zero, then idle connections are not closed. Applications should set
    // the timeout to a value less than the server's timeout.
    IdleTimeout time.Duration

    // If Wait is true and the pool is at the MaxActive limit, then Get() waits
    // for a connection to be returned to the pool before returning.
    Wait bool

    // Close connections older than this duration. If the value is zero, then
    // the pool does not close connections based on age.
    MaxConnLifetime time.Duration

    mu           sync.Mutex    // mu protects the following fields
    closed       bool          // set to true when the pool is closed.
    active       int           // the number of open connections in the pool
    initOnce     sync.Once     // the init ch once func
    ch           chan struct{} // limits open connections when p.Wait is true
    idle         idleList      // idle connections
    waitCount    int64         // total number of connections waited for.
    waitDuration time.Duration // total time waited for new connections.
}

pool.Get函数从池中获取连接,如果Wait标志被打开,则它等待连接可用

有没有办法设置这个等待期的超时时间?

推荐答案

GetContext与可取消上下文一起使用.

ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
conn, err := pool.GetContext(ctx)

Go相关问答推荐

Go Net/http路由

我不能让GIO画一个按钮

GoLang:无法发送带有附件的邮箱

如何使用中间件更改http请求的响应代码?

关于如何使用 Service Weaver 设置多个不同侦听器的问题

以编程方式取消 pyspark dataproc 批处理作业(job)

在 Cloud Run 中找不到默认凭据

regex.ReplaceAll 但如果替换则添加相同数量的字符

SSH 代理,数据包长度错误

将 big.Int 转换为 [2]int64,反之亦然和二进制补码

处理程序后访问 HTTP 请求上下文

如何在 golang 中同时加载 .env 文件和 os 环境变量

gorm 获取列名

从golang中的url加载图像

Golang SSH客户端错误无法验证,try 的方法[无公钥],没有支持的方法

递归数据 struct 解组在 Go Lang Protobuf 中给出错误无法解析无效的线格式数据

从 Go struct 中提取标签作为 reflect.Value

无法识别同步错误.使用一次

如何扩充 ResponseWriter 的 Header() 返回的 map

在 go (1.18) 的泛型上实现多态的最佳方法是什么?