-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathproxy.go
More file actions
95 lines (78 loc) · 2.26 KB
/
proxy.go
File metadata and controls
95 lines (78 loc) · 2.26 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package proxy
import (
"context"
"fmt"
"net"
"net/http"
"sync/atomic"
"time"
"github.com/e2b-dev/infra/packages/shared/pkg/proxy/pool"
"github.com/e2b-dev/infra/packages/shared/pkg/proxy/tracking"
)
const (
maxClientConns = 16384 // Reasonably big number that is lower than the number of available ports.
idleTimeoutBufferUpstreamDownstream = 10
)
type Proxy struct {
http.Server
pool *pool.ProxyPool
currentServerConnsCounter atomic.Int64
}
type MaxConnectionAttempts int
const (
ClientProxyRetries = 1
SandboxProxyRetries = 5
)
func New(
port uint16,
maxConnectionAttempts MaxConnectionAttempts,
idleTimeout time.Duration,
getDestination func(r *http.Request) (*pool.Destination, error),
) *Proxy {
p := pool.New(
maxClientConns,
int(maxConnectionAttempts),
idleTimeout,
)
return &Proxy{
Server: http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadTimeout: 0,
WriteTimeout: 0,
// Downstream idle timeout (client facing) > upstream idle timeout (server facing)
// otherwise there's a chance for a race condition when the server closes and the client tries to use the connection
IdleTimeout: idleTimeout + idleTimeoutBufferUpstreamDownstream,
ReadHeaderTimeout: 0,
Handler: handler(p, getDestination),
},
pool: p,
}
}
// TotalPoolConnections returns the total number of connections that have been established across whole pool.
func (p *Proxy) TotalPoolConnections() uint64 {
return p.pool.TotalConnections()
}
// CurrentServerConnections returns the current number of connections that are alive across whole pool.
func (p *Proxy) CurrentServerConnections() int64 {
return p.currentServerConnsCounter.Load()
}
func (p *Proxy) CurrentPoolSize() int {
return p.pool.Size()
}
func (p *Proxy) CurrentPoolConnections() int64 {
return p.pool.CurrentConnections()
}
func (p *Proxy) RemoveFromPool(connectionKey string) error {
return p.pool.Close(connectionKey)
}
func (p *Proxy) ListenAndServe(ctx context.Context) error {
var lisCfg net.ListenConfig
l, err := lisCfg.Listen(ctx, "tcp", p.Addr)
if err != nil {
return err
}
return p.Serve(l)
}
func (p *Proxy) Serve(l net.Listener) error {
return p.Server.Serve(tracking.NewListener(l, &p.currentServerConnsCounter))
}