Database query context canceled


When I send hundreds of concurrent requests to the route below I get so many context canceled error - even if I increate timeout to 100 seconds. As far as the database interaction goes, is this Go way of doing things or am I missing something here?

I know that context is cancelled when the response is returned but I am thinking if the query is still running, response is not returned yet so why is context canceled error? WithCancel() causes the case error.


func Handler(w http.ResponseWriter, r *http.Request) {
    err := Insert(r.Context())
    // handle the rest

func Insert(ctx context.Context) error {
	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
	defer cancel()

	if _, err := mysql.ExecContext(ctx, `MY QUERY`, "xyz"); err != nil {
		return err // context canceled

	return nil

// This is how I do the rest
// mysql, err := sql.Open("mysql", "user:pass@tcp(:3306)/client")
// if err != nil {
// 	log.Fatalln(err)
// }
// http.ListenAndServe(":8080", Handler)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.