optimizations + clean some code

This commit is contained in:
KingKDot
2026-05-21 23:28:06 -05:00
parent 06b001e5a1
commit bd47b233d6
7 changed files with 189 additions and 120 deletions
+22 -35
View File
@@ -14,6 +14,7 @@ import (
"os"
"runtime"
"runtime/debug"
"slices"
"strconv"
"strings"
"time"
@@ -87,17 +88,9 @@ func runClient(cfg config.Config) {
}
if shouldRefreshServerList(cfg, consecutiveFailures, lastDisconnect, lastSolRefresh, solInitialWait, solRefreshInterval) {
if refreshServerList(&cfg) {
lastSolRefresh = time.Now()
currentIndex = 0
consecutiveFailures = 0
}
}
if len(cfg.ServerURLs) > 1 {
currentIndex = (currentIndex + 1) % len(cfg.ServerURLs)
log.Printf("switching to next server [%d/%d]: %s", currentIndex+1, len(cfg.ServerURLs), cfg.ServerURLs[currentIndex])
tryRefreshServerList(&cfg, &lastSolRefresh, &currentIndex, &consecutiveFailures)
}
rotateToNextServer(&currentIndex, cfg.ServerURLs)
time.Sleep(backoff)
cancel()
@@ -134,17 +127,9 @@ func runClient(cfg config.Config) {
lastDisconnect = time.Now()
if shouldRefreshServerList(cfg, len(cfg.ServerURLs), lastDisconnect, lastSolRefresh, solInitialWait, solRefreshInterval) {
if refreshServerList(&cfg) {
lastSolRefresh = time.Now()
currentIndex = 0
consecutiveFailures = 0
}
}
if len(cfg.ServerURLs) > 1 {
currentIndex = (currentIndex + 1) % len(cfg.ServerURLs)
log.Printf("switching to next server [%d/%d]: %s", currentIndex+1, len(cfg.ServerURLs), cfg.ServerURLs[currentIndex])
tryRefreshServerList(&cfg, &lastSolRefresh, &currentIndex, &consecutiveFailures)
}
rotateToNextServer(&currentIndex, cfg.ServerURLs)
}
sleepFor := backoff
@@ -162,6 +147,21 @@ func runClient(cfg config.Config) {
}
}
func rotateToNextServer(currentIndex *int, urls []string) {
if len(urls) > 1 {
*currentIndex = (*currentIndex + 1) % len(urls)
log.Printf("switching to next server [%d/%d]: %s", *currentIndex+1, len(urls), urls[*currentIndex])
}
}
func tryRefreshServerList(cfg *config.Config, lastSolRefresh *time.Time, currentIndex *int, consecutiveFailures *int) {
if refreshServerList(cfg) {
*lastSolRefresh = time.Now()
*currentIndex = 0
*consecutiveFailures = 0
}
}
func buildDialOptions(cfg config.Config, transport *http.Transport) *websocket.DialOptions {
headers := http.Header{}
if token := strings.TrimSpace(cfg.AgentToken); token != "" {
@@ -268,7 +268,7 @@ func refreshServerURLsFromSolana(cfg *config.Config) bool {
log.Printf("[config] WARNING: Solana memo returned no valid URLs")
return false
}
if !equalStringSlices(cfg.ServerURLs, urls) {
if !slices.Equal(cfg.ServerURLs, urls) {
log.Printf("[config] resolved server URLs from Solana memo (%d servers)", len(urls))
cfg.ServerURLs = urls
}
@@ -287,24 +287,13 @@ func refreshServerURLsFromRaw(cfg *config.Config) bool {
return false
}
if !equalStringSlices(cfg.ServerURLs, urls) {
if !slices.Equal(cfg.ServerURLs, urls) {
log.Printf("[config] refreshed raw server list (%d servers)", len(urls))
cfg.ServerURLs = urls
}
return true
}
func equalStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func createHTTPTransport(cfg config.Config) *http.Transport {
tlsConfig := &tls.Config{
@@ -389,8 +378,6 @@ func classifyDialError(err error) string {
}
func computeBaseBackoff() time.Duration {
mode := strings.ToLower(strings.TrimSpace(os.Getenv("OVERLORD_MODE")))
_ = mode
return randomReconnectDelay(10*time.Second, 30*time.Second)
}