-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
82 lines (72 loc) · 1.94 KB
/
api.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
78
79
80
81
82
package retrieval
import (
"context"
"time"
"github.com/gauss-project/aurorafs/pkg/logging"
"github.com/gauss-project/aurorafs/pkg/rpc"
"github.com/gauss-project/aurorafs/pkg/subscribe"
"github.com/prometheus/client_golang/prometheus"
)
func (s *Service) API() rpc.API {
return rpc.API{
Namespace: "retrieval",
Version: "1.0",
Service: &apiService{s: s},
Public: true,
}
}
type apiService struct {
s *Service
}
func (a *apiService) Metrics(ctx context.Context) (*rpc.Subscription, error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
sub := notifier.CreateSubscription()
iNotifier := subscribe.NewNotifier(notifier, sub)
_ = a.s.subPub.Subscribe(iNotifier, "retrieval", "metrics", "")
go func() {
logging.Tracef("%s metrics subscribe success", sub.ID)
send, out := subscribe.ProcessMetricMsg(sub, []prometheus.Metric{
a.s.metrics.TotalRetrieved,
a.s.metrics.TotalTransferred,
})
if send {
err := a.s.subPub.Publish("retrieval", "metrics", "", out)
if err != nil {
logging.Errorf("%s metrics notify %s", sub.ID, err)
} else {
logging.Tracef("%s metrics notify success", sub.ID)
}
}
t := time.NewTicker(time.Second * 5)
defer t.Stop()
for {
select {
case <-t.C:
send, out = subscribe.ProcessMetricMsg(sub, []prometheus.Metric{
a.s.metrics.TotalRetrieved,
a.s.metrics.TotalTransferred,
})
if send {
err := a.s.subPub.Publish("retrieval", "metrics", "", out)
if err != nil {
logging.Errorf("%s metrics notify %s", sub.ID, err)
} else {
logging.Tracef("%s metrics notify success", sub.ID)
}
}
case e := <-sub.Err():
if e == nil {
logging.Debugf("%s metrics quit unsubscribe", sub.ID)
} else {
logging.Warningf("%s metrics quit %s", sub.ID, e)
}
subscribe.CacheRemove(ctx, sub.ID)
return
}
}
}()
return sub, nil
}