-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.go
More file actions
326 lines (277 loc) · 8.25 KB
/
Copy pathproxy.go
File metadata and controls
326 lines (277 loc) · 8.25 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package dewy
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/linyows/dewy/logging"
"github.com/linyows/dewy/telemetry"
"go.opentelemetry.io/otel/attribute"
otelmetric "go.opentelemetry.io/otel/metric"
)
// tcpProxy manages a TCP proxy for a single port.
type tcpProxy struct {
proxyPort int
listener net.Listener
backends []tcpBackend
backendIndex uint64 // Atomic counter for round-robin
mu sync.RWMutex
done chan struct{}
logger *logging.Logger
idleTimeout time.Duration // 0 means no timeout
metrics *telemetry.Metrics
}
// tcpBackend represents a backend server.
type tcpBackend struct {
host string
port int
}
// startProxy starts TCP proxies for all configured port mappings.
func (d *Dewy) startProxy(ctx context.Context) error {
if len(d.config.Container.PortMappings) == 0 {
return fmt.Errorf("no port mappings configured for proxy")
}
d.proxyMutex.Lock()
d.tcpProxies = make(map[int]*tcpProxy)
d.proxyMutex.Unlock()
// Start a TCP proxy for each port mapping
var metrics *telemetry.Metrics
if d.telemetry != nil && d.telemetry.Enabled() {
metrics = d.telemetry.Metrics()
}
for _, mapping := range d.config.Container.PortMappings {
proxy, err := newTCPProxy(mapping.ProxyPort, d.logger, d.config.Container.ProxyIdleTimeout, metrics)
if err != nil {
// Clean up already started proxies
if stopErr := d.stopProxy(ctx); stopErr != nil {
d.logger.Error("Failed to stop proxies during cleanup", slog.String("error", stopErr.Error()))
}
return fmt.Errorf("failed to start proxy on port %d: %w", mapping.ProxyPort, err)
}
d.proxyMutex.Lock()
d.tcpProxies[mapping.ProxyPort] = proxy
d.proxyMutex.Unlock()
d.logger.Info("TCP proxy started",
slog.Int("proxy_port", mapping.ProxyPort))
}
d.logger.Info("All TCP proxies started successfully",
slog.Int("count", len(d.config.Container.PortMappings)))
return nil
}
// newTCPProxy creates and starts a new TCP proxy on the specified port.
func newTCPProxy(port int, logger *logging.Logger, idleTimeout time.Duration, metrics *telemetry.Metrics) (*tcpProxy, error) {
addr := fmt.Sprintf(":%d", port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed to listen on %s: %w", addr, err)
}
proxy := &tcpProxy{
proxyPort: port,
listener: listener,
backends: make([]tcpBackend, 0),
done: make(chan struct{}),
logger: logger,
idleTimeout: idleTimeout,
metrics: metrics,
}
go proxy.acceptLoop()
return proxy, nil
}
// acceptLoop accepts incoming connections and proxies them to backends.
func (p *tcpProxy) acceptLoop() {
for {
select {
case <-p.done:
return
default:
}
conn, err := p.listener.Accept()
if err != nil {
select {
case <-p.done:
return
default:
p.logger.Debug("Accept error",
slog.Int("proxy_port", p.proxyPort),
slog.String("error", err.Error()))
continue
}
}
go p.handleConnection(conn)
}
}
// handleConnection proxies a single connection to a backend.
func (p *tcpProxy) handleConnection(clientConn net.Conn) {
defer clientConn.Close()
ctx := context.Background()
portAttr := otelmetric.WithAttributes(attribute.Int("proxy_port", p.proxyPort))
// Record connection accepted metrics immediately
if p.metrics != nil {
p.metrics.ProxyConnectionsTotal.Add(ctx, 1, portAttr)
p.metrics.ProxyActiveConnections.Add(ctx, 1, portAttr)
}
connStart := time.Now()
defer func() {
if p.metrics != nil {
p.metrics.ProxyActiveConnections.Add(ctx, -1, portAttr)
p.metrics.ProxyConnectionDuration.Record(ctx, time.Since(connStart).Seconds(), portAttr)
}
}()
// Get backend using round-robin
backend, ok := p.getNextBackend()
if !ok {
p.logger.Debug("No backend available",
slog.Int("proxy_port", p.proxyPort))
if p.metrics != nil {
p.metrics.ProxyErrorsTotal.Add(ctx, 1, portAttr)
}
return
}
// Connect to backend with latency measurement
backendAddr := net.JoinHostPort(backend.host, strconv.Itoa(backend.port))
dialStart := time.Now()
backendConn, err := net.DialTimeout("tcp", backendAddr, 5*time.Second)
if p.metrics != nil {
p.metrics.ProxyConnectLatency.Record(ctx, time.Since(dialStart).Seconds(), portAttr)
}
if err != nil {
p.logger.Error("Failed to connect to backend",
slog.Int("proxy_port", p.proxyPort),
slog.String("backend", backendAddr),
slog.String("error", err.Error()))
if p.metrics != nil {
p.metrics.ProxyErrorsTotal.Add(ctx, 1, portAttr)
}
return
}
defer backendConn.Close()
p.logger.Debug("Proxying connection",
slog.Int("proxy_port", p.proxyPort),
slog.String("backend", backendAddr),
slog.String("client", clientConn.RemoteAddr().String()))
// Wrap connections with idle timeout (skip if timeout is 0)
var src io.Reader = clientConn
var dst io.Writer = backendConn
var srcBack io.Reader = backendConn
var dstBack io.Writer = clientConn
if p.idleTimeout > 0 {
tcClient := &timeoutConn{Conn: clientConn, idleTimeout: p.idleTimeout}
tcBackend := &timeoutConn{Conn: backendConn, idleTimeout: p.idleTimeout}
src = tcClient
dst = tcBackend
srcBack = tcBackend
dstBack = tcClient
}
// Bidirectional copy
done := make(chan struct{}, 2)
go func() {
n, _ := io.Copy(dst, src)
if p.metrics != nil && n > 0 {
p.metrics.ProxyBytesTransferred.Add(ctx, n, portAttr)
}
done <- struct{}{}
}()
go func() {
n, _ := io.Copy(dstBack, srcBack)
if p.metrics != nil && n > 0 {
p.metrics.ProxyBytesTransferred.Add(ctx, n, portAttr)
}
done <- struct{}{}
}()
// Wait for either direction to complete
<-done
}
// getNextBackend returns the next backend using round-robin.
func (p *tcpProxy) getNextBackend() (tcpBackend, bool) {
p.mu.RLock()
defer p.mu.RUnlock()
if len(p.backends) == 0 {
return tcpBackend{}, false
}
index := atomic.AddUint64(&p.backendIndex, 1) - 1
return p.backends[index%uint64(len(p.backends))], true
}
// addBackend adds a backend to this proxy.
func (p *tcpProxy) addBackend(host string, port int) {
p.mu.Lock()
defer p.mu.Unlock()
p.backends = append(p.backends, tcpBackend{host: host, port: port})
p.logger.Info("Backend added to TCP proxy",
slog.Int("proxy_port", p.proxyPort),
slog.String("backend_host", host),
slog.Int("backend_port", port),
slog.Int("total_backends", len(p.backends)))
if p.metrics != nil {
p.metrics.ProxyBackendCount.Add(context.Background(), 1,
otelmetric.WithAttributes(attribute.Int("proxy_port", p.proxyPort)))
}
}
// removeBackend removes a backend from this proxy.
func (p *tcpProxy) removeBackend(host string, port int) bool {
p.mu.Lock()
defer p.mu.Unlock()
for i, b := range p.backends {
if b.host == host && b.port == port {
p.backends = append(p.backends[:i], p.backends[i+1:]...)
p.logger.Info("Backend removed from TCP proxy",
slog.Int("proxy_port", p.proxyPort),
slog.String("backend_host", host),
slog.Int("backend_port", port),
slog.Int("remaining_backends", len(p.backends)))
if p.metrics != nil {
p.metrics.ProxyBackendCount.Add(context.Background(), -1,
otelmetric.WithAttributes(attribute.Int("proxy_port", p.proxyPort)))
}
return true
}
}
return false
}
// stop stops the TCP proxy.
func (p *tcpProxy) stop() error {
close(p.done)
return p.listener.Close()
}
// backendCount returns the number of backends.
func (p *tcpProxy) backendCount() int {
p.mu.RLock()
defer p.mu.RUnlock()
return len(p.backends)
}
// stopProxy gracefully shuts down all TCP proxies.
func (d *Dewy) stopProxy(ctx context.Context) error {
d.proxyMutex.Lock()
defer d.proxyMutex.Unlock()
if d.tcpProxies == nil {
return nil
}
d.logger.Info("Stopping TCP proxies", slog.Int("count", len(d.tcpProxies)))
var errs []error
for port, proxy := range d.tcpProxies {
if err := proxy.stop(); err != nil {
errs = append(errs, fmt.Errorf("failed to stop proxy on port %d: %w", port, err))
}
}
d.tcpProxies = nil
if len(errs) > 0 {
return errors.Join(errs...)
}
d.logger.Info("All TCP proxies stopped")
return nil
}
// totalProxyBackends returns the total number of backends across all TCP proxies.
func (d *Dewy) totalProxyBackends() int {
d.proxyMutex.RLock()
defer d.proxyMutex.RUnlock()
total := 0
for _, proxy := range d.tcpProxies {
total += proxy.backendCount()
}
return total
}