feat: 체인 클라이언트 멀티노드 페일오버 (SPOF 해결)
All checks were successful
Server CI/CD / lint-and-build (push) Successful in 21s
Server CI/CD / deploy (push) Successful in 56s

CHAIN_NODE_URLS 환경변수(쉼표 구분)로 복수 노드 지정 가능.
Client.Call()이 네트워크/HTTP 오류 시 다음 노드로 자동 전환.
RPC 레벨 오류(트랜잭션 실패 등)는 즉시 반환 (페일오버 미적용).
기존 CHAIN_NODE_URL 단일 설정은 하위 호환 유지.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-18 17:31:46 +09:00
parent e187a20e28
commit feb8ec96ad
3 changed files with 60 additions and 10 deletions

View File

@@ -34,21 +34,55 @@ func (e *rpcError) Error() string {
}
// Client is a JSON-RPC 2.0 client for the TOL Chain node.
// It supports multiple node URLs for failover: on a network/HTTP error the
// client automatically retries against the next URL in the list.
// RPC-level errors (transaction failures, etc.) are returned immediately
// without failover since they indicate a logical error, not node unavailability.
type Client struct {
nodeURL string
http *http.Client
idSeq atomic.Int64
nodeURLs []string
http *http.Client
idSeq atomic.Int64
next atomic.Uint64 // round-robin index
}
func NewClient(nodeURL string) *Client {
// NewClient creates a client for one or more chain node URLs.
// When multiple URLs are provided, failed requests fall over to the next URL.
func NewClient(nodeURLs ...string) *Client {
if len(nodeURLs) == 0 {
panic("chain.NewClient: at least one node URL is required")
}
return &Client{
nodeURL: nodeURL,
http: &http.Client{Timeout: 10 * time.Second},
nodeURLs: nodeURLs,
http: &http.Client{Timeout: 10 * time.Second},
}
}
// Call invokes a JSON-RPC method and unmarshals the result into out.
// On network or HTTP errors it tries each node URL once before giving up.
func (c *Client) Call(method string, params any, out any) error {
n := len(c.nodeURLs)
start := int(c.next.Load() % uint64(n))
var lastErr error
for i := 0; i < n; i++ {
url := c.nodeURLs[(start+i)%n]
err := c.callNode(url, method, params, out)
if err == nil {
return nil
}
// RPC-level error (e.g. tx execution failure): return immediately,
// retrying on another node would give the same result.
if _, isRPC := err.(*rpcError); isRPC {
return err
}
// Network / HTTP error: mark this node as degraded and try the next.
lastErr = err
c.next.Add(1)
}
return fmt.Errorf("all chain nodes unreachable: %w", lastErr)
}
func (c *Client) callNode(nodeURL, method string, params any, out any) error {
reqBody := rpcRequest{
JSONRPC: "2.0",
ID: c.idSeq.Add(1),
@@ -60,14 +94,14 @@ func (c *Client) Call(method string, params any, out any) error {
return fmt.Errorf("marshal RPC request: %w", err)
}
resp, err := c.http.Post(c.nodeURL, "application/json", bytes.NewReader(data))
resp, err := c.http.Post(nodeURL, "application/json", bytes.NewReader(data))
if err != nil {
return fmt.Errorf("RPC network error: %w", err)
return fmt.Errorf("RPC network error (%s): %w", nodeURL, err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("RPC HTTP error: status %d", resp.StatusCode)
return fmt.Errorf("RPC HTTP error (%s): status %d", nodeURL, resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 10*1024*1024))