ServeHTTP handles incoming HTTP requests and forwards them to the backend selected by the load balancer.
(w http.ResponseWriter, r *http.Request)
| 23 | // ServeHTTP handles incoming HTTP requests and forwards them to the backend |
| 24 | // selected by the load balancer. |
| 25 | func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 26 | // Select a healthy backend according to the load balancing strategy. |
| 27 | backend, err := p.balancer.Next(r) |
| 28 | if err != nil { |
| 29 | log.Printf("[Proxy] error selecting backend: %v", err) |
| 30 | http.Error(w, "service not available", http.StatusServiceUnavailable) |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | // Increase the active connection count, and ensure it is decremented |
| 35 | // when the request completes. |
| 36 | backend.IncrementActiveConns() |
| 37 | defer backend.DecrementActiveConns() |
| 38 | |
| 39 | backend.proxy.ServeHTTP(w, r) |
| 40 | } |