Skip to content

Instantly share code, notes, and snippets.

@kasvith
Created November 9, 2019 08:42
Show Gist options
  • Save kasvith/ed35e205c7e91fd72c9146809365130b to your computer and use it in GitHub Desktop.
Save kasvith/ed35e205c7e91fd72c9146809365130b to your computer and use it in GitHub Desktop.
// lb load balances the incoming request
func lb(w http.ResponseWriter, r *http.Request) {
attempts := GetAttemptsFromContext(r)
if attempts > 3 {
log.Printf("%s(%s) Max attempts reached, terminating\n", r.RemoteAddr, r.URL.Path)
http.Error(w, "Service not available", http.StatusServiceUnavailable)
return
}
peer := serverPool.GetNextPeer()
if peer != nil {
peer.ReverseProxy.ServeHTTP(w, r)
return
}
http.Error(w, "Service not available", http.StatusServiceUnavailable)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment