-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
factory.go
44 lines (40 loc) · 1 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package client
import (
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/protocol"
"github.com/bitmagnet-io/bitmagnet/internal/protocol/dht/server"
"go.uber.org/fx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
)
type Params struct {
fx.In
NodeID protocol.ID `name:"dht_node_id"`
Server lazy.Lazy[server.Server]
Logger *zap.SugaredLogger
}
type Result struct {
fx.Out
Client lazy.Lazy[Client]
}
func New(p Params) Result {
return Result{
Client: lazy.New(func() (Client, error) {
s, err := p.Server.Get()
if err != nil {
return nil, err
}
return clientLogger{
client: serverAdapter{
nodeID: p.NodeID,
server: s,
},
// we make way to many queries to usefully log everything, but having a sample is helpful:
logger: p.Logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSamplerWithOptions(core, time.Minute, 10, 0)
})).Named("dht_client"),
}, nil
}),
}
}