-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreporter.go
144 lines (124 loc) · 2.61 KB
/
reporter.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package usage
import (
"context"
"net/http"
"runtime"
"time"
"github.com/catalinc/hashcash"
)
type UsageClient interface {
NewSession(context.Context, *SessionRequest) (*SessionReply, error)
SendReport(context.Context, *ReportRequest) (*ReportReply, error)
}
type Reporter struct {
Options
Client UsageClient
Start time.Time
Token string
}
func Report(ctx context.Context, opt Options, c UsageClient) error {
reporter, err := New(opt, c)
if err != nil {
return err
}
go reporter.Report(ctx)
return nil
}
func New(opt Options, c UsageClient) (*Reporter, error) {
if opt.URL == "" {
opt.URL = defaultURL
}
if opt.HashBits == 0 {
opt.HashBits = hashBits
}
if opt.SaltChars == 0 {
opt.SaltChars = saltChars
}
if opt.Minter == nil {
opt.Minter = hashcash.New(opt.HashBits, opt.SaltChars, defaultExtension)
}
if opt.SessionEndpoint == "" {
opt.SessionEndpoint = sessionEndpoint
}
if opt.ReportEndpoint == "" {
opt.ReportEndpoint = reportEndpoint
}
if opt.Timeout == 0 {
opt.Timeout = timeout
}
if opt.ReportLapse == 0 {
opt.ReportLapse = reportLapse
}
if c == nil {
httpClient := &http.Client{}
if opt.Client != nil {
httpClient = opt.Client
}
c = &Client{
HTTPClient: &BasicClient{
Client: httpClient,
URL: opt.URL,
UserAgent: opt.UserAgent,
},
SessionEndpoint: opt.SessionEndpoint,
ReportEndpoint: opt.ReportEndpoint,
}
}
localCtx, cancel := context.WithTimeout(context.Background(), opt.Timeout)
defer cancel()
ses, err := c.NewSession(
localCtx,
&SessionRequest{
ClusterID: opt.ClusterID,
ServerID: opt.ServerID,
},
)
return &Reporter{
Client: c,
Start: time.Now(),
Options: opt,
Token: ses.Token,
}, err
}
func (r *Reporter) Report(ctx context.Context) {
tc := time.NewTicker(r.ReportLapse)
defer tc.Stop()
for {
localCtx, cancel := context.WithTimeout(ctx, r.Timeout)
r.SingleReport(localCtx)
cancel()
select {
case <-ctx.Done():
return
case <-tc.C:
}
}
}
func (r *Reporter) SingleReport(ctx context.Context) error {
ud := UsageData{
Version: r.Version,
Arch: runtime.GOARCH,
OS: runtime.GOOS,
ClusterID: r.ClusterID,
ServerID: r.ServerID,
Uptime: int64(time.Since(r.Start) / time.Second),
Time: time.Now().Unix(),
}
if v := int64(r.ReportLapse / time.Second); ud.Uptime < v {
ud.Extra = r.ExtraPayload
}
base, err := ud.Hash()
if err != nil {
return err
}
pow, err := r.Minter.Mint(r.Token + base)
if err != nil {
return err
}
_, err = r.Client.SendReport(ctx, &ReportRequest{
Token: r.Token,
Pow: pow,
Data: ud,
})
return err
}