-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkers.go
78 lines (65 loc) · 1.52 KB
/
workers.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
package hammer
// Result holds the balance of each address
// All balances in Satoshi
type Result struct {
Source, Address string
BalanceTotal float64
BalanceConfirmed float64
BalanceUnconfirmed float64
}
// Request holds a balance request
type Request struct {
Address string
Output chan Result
}
// Worker defines the methods a balance querying worker must implement
type Worker interface {
// Name of the worker
Name() string
// Start worker processing
Start()
// Get balance of address using this worker
GetBalance(string) Result
// Set the input channel of worker
setInput(chan Request)
}
// W contains the fields that nearly all workers need
// Also implements some of the Worker interface
type W struct {
name string
input chan Request
}
// Name of the worker
func (w W) Name() string {
return w.name
}
// GetBalance queries and returns balance of given address
func (w W) GetBalance(addr string) Result {
output := make(chan Result)
defer close(output)
req := Request{
Address: addr,
Output: output,
}
w.input <- req
return <-output
}
// SetInput sets the input channel of the worker
func (w *W) setInput(input chan Request) {
w.input = input
}
// Helpers
// WorkersAll returns slice containing all workers
func WorkersAll() []Worker {
return []Worker{
NewBlockonomics(),
NewBlockcypher(),
}
}
// SubmitAddresses submits addresses to a channel
// Usually called in a goroutine
func submitRequests(requests []Request, ch chan<- Request) {
for _, rq := range requests {
ch <- rq
}
}