dev-resources.site
for different kinds of informations.
Golang Context Cancelled On Goroutine
Published at
3/26/2021
Categories
notes
go
context
goroutine
Author
clavinjune
Author
10 person written this
clavinjune
open
Golang’s request context is automatically be done when passed on goroutine, and its parents goroutine is already done.
package main
import (
"context"
"log"
"net/http"
"time"
)
func foo(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx,
http.MethodGet, "https://google.com", nil)
_, err := http.DefaultClient.Do(req)
log.Println(err) // Get "https://google.com": context canceled
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
go foo(r.Context())
}) // context will be done when it reaches here
http.ListenAndServe(":8888", nil)
}
goroutine Article's
17 articles in total
🚀 Demystifying Golang Concurrency: Channels and Select🚀
read article
Goroutines and Channels: Concurrency Patterns in Go
read article
Golang: Como a observabilidade e profiling revelaram um throttling quase indetectável
read article
Golang Concurrency: A Fun and Fast Ride
read article
Non-blocking sequential processing in Go using infinite (unbounded) buffered channel
read article
Goroutine Race Condition
read article
How to limit the number of simultaneously running goroutines and wait for their completion
read article
What is a Goroutine ? Find the right way to implement goroutine 🔥
read article
How to implement Concurrency and Parallelism in Go? 🔥
read article
Golang Context Cancelled On Goroutine
currently reading
Concurrency trong Go: Tạo goroutine (phần 2)
read article
Concurrency trong Go: Tạo goroutine
read article
Concurrency trong Go: Tổng quan cơ chế hoạt động
read article
why to use sync.WaitGroup in golang?
read article
Releasing a high-performance goroutine pool in Go
read article
Go Concurrency pipelines
read article
My stupid mistake about goroutine/defer
read article
Featured ones: