etcd err: context deadline exceeded


When I use shell it returns ok,
but when I use golang it returns err: context deadline exceeded.


package main

import (
func main()  {
    cli,err := clientv3.New(clientv3.Config{
        Endpoints: []string{""},
        DialTimeout: 5*time.Second,
    if err != nil{
    fmt.Println("connect etcd success!")
    defer cli.Close()

    ctx,cancel := context.WithTimeout(context.Background(),time.Second*5)
    _,err = cli.Put(ctx,"mac","leave")
    defer  cancel()
    if err !=nil{
        fmt.Println("put etcd err:",err.Error())
connect etcd success!
{"level":"warn","ts":"2021-02-08T17:10:46.490+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-5b14f6c4-2395-4a15-9ebb-cadf5985fb06/","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = latest balancer error: all SubConns are in TransientFailure, latest connection error: connection error: desc = \"transport: Error while dialing dial tcp connect: connection refused\""}
put etcd err: context deadline exceeded


Creation of client does not guarantee that the connection is established.
It might get delayed till the first request that actually fails.

Please make sure that your server is reachable.

Answered By – Piotr Tabor

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.