zrok/util/proxy.go

33 lines
627 B
Go
Raw Normal View History

2022-07-21 22:01:39 +02:00
package util
import (
"net/http"
"net/http/httputil"
2022-08-09 19:57:45 +02:00
"sync/atomic"
2022-07-21 22:01:39 +02:00
)
2024-07-22 18:34:42 +02:00
type requestsWrapper struct {
2022-08-09 19:57:45 +02:00
proxy *httputil.ReverseProxy
requests int32
2022-07-21 22:01:39 +02:00
}
2024-07-22 18:34:42 +02:00
func NewRequestsWrapper(proxy *httputil.ReverseProxy) *requestsWrapper {
handler := &requestsWrapper{proxy: proxy}
2022-08-09 19:57:45 +02:00
director := proxy.Director
proxy.Director = func(req *http.Request) {
atomic.AddInt32(&handler.requests, 1)
director(req)
}
return handler
}
2024-07-22 18:34:42 +02:00
func (self *requestsWrapper) Requests() int32 {
2022-08-09 19:57:45 +02:00
return atomic.LoadInt32(&self.requests)
2022-07-21 22:01:39 +02:00
}
2024-07-22 18:34:42 +02:00
func (self *requestsWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2022-07-21 22:01:39 +02:00
self.proxy.ServeHTTP(w, r)
}