Ai
1 Star 0 Fork 0

monobytes/gcore

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
builder.go 975 Bytes
Copy Edit Raw Blame History
null authored 2025-01-22 18:29 +08:00 . first commit
package node
import (
"gitee.com/monobytes/gcore/gcluster"
"gitee.com/monobytes/gcore/internal/transporter/internal/client"
"golang.org/x/sync/singleflight"
"sync"
)
type Options struct {
InsID string // 实例ID
InsKind gcluster.Kind // 实例类型
}
type Builder struct {
sfg singleflight.Group
opts *Options
clients sync.Map
}
func NewBuilder(opts *Options) *Builder {
return &Builder{
opts: opts,
}
}
// Build 构建客户端
func (b *Builder) Build(addr string) (*Client, error) {
if cli, ok := b.clients.Load(addr); ok {
return cli.(*Client), nil
}
cli, err, _ := b.sfg.Do(addr, func() (interface{}, error) {
cli := NewClient(client.NewClient(&client.Options{
Addr: addr,
InsID: b.opts.InsID,
InsKind: b.opts.InsKind,
CloseHandler: func() { b.clients.Delete(addr) },
}))
b.clients.Store(addr, cli)
return cli, nil
})
if err != nil {
return nil, err
}
return cli.(*Client), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/monobytes/gcore.git
git@gitee.com:monobytes/gcore.git
monobytes
gcore
gcore
v0.0.17

Search