Golang : Delay or limit HTTP requests example
Problem:
For some technical limitation reason, you want to delay or slow down HTTP requests to one of your machines in order not to overwhelm the CPU. How to do that?
Solution:
Introduce a delay to the HTTP handler or prioritise the request with job queues. In this code example that follows, we will use the simplest delay mechanism for delaying HTTP requests.
Here you go!
package main
import (
"log"
"net/http"
"time"
)
// some global variables
var (
delayer <-chan time.Time
counter int
seconds time.Duration
)
func delayedHelloWorld(w http.ResponseWriter, r *http.Request) {
<-delayer
counter++
log.Printf("Processing request #%d - delayed HTTP request by %d seconds", counter, seconds)
w.Write([]byte("Hello, processed your request."))
}
func HelloWorld(w http.ResponseWriter, r *http.Request) {
<-delayer
counter++
log.Printf("Processing request #%d - delayed HTTP request by %d seconds", counter, seconds)
w.Write([]byte("Hello, processed your request."))
}
func main() {
counter = 0
seconds = 3
delayer = time.Tick(seconds * time.Second)
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(delayedHelloWorld)) // for Handle() method
mux.HandleFunc("/helloworld", HelloWorld) // for HandleFunc() method
http.ListenAndServe(":8080", mux)
}
Run the code and see the log output on the terminal.
Next, point your browsers to http://localhost:8080
and make couple of requests by clicking on the refresh button(or press F5 button repeatedly). You should see that all the requests are delayed by 3 seconds.
2016/10/17 13:06:57 Processing request #16 - delayed HTTP request by 3 seconds
2016/10/17 13:07:00 Processing request #17 - delayed HTTP request by 3 seconds
2016/10/17 13:07:03 Processing request #18 - delayed HTTP request by 3 seconds
2016/10/17 13:07:06 Processing request #19 - delayed HTTP request by 3 seconds
2016/10/17 13:07:09 Processing request #20 - delayed HTTP request by 3 seconds
NOTES:
If you're looking for job queuing example, see https://gist.github.com/harlow/dbcd639cf8d396a2ab73
Apart from that, you can also choose to filter requests made by legit Go client or HTTP POST or GET.
Happy coding!
References:
https://www.socketloop.com/tutorials/golang-web-routing-multiplex-example
See also : Golang : Execute function at intervals or after some delay
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10k Golang : cannot assign type int to value (type uint8) in range error
+11.8k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+8.1k Your page has meta tags in the body instead of the head
+9.7k Golang : Translate language with language package example
+5.8k Fontello : How to load and use fonts?
+8.9k Golang : Gonum standard normal random numbers example
+10.9k Golang : Calculate Relative Strength Index(RSI) example
+12.9k Golang : How to get a user home directory path?
+13.4k Golang : Activate web camera and broadcast out base64 encoded images
+5.2k How to check with curl if my website or the asset is gzipped ?
+5.5k Unix/Linux : Get reboot history or check when was the last reboot date
+11.3k Golang : Change date format to yyyy-mm-dd