From cf46dcf53f92355652046732b049d66f78170151 Mon Sep 17 00:00:00 2001 From: terorie Date: Mon, 30 Jul 2018 03:09:23 +0200 Subject: [PATCH] Minimal async HTTP --- common/httpasync.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 common/httpasync.go diff --git a/common/httpasync.go b/common/httpasync.go new file mode 100644 index 0000000..5dbf097 --- /dev/null +++ b/common/httpasync.go @@ -0,0 +1,35 @@ +package common + +import "net/http" + +type JobResult struct { + Res *http.Response + Err error + ReqData interface{} // job.data +} + +type job struct { + req *http.Request + c chan JobResult + data interface{} +} + +var jobs = make(chan job) + +func InitAsyncHTTP(nWorkers uint) { + for i := uint(0); i < nWorkers; i++ { + go asyncHTTPWorker() + } +} + +func DoAsyncHTTP(r *http.Request, c chan JobResult, data interface{}) { + jobs <- job{r, c, data} +} + +func asyncHTTPWorker() { + for { + job := <-jobs + res, err := Client.Do(job.req) + job.c <- JobResult{res, err, job.data} + } +}