Last active
July 13, 2023 05:43
-
-
Save egonelbre/298a97af4c86aebc2141 to your computer and use it in GitHub Desktop.
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 | |
const MaxLength = 1 << 20 | |
var ( | |
addr = flag.String("listen", ":8000", "listen for requests") | |
numprocs = flag.Int("p", runtime.NumCPU(), "number of workers to start") | |
maxqueue = flag.Int("q", runtime.NumCPU()*2, "largest queue size") | |
jobs chan Job | |
) | |
func main() { | |
flag.Parse() | |
jobs = make(chan Job, *maxqueue) | |
for i := 0; i < *numprocs; i++ { | |
go Work(jobs) | |
} | |
http.ListenAndHandle(*addr, request) | |
} | |
type Job func() error | |
func Work(jobs chan Job) { | |
for job := range jobs { | |
if err := job(); err != nil { | |
log.Errorf("Error handling job: %s", err.Error()) | |
} | |
} | |
} | |
func request(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "POST" { | |
w.WriteHeader(http.StatusMethodNotAllowed) | |
return | |
} | |
var content = &PayloadCollection{} | |
err := json.NewDecoder(io.LimitReader(r.Body, MaxLength)).Decode(&content) | |
if err != nil { | |
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | |
w.WriteHeader(http.StatusBadRequest) | |
return | |
} | |
for _, payload := range content.Payloads { | |
jobs <- payload.UploadToS3 | |
} | |
w.WriteHeader(http.StatusOK) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment