Wait for request to be handled before returning

This commit is contained in:
Mark Beamer Jr 2021-04-06 14:21:05 -04:00
parent bd13836897
commit b97595311f
No known key found for this signature in database
GPG key ID: 1C314FB89AD76973
2 changed files with 9 additions and 3 deletions

View file

@ -70,7 +70,11 @@ func (s *Server) Start(address string) error {
}
r := mux.NewRouter()
r.HandleFunc("/get/{hash}", func(w http.ResponseWriter, r *http.Request) {
enqueue(&blobRequest{request: r, reply: w})
waiter := stop.New()
enqueue(&blobRequest{request: r, reply: w, finished: waiter})
select {
case <-waiter.Ch():
}
})
r.HandleFunc("/has/{hash}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

View file

@ -11,6 +11,7 @@ import (
type blobRequest struct {
request *http.Request
reply http.ResponseWriter
finished *stop.Group
}
var getReqCh = make(chan *blobRequest)
@ -37,4 +38,5 @@ func enqueue(b *blobRequest) {
func process(server *Server, r *blobRequest) {
server.HandleGetBlob(r.reply, r.request)
r.finished.Done()
}