Last active
August 21, 2018 11:18
-
-
Save aarjan/2ac55201761ca61a69a2c58077602378 to your computer and use it in GitHub Desktop.
Simple concurrent worker model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
// JobQueue is a channel for sending/recieving jobs | |
var JobQueue = make(chan Job, 1000) | |
// Job represents a single instance of job to be done | |
type Job struct { | |
payload Payload | |
} | |
// Payload is an information to be transferred | |
type Payload struct { | |
Browser string `json:"browser"` | |
BrowserVersion int64 `json:"browser_version"` | |
City string `json:"city"` | |
CurrentURL string `json:"current_url"` | |
DistinctID string `json:"distinct_id"` | |
InitialReferrer string `json:"initial_referrer"` | |
InitialReferringDomain string `json:"initial_referring_domain"` | |
LibVersion string `json:"lib_version"` | |
MpCountryCode string `json:"mp_country_code"` | |
MpLib string `json:"mp_lib"` | |
Name string `json:"name"` | |
Os string `json:"os"` | |
Platform string `json:"platform"` | |
ScreenHeight int64 `json:"screen_height"` | |
ScreenWidth int64 `json:"screen_width"` | |
Timestamp int64 `json:"timestamp"` | |
} | |
var worker = make(chan chan Job, 1000) | |
var maxWorker = 100 | |
func main() { | |
for i := 0; i < maxWorker; i++ { | |
// this loop will run indefinitely until the channel is closed | |
// hence, mulitple worker would run concurrently | |
go func(i int) { | |
for range JobQueue { | |
time.Sleep(10 * time.Millisecond) | |
} | |
}(i) | |
} | |
log.Fatal(http.ListenAndServe(":8080", handleRequest())) | |
} | |
func handleRequest() http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
if r.URL.Path != "/event" { | |
http.NotFound(w, r) | |
return | |
} | |
p := Payload{} | |
buf, _ := ioutil.ReadAll(r.Body) | |
json.Unmarshal(buf, &p) | |
work := Job{p} | |
JobQueue <- work | |
w.WriteHeader(http.StatusOK) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment