dongrui6787 2018-03-16 12:37
浏览 39

使用nil上下文,但不要惊慌并挂在那里。 这怎么可能呢?

type Etcd struct {
    Next       plugin.Handler
    Fall       fall.F
    Zones      []string
    PathPrefix string
    Upstream   upstream.Upstream // Proxy for looking up names during the resolution process
    Client     etcdc.KeysAPI
    Ctx        context.Context
    Stubmap    *map[string]proxy.Proxy // list of proxies for stub resolving.

    endpoints []string // Stored here as well, to aid in testing.
}


func (e *Etcd) get(path string, recursive bool) (*etcdc.Response, error) {
    if e.Ctx == nil {
            // run here
            log.Printf("e.Ctx is nil")
    }
    // hang here but not panic.
    ctx, cancel := context.WithTimeout(e.Ctx, etcdTimeout)
    defer cancel()
    r, err := e.Client.Get(ctx, path, &etcdc.GetOptions{Sort: false, Recursive: recursive})
    if err != nil {
            return nil, err
    }
    return r, nil
}

source code link: https://github.com/coredns/coredns/blob/v1.0.6/plugin/etcd/etcd.go#L87

I used this etcd package in my own project, and init it, but not initialize filed Ctx, and I found that the process hang at this code:

ctx, cancel := context.WithTimeout(e.Ctx, etcdTimeout)

how could this happen with e.Ctx is nil and context.WithTimeout() do not panic ?

Anyone's suggestion is helpful.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序