-
Notifications
You must be signed in to change notification settings - Fork 44
/
poolservice.go
77 lines (67 loc) · 1.76 KB
/
poolservice.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package pool
import (
"context"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/app/ocache"
"github.com/anyproto/any-sync/metric"
"github.com/anyproto/any-sync/net/peer"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"time"
)
const (
CName = "common.net.pool"
)
var log = logger.NewNamed(CName)
func New() Service {
return &poolService{}
}
type Service interface {
Pool
app.ComponentRunnable
}
type dialer interface {
Dial(ctx context.Context, peerId string) (pr peer.Peer, err error)
}
type poolService struct {
// default pool
*pool
dialer dialer
metricReg *prometheus.Registry
}
func (p *poolService) Init(a *app.App) (err error) {
p.dialer = a.MustComponent("net.peerservice").(dialer)
p.pool = &pool{}
if m := a.Component(metric.CName); m != nil {
p.metricReg = m.(metric.Metric).Registry()
}
p.pool.outgoing = ocache.New(
func(ctx context.Context, id string) (value ocache.Object, err error) {
return p.dialer.Dial(ctx, id)
},
ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute/2),
ocache.WithTTL(time.Minute),
ocache.WithPrometheus(p.metricReg, "netpool", "outgoing"),
)
p.pool.incoming = ocache.New(
func(ctx context.Context, id string) (value ocache.Object, err error) {
return nil, ocache.ErrNotExists
},
ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute/2),
ocache.WithTTL(time.Minute),
ocache.WithPrometheus(p.metricReg, "netpool", "incoming"),
)
return nil
}
func (p *pool) Run(ctx context.Context) (err error) {
return nil
}
func (p *pool) Close(ctx context.Context) (err error) {
if e := p.incoming.Close(); e != nil {
log.Warn("close incoming cache error", zap.Error(e))
}
return p.outgoing.Close()
}