Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cmd/centralserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (cs *centralServer) dispatchFrame(frame *tunnel.Frame, source net.Conn) {
cs.handleRST(frame)
return
}
cs.handleData(frame)
cs.handleData(frame, source)
}

func (cs *centralServer) handleSYN(frame *tunnel.Frame, source net.Conn) {
Expand Down Expand Up @@ -424,7 +424,7 @@ func (cs *centralServer) sendFrame(connID uint32, frame *tunnel.Frame) {
log.Printf("[central] conn=%d: all sources failed", connID)
}

func (cs *centralServer) handleData(frame *tunnel.Frame) {
func (cs *centralServer) handleData(frame *tunnel.Frame, source net.Conn) {
cs.mu.RLock()
state, ok := cs.conns[frame.ConnID]
cs.mu.RUnlock()
Expand All @@ -435,6 +435,19 @@ func (cs *centralServer) handleData(frame *tunnel.Frame) {
state.mu.Lock()
defer state.mu.Unlock()

// If this source isn't known yet (e.g., after tunnel recycling), add it.
// This ensures responses can flow back through the new connection.
found := false
for _, s := range state.sources {
if s == source {
found = true
break
}
}
if !found {
state.sources = append(state.sources, source)
}

state.reorderer.Insert(frame.SeqNum, frame.Payload)
if state.target == nil {
return // buffered, flushed when upstream connects
Expand Down
13 changes: 9 additions & 4 deletions internal/tunnel/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"log"
"math/rand"
"net"
"sync"
"sync/atomic"
Expand All @@ -30,8 +31,9 @@ type TunnelConn struct {
closed bool
incoming chan *Frame
createdAt time.Time
lastRead atomic.Int64 // unix timestamp of last successful read
writeErrs atomic.Int32 // consecutive write errors
maxAge time.Duration // per-connection jittered max age
lastRead atomic.Int64 // unix timestamp of last successful read
writeErrs atomic.Int32 // consecutive write errors
}

// TunnelPool manages persistent connections to all healthy instances.
Expand Down Expand Up @@ -151,8 +153,7 @@ func (p *TunnelPool) refreshConnections() {

if !activeIDs[id] || tc.closed {
shouldRemove = true
} else if now.Sub(tc.createdAt) > tunnelMaxAge {
// Connection too old → recycle to get a fresh QUIC stream
} else if now.Sub(tc.createdAt) > tc.maxAge {
log.Printf("[tunnel-pool] recycling instance %d connection (age=%s)",
id, now.Sub(tc.createdAt).Round(time.Second))
shouldRemove = true
Expand Down Expand Up @@ -202,11 +203,15 @@ func (p *TunnelPool) connectInstance(inst *engine.Instance) (*TunnelConn, error)
tc.SetNoDelay(true)
}

// Jittered max age: tunnelMaxAge ± 50%
jitter := time.Duration(rand.Int63n(int64(tunnelMaxAge))) - tunnelMaxAge/2

tunnel := &TunnelConn{
inst: inst,
conn: conn,
incoming: make(chan *Frame, 256),
createdAt: time.Now(),
maxAge: tunnelMaxAge + jitter,
}
tunnel.lastRead.Store(time.Now().Unix())

Expand Down
Loading