Files
tasks/client/history/connections.go
zeekay 1c76604f15 feat: ZAP transport for internode RPC, replace gRPC for history/matching/worker
- New zaptransport package: factory, client, server, rpcserver
- Internode services (history, matching, worker) use ZAP
- Frontend keeps gRPC for Temporal SDK backward compat (for now)
- luxfi/zap added as dependency
2026-04-09 18:18:31 -07:00

94 lines
2.2 KiB
Go

package history
import (
"sync"
"github.com/hanzoai/tasks/common/membership"
"google.golang.org/grpc"
)
type (
clientConnection[C any] struct {
grpcClient C
conn grpc.ClientConnInterface
}
rpcAddress string
connectionPoolImpl[C any] struct {
mu struct {
sync.RWMutex
conns map[rpcAddress]clientConnection[C]
}
historyServiceResolver membership.ServiceResolver
rpcFactory RPCFactory
clientCtor func(grpc.ClientConnInterface) C
}
// RPCFactory is a subset of the common.RPCFactory interface for testing.
RPCFactory interface {
CreateHistoryGRPCConnection(rpcAddress string) grpc.ClientConnInterface
}
connectionPool[C any] interface {
getOrCreateClientConn(addr rpcAddress) clientConnection[C]
getAllClientConns() []clientConnection[C]
resetConnectBackoff(clientConnection[C])
}
)
func NewConnectionPool[C any](
historyServiceResolver membership.ServiceResolver,
rpcFactory RPCFactory,
clientCtor func(grpc.ClientConnInterface) C,
) *connectionPoolImpl[C] {
c := &connectionPoolImpl[C]{
historyServiceResolver: historyServiceResolver,
rpcFactory: rpcFactory,
clientCtor: clientCtor,
}
c.mu.conns = make(map[rpcAddress]clientConnection[C])
return c
}
func (c *connectionPoolImpl[C]) getOrCreateClientConn(addr rpcAddress) clientConnection[C] {
c.mu.RLock()
cc, ok := c.mu.conns[addr]
c.mu.RUnlock()
if ok {
return cc
}
c.mu.Lock()
defer c.mu.Unlock()
if cc, ok = c.mu.conns[addr]; ok {
return cc
}
conn := c.rpcFactory.CreateHistoryGRPCConnection(string(addr))
cc = clientConnection[C]{
grpcClient: c.clientCtor(conn),
conn: conn,
}
c.mu.conns[addr] = cc
return cc
}
func (c *connectionPoolImpl[C]) getAllClientConns() []clientConnection[C] {
hostInfos := c.historyServiceResolver.Members()
var clientConns []clientConnection[C]
for _, hostInfo := range hostInfos {
cc := c.getOrCreateClientConn(rpcAddress(hostInfo.GetAddress()))
clientConns = append(clientConns, cc)
}
return clientConns
}
// resetConnectBackoff is a no-op for ZAP transport (no backoff state).
func (c *connectionPoolImpl[C]) resetConnectBackoff(cc clientConnection[C]) {
}