fasthttp-reverse-proxy
fasthttp-reverse-proxy copied to clipboard
reverse http / websocket proxy based on fasthttp
fasthttp-reverse-proxy
reverse http proxy handler based on fasthttp.
Features
-
[x]
HTTP
reverse proxy based fasthttp- [x] it's faster than golang standard
httputil.ReverseProxy
library. - [x] implemented by
fasthttp.HostClient
- [x] support balance distribute based
rounddobin
- [x]
HostClient
object pool with an overlay of fasthttp connection pool.
- [x] it's faster than golang standard
- [x]
WebSocket
reverse proxy.
Get started
HTTP (with balancer option)
var (
proxyServer = proxy.NewReverseProxy("localhost:8080")
// use with balancer
// weights = map[string]proxy.Weight{
// "localhost:8080": 20,
// "localhost:8081": 30,
// "localhost:8082": 50,
// }
// proxyServer = proxy.NewReverseProxy("", proxy.WithBalancer(weights))
)
// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
// all proxy to localhost
proxyServer.ServeHTTP(ctx)
}
func main() {
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
log.Fatal(err)
}
}
Websocket
var (
proxyServer *proxy.WSReverseProxy
once sync.Once
)
// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
once.Do(func() {
var err error
proxyServer, err = proxy.NewWSReverseProxyWith(
proxy.WithURL_OptionWS("ws://localhost:8080/echo"),
)
if err != nil {
panic(err)
}
})
switch string(ctx.Path()) {
case "/echo":
proxyServer.ServeHTTP(ctx)
case "/":
fasthttp.ServeFileUncompressed(ctx, "./index.html")
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
func main() {
log.Println("serving on: 8081")
if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
log.Fatal(err)
}
}
Usages
- HTTP reverse proxy
- HTTP reverse proxy with object pool
- Websocket reverse proxy
Contrast
- HTTP benchmark
- Websocket benchmark
References
Thanks
