github.com/prebid/prebid-server/v2@v2.18.0/router/aspects/request_timeout_handler.go (about) 1 package aspects 2 3 import ( 4 "net/http" 5 "strconv" 6 "time" 7 8 "github.com/julienschmidt/httprouter" 9 "github.com/prebid/prebid-server/v2/config" 10 "github.com/prebid/prebid-server/v2/metrics" 11 ) 12 13 func QueuedRequestTimeout(f httprouter.Handle, reqTimeoutHeaders config.RequestTimeoutHeaders, metricsEngine metrics.MetricsEngine, requestType metrics.RequestType) httprouter.Handle { 14 15 return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) { 16 17 reqTimeInQueue := r.Header.Get(reqTimeoutHeaders.RequestTimeInQueue) 18 reqTimeout := r.Header.Get(reqTimeoutHeaders.RequestTimeoutInQueue) 19 20 //If request timeout headers are not specified - process request as usual 21 if reqTimeInQueue == "" || reqTimeout == "" { 22 f(w, r, params) 23 return 24 } 25 26 reqTimeFloat, reqTimeFloatErr := strconv.ParseFloat(reqTimeInQueue, 64) 27 reqTimeoutFloat, reqTimeoutFloatErr := strconv.ParseFloat(reqTimeout, 64) 28 29 //Return HTTP 500 if request timeout headers are incorrect (wrong format) 30 if reqTimeFloatErr != nil || reqTimeoutFloatErr != nil { 31 w.WriteHeader(http.StatusInternalServerError) 32 w.Write([]byte("Request timeout headers are incorrect (wrong format)")) 33 return 34 } 35 36 reqTimeDuration := time.Duration(reqTimeFloat * float64(time.Second)) 37 38 //Return HTTP 408 if requests stays too long in queue 39 if reqTimeFloat >= reqTimeoutFloat { 40 w.WriteHeader(http.StatusRequestTimeout) 41 w.Write([]byte("Queued request processing time exceeded maximum")) 42 metricsEngine.RecordRequestQueueTime(false, requestType, reqTimeDuration) 43 return 44 } 45 46 metricsEngine.RecordRequestQueueTime(true, requestType, reqTimeDuration) 47 f(w, r, params) 48 } 49 50 }