refactor: hoist SSE client to struct, split RPC and SSE HTTP clients
The SSE client (no timeout, long-lived stream) was recreated on every reconnect, wasting connection pool resources. Hoist it to the struct alongside the RPC client (30s timeout) so connections can be reused. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b5bd7985c6
commit
af25536c19
1 changed files with 10 additions and 9 deletions
|
|
@ -40,7 +40,8 @@ const (
|
||||||
type SignalChannel struct {
|
type SignalChannel struct {
|
||||||
*channels.BaseChannel
|
*channels.BaseChannel
|
||||||
config config.SignalConfig
|
config config.SignalConfig
|
||||||
httpClient *http.Client
|
rpcClient *http.Client // JSON-RPC calls (30s timeout)
|
||||||
|
sseClient *http.Client // SSE streaming (no timeout)
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
|
@ -134,7 +135,8 @@ func NewSignalChannel(cfg *config.Config, b *bus.MessageBus) (channels.Channel,
|
||||||
return &SignalChannel{
|
return &SignalChannel{
|
||||||
BaseChannel: base,
|
BaseChannel: base,
|
||||||
config: signalCfg,
|
config: signalCfg,
|
||||||
httpClient: &http.Client{Timeout: signalRPCTimeout},
|
rpcClient: &http.Client{Timeout: signalRPCTimeout},
|
||||||
|
sseClient: &http.Client{Timeout: 0},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -291,8 +293,7 @@ func (c *SignalChannel) connectSSE() error {
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", "text/event-stream")
|
req.Header.Set("Accept", "text/event-stream")
|
||||||
|
|
||||||
sseClient := &http.Client{Timeout: 0}
|
resp, err := c.sseClient.Do(req)
|
||||||
resp, err := sseClient.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("SSE connection failed: %w", err)
|
return fmt.Errorf("SSE connection failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -659,7 +660,7 @@ func (c *SignalChannel) rpcCall(ctx context.Context, method string, params any)
|
||||||
}
|
}
|
||||||
httpReq.Header.Set("Content-Type", "application/json")
|
httpReq.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(httpReq)
|
resp, err := c.rpcClient.Do(httpReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("RPC request failed: %w", err)
|
return nil, fmt.Errorf("RPC request failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue