Skip to content

Commit

Permalink
fix: Remove 50ms request penalty
Browse files Browse the repository at this point in the history
Each request has to wait for the 50ms ticker to tick before trying to
forward a request.

Now, attempt to forward first. If the request fails, then go ahead and
setup the tick + timeout handler.
  • Loading branch information
PatKoperwas committed Mar 1, 2018
1 parent d4d93f2 commit 87fd8a2
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions tychus/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (p *proxy) start() error {
func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.requests <- true

if ok := p.forward(w, r); ok {
return
}

timeout := time.After(time.Second * time.Duration(p.config.Timeout))
tick := time.Tick(50 * time.Millisecond)

Expand All @@ -77,18 +81,7 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for {
select {
case <-tick:
if p.mode == mode_errored {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(p.errorStr))
return
}

writer := &proxyWriter{res: w}
p.revproxy.ServeHTTP(writer, r)

// If the request is "successful" - as in the server responded in
// some way, return the response to the client.
if writer.status != http.StatusBadGateway {
if ok := p.forward(w, r); ok {
return
}

Expand All @@ -105,6 +98,21 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (p *proxy) forward(w http.ResponseWriter, r *http.Request) bool {
if p.mode == mode_errored {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(p.errorStr))
return true
}

writer := &proxyWriter{res: w}
p.revproxy.ServeHTTP(writer, r)

// If the request is "successful" - as in the server responded in
// some way, return the response to the client.
return writer.status != http.StatusBadGateway
}

func (p *proxy) serve() {
p.config.Logger.Debug("Proxy: Serving")
p.mode = mode_serving
Expand Down

0 comments on commit 87fd8a2

Please sign in to comment.