Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post Context deadline exceeded (Client.Timeout exceeded while awaiting headers)

I am running a simple query using Clickhouse HTTP Interface

package main

import (
"fmt"
"net/http"
"time"
)

func main() {

    url := "http://localhost:8123" + "?query=select%201"
    req, _ := http.NewRequest("Post", url, nil)

    // set headers
    req.Header.Set("X-ClickHouse-User", "user")      //user
    req.Header.Set("X-ClickHouse-Key", "password") //password

    client := &http.Client{
        Timeout: 5 * time.Second,
    }
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("Success")
    }
}

It is giving me this error Post "http://localhost:8123?query=select%201": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

Note: If I use insert query instead of this select query it will give me the same error but also will insert the data correctly.

Any help would be appreciated.

Thanks

like image 913
Zunnurain Badar Avatar asked Apr 09 '26 13:04

Zunnurain Badar


1 Answers

I did a quick sample here using your code, and as I suspected, locally this error only happens when the server times out. Maybe postman is doing something that hides this behavior.

Also, don't forget that the http.Client timeout is for the entire request-response cycle that is constituted up of Dialer, TLS Handshake, Request Header, Request Body, Response Header and Response Body timeouts.

Please take a look in the POC below and change the timeout() for different values to see the success (4- sec) or fail (5+ sec).

package main

import (
    "fmt"
    "log"
    "net/http"
    "time"
)


func timeout(w http.ResponseWriter, req *http.Request) {
    time.Sleep(5 * time.Second)
}

func server() {
    go func() {
        http.HandleFunc("/", timeout)
        log.Fatal(http.ListenAndServe(":8123", nil))
    }()
}

func main() {
    server()

        url := "http://localhost:8123" + "?query=select%201"
    req, _ := http.NewRequest("Post", url, nil)

    // set headers
    req.Header.Set("X-ClickHouse-User", "user")      //user
    req.Header.Set("X-ClickHouse-Key", "password") //password

    client := &http.Client{
        Timeout: 5 * time.Second,
    }
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("Success", resp)
    }
}

you can also try to make use of specific http.Client timeouts as for example:

c := &http.Client{
    Transport: &http.Transport{
        Dial: (&net.Dialer{
                Timeout:   30 * time.Second,
                KeepAlive: 30 * time.Second,
        }).Dial,
        TLSHandshakeTimeout:   10 * time.Second,
        ResponseHeaderTimeout: 10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    }
}

Some references that may help you: https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/ https://itnext.io/http-request-timeouts-in-go-for-beginners-fe6445137c90 https://gosamples.dev/http-client-timeout/

like image 82
Marcel Kohls Avatar answered Apr 11 '26 04:04

Marcel Kohls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!