Add multi-relay URL support with round-robin and random endpoint selection

This commit is contained in:
Amin.MasterkinG
2026-04-21 15:58:23 +03:30
parent ab7365e35d
commit 787bd90ffd
9 changed files with 254 additions and 20 deletions
+22
View File
@@ -33,6 +33,7 @@ type Client struct {
socksConnections *SOCKSConnectionStore
chunkPolicy ChunkPolicy
headerBuilder *relayHeaderBuilder
relayURLs []string
connMu sync.Mutex
conns map[net.Conn]struct{}
@@ -46,6 +47,7 @@ type Client struct {
idlePongStreak atomic.Int64
pingState atomic.Int32
batchCursor atomic.Uint64
relayURLCursor atomic.Uint64
}
func New(cfg config.Config, lg *logger.Logger) *Client {
@@ -58,6 +60,7 @@ func New(cfg config.Config, lg *logger.Logger) *Client {
socksConnections: NewSOCKSConnectionStore(),
chunkPolicy: newChunkPolicy(cfg),
headerBuilder: newRelayHeaderBuilder(cfg, lg),
relayURLs: cfg.RelayEndpointURLs(),
conns: make(map[net.Conn]struct{}),
workCh: make(chan struct{}, 1),
}
@@ -216,3 +219,22 @@ func generateClientSessionKey() string {
return fmt.Sprintf("%s_%s", now, hex.EncodeToString(random))
}
func (c *Client) nextRelayURL() string {
if len(c.relayURLs) == 0 {
return c.cfg.RelayURL
}
if len(c.relayURLs) == 1 {
return c.relayURLs[0]
}
switch c.cfg.RelayURLSelection {
case "random":
return c.relayURLs[randomIndex(len(c.relayURLs))]
case "round_robin":
fallthrough
default:
index := c.relayURLCursor.Add(1) - 1
return c.relayURLs[index%uint64(len(c.relayURLs))]
}
}