perf: reuse shared HTTP client in Hyperliquid trader

Previously each direct API call (orders, sync, account) created a new
http.Client, preventing TCP/TLS connection reuse. Now all calls share
a single client on the HyperliquidTrader struct (30s timeout).
This commit is contained in:
shinchan-zhai
2026-03-23 10:56:54 +08:00
parent 982ee668c9
commit 3c698e3fc5
4 changed files with 11 additions and 8 deletions

View File

@@ -4,10 +4,12 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"net/http"
"nofx/logger"
"strconv"
"strings"
"sync"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/sonirico/go-hyperliquid"
@@ -17,6 +19,7 @@ import (
type HyperliquidTrader struct {
exchange *hyperliquid.Exchange
ctx context.Context
httpClient *http.Client // Shared HTTP client for direct API calls (connection reuse)
walletAddr string
meta *hyperliquid.Meta // Cache meta information (including precision)
metaMutex sync.RWMutex // Protect concurrent access to meta field
@@ -220,6 +223,7 @@ func NewHyperliquidTrader(privateKeyHex string, walletAddr string, testnet bool,
return &HyperliquidTrader{
exchange: exchange,
ctx: ctx,
httpClient: &http.Client{Timeout: 30 * time.Second},
walletAddr: walletAddr,
meta: meta,
isCrossMargin: true, // Use cross margin mode by default