Bring back the queue.

This commit is contained in:
Joseph Burnett 2018-02-02 18:43:56 -05:00
parent 6c4c26e6fe
commit 520284f040
3 changed files with 52 additions and 4 deletions

View File

@ -50,11 +50,7 @@ http {
# to avoid a race condition between the two timeouts.
keepalive_timeout 650;
keepalive_requests 10000;
{{if .EnableQueue}}
upstream queue { server 127.0.0.1:8012; }
{{else}}
upstream app_server { keepalive 64; server 127.0.0.1:8080; }
{{end}}
geo $source_type {
default ext;

View File

@ -0,0 +1,4 @@
FROM ubuntu:latest
ADD queue /
ENTRYPOINT ["/queue"]
EXPOSE 8012

View File

@ -0,0 +1,48 @@
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
type request struct {
w http.ResponseWriter
r *http.Request
c chan struct{}
}
var requestChan = make(chan *request)
func init() { log.Println("Init queue container.") }
func consumer() {
target, err := url.Parse("http://127.0.0.1:8080")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
for {
r := <-requestChan
log.Println("Forwarding a request to the app container at ", time.Now().String())
proxy.ServeHTTP(r.w, r.r)
close(r.c)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
req := &request{
w: w,
r: r,
c: make(chan struct{}),
}
log.Println("Adding a request to the the request channel at ", time.Now().String())
requestChan <- req
<-req.c
}
func main() {
log.Println("Queue container is running")
go consumer()
http.HandleFunc("/", handler)
http.ListenAndServe(":8012", nil)
}