Hi,
I am currently using Request context as the “parent” context for my database query context. However, this causes context canceled
error in logs. If I just use a Background
context as the “parent” context then all works fine. Is this expected behaviour or am I missing something?
Thanks
CURRENT (broken)
// ctx is `*http.Request.Context` which come from the handler.
func Insert(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 3 * time.Millisecond)
defer cancel()
qry := `INSERT INTO users (name) VALUES (?)`
res, err := r.database.ExecContext(ctx, qry, "hello")
// ....
}
FINE
func Insert() error {
ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Millisecond)
defer cancel()
qry := `INSERT INTO users (name) VALUES (?)`
res, err := r.database.ExecContext(ctx, qry, "hello")
// ....
}