etcd err: context deadline exceeded

Issue

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

Why?

package main

import (
    "context"
    "fmt"
    "go.etcd.io/etcd/clientv3"
    "time"
)
func main()  {
    cli,err := clientv3.New(clientv3.Config{
        Endpoints: []string{"http://192.168.11.34:2379"},
        DialTimeout: 5*time.Second,
    })
    if err != nil{
        panic(err)
    }
    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())
        return
    }
}
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/192.168.11.34:2379","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 192.168.11.34:2379: connect: connection refused\""}
put etcd err: context deadline exceeded

Solution

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.