forked from yeqown/fasthttp-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
41 lines (34 loc) · 845 Bytes
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"log"
"github.com/valyala/fasthttp"
proxy "github.com/smantriplw/fasthttp-reverse-proxy/v2"
)
var (
pool proxy.Pool
)
// ProxyPoolHandler ...
func ProxyPoolHandler(ctx *fasthttp.RequestCtx) {
proxyServer, err := pool.Get("localhost:9090")
if err != nil {
log.Println("ProxyPoolHandler got an error: ", err)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
return
}
defer pool.Put(proxyServer)
proxyServer.ServeHTTP(ctx)
}
func factory(hostAddr string) (*proxy.ReverseProxy, error) {
return proxy.NewReverseProxyWith(proxy.WithAddress(hostAddr))
}
func main() {
initialCap, maxCap := 100, 1000
var err error
pool, err = proxy.NewChanPool(initialCap, maxCap, factory)
if err != nil {
panic(err)
}
if err = fasthttp.ListenAndServe(":8083", ProxyPoolHandler); err != nil {
panic(err)
}
}