Context Deadline Exceeded
is an error occurring in Go when a context of an HTTP request has a deadline or a timeout set, i.e., the time after which the request ought to abort. This error is returned if the time of a server response is bigger than the set timeout. Setting timeouts on requests is an efficient observe in a manufacturing surroundings to make sure that you at all times get a response (or error) in a finite time.
Context deadline exceeded
instance
package deal major
import (
"context"
"errors"
"log"
"internet/http"
"os"
"time"
)
func slowServer(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Second)
w.Write([]byte("Hi there world!"))
}
func name() error {
shopper := &http.Consumer{}
req, err := http.NewRequest(http.MethodGet, "http://localhost:8080", nil)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(req.Context(), 1*time.Second)
defer cancel()
req = req.WithContext(ctx)
_, err = shopper.Do(req)
return err
}
func major() {
// run gradual server
go func() {
http.HandleFunc("/", slowServer)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Deadly(err)
}
}()
time.Sleep(1 * time.Second) // look ahead to server to run
// name server
err := name()
if errors.Is(err, context.DeadlineExceeded) {
log.Println("ContextDeadlineExceeded: true")
}
if os.IsTimeout(err) {
log.Println("IsTimeoutError: true")
}
if err != nil {
log.Deadly(err)
}
}
Output:
2021/08/19 06:39:09 ContextDeadlineExceeded: true
2021/08/19 06:39:09 IsTimeoutError: true
2021/08/19 06:39:09 Get "http://localhost:8080": context deadline exceeded
exit standing 1
Within the instance above, we:
- run our gradual server, whose job is to return a response after 10 seconds
- create a brand new request within the
name()
operate, which will probably be despatched to the server - set a timeout of 1 second on this request, i.e., the worth of time after the ready for the server response is interrupted
- ship the request to the server
Since a timeout for this request is about to 1 second and the server responds after 10 seconds, the HTTP shopper returns an error.
Deal with Context deadline exceeded
error
An HTTP shopper returns the context.DeadlineExceeded
error when the set timeout is exceeded. This error can be dealt with with the extra basic os.IsTimeout()
operate that checks if the error is thought to report {that a} timeout occurred.
The timeout could be set not solely on the stage of a single HTTP request but additionally on the stage of your entire HTTP shopper. On this case, every request made by such a shopper has the identical timeout worth. See the instance of a name()
operate utilizing the shopper timeout choice:
func name() error {
shopper := &http.Consumer{Timeout: 1 * time.Second}
_, err := shopper.Get("http://localhost:8080")
return err
}
Output:
2021/08/19 07:35:14 IsTimeoutError: true
2021/08/19 07:35:14 Get "http://localhost:8080": context deadline exceeded (Consumer.Timeout exceeded whereas awaiting headers)
exit standing 1
On this case, we get the context deadline exceeded (Consumer.Timeout exceeded whereas awaiting headers)
error. Be aware that this isn’t an occasion of context.DeadlineExceeded
error.