Documentation
¶
Overview ¶
Example ¶
Example 展示如何使用泛型负载均衡器
// 定义URLs
urls := []string{
"http://localhost:8545",
"http://localhost:8546",
"http://localhost:8547",
}
// 创建负载均衡器
lb := New(urls, mockClientFactory)
if lb == nil {
return
}
defer lb.Close()
// 获取客户端并使用
client := lb.NextClient()
_ = client.GetRawUrl() // 实际使用中调用客户端方法
lb.ReleaseClient(client)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientFactory ¶
ClientFactory 定义了创建客户端的工厂函数类型
type LoadBalance ¶
type LoadBalance[T RPCClient] interface { Close() SetMode(mode int) GetChainId() uint64 NextClient() T ReleaseClient(T) }
LoadBalance 负载均衡器接口
func New ¶
func New[T RPCClient](urls []string, factory ClientFactory[T]) LoadBalance[T]
New 创建新的负载均衡器
type RPCClient ¶
type RPCClient interface {
comparable
// Close 关闭客户端连接
Close()
// GetRawUrl 获取原始URL
GetRawUrl() string
// GetChainId 获取链ID
GetChainId() uint64
}
RPCClient 定义了RPC客户端需要实现的通用接口
Click to show internal directories.
Click to hide internal directories.