/
market.go
170 lines (148 loc) · 3.42 KB
/
market.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package marketwatch
import (
"context"
"errors"
"fmt"
"log"
"strconv"
"sync"
"time"
"github.com/antihax/goesi/esi"
"github.com/antihax/goesi/optional"
"github.com/prometheus/client_golang/prometheus"
)
func (s *MarketWatch) marketWorker(regionID int32) {
// For totalization
wg := sync.WaitGroup{}
// Loop forever
for {
start := time.Now()
numOrders := 0
// Return Channels
rchan := make(chan []esi.GetMarketsRegionIdOrders200Ok, 100000)
echan := make(chan error, 100000)
orders, res, err := s.esi.ESI.MarketApi.GetMarketsRegionIdOrders(
context.Background(), "all", regionID, nil,
)
if err != nil {
log.Println(err)
continue
}
rchan <- orders
// Figure out if there are more pages
pages, err := getPages(res)
if err != nil {
log.Println(err)
continue
}
duration := timeUntilCacheExpires(res)
if duration.Minutes() < 3 {
fmt.Printf("%d market too close to window: waiting %s\n", regionID, duration.String())
time.Sleep(duration)
continue
}
// Get the other pages concurrently
for pages > 1 {
wg.Add(1) // count whats running
go func(page int32) {
defer wg.Done() // release when done
orders, r, err := s.esi.ESI.MarketApi.GetMarketsRegionIdOrders(
context.Background(),
"all",
regionID,
&esi.GetMarketsRegionIdOrdersOpts{Page: optional.NewInt32(page)},
)
if err != nil {
echan <- err
return
}
// Are we too close to the end of the window?
duration := timeUntilCacheExpires(r)
if duration.Seconds() < 20 {
echan <- errors.New("market too close to end of window")
return
}
// Add the orders to the channel
rchan <- orders
}(pages)
pages--
}
wg.Wait() // Wait for everything to finish
// Close the channels
close(rchan)
close(echan)
for err := range echan {
// Start over if any requests failed
log.Println(err)
continue
}
changes := []OrderChange{}
newOrders := []esi.GetMarketsRegionIdOrders200Ok{}
// Add all the orders together
for o := range rchan {
for i := range o {
change, isNew := s.storeData(int64(regionID), Order{Touched: start, Order: o[i]})
numOrders++
if change.Changed && !isNew {
changes = append(changes, change)
}
if isNew {
newOrders = append(newOrders, o[i])
}
}
}
deletions := s.expireOrders(int64(regionID), start)
// Log metrics
metricMarketTimePull.With(
prometheus.Labels{
"locationID": strconv.FormatInt(int64(regionID), 10),
},
).Observe(float64(time.Since(start).Nanoseconds()) / float64(time.Millisecond))
if len(newOrders) > 0 {
s.broadcast.Broadcast(
"market",
Message{
Action: "addition",
Payload: newOrders,
},
)
}
if len(changes) > 0 {
s.broadcast.Broadcast(
"market",
Message{
Action: "change",
Payload: changes,
},
)
}
if len(deletions) > 0 {
s.broadcast.Broadcast(
"market",
Message{
Action: "deletion",
Payload: deletions,
},
)
}
// Sleep until the cache timer expires, plus a little.
time.Sleep(duration)
}
}
// Metrics
var (
metricMarketTimePull = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "evemarketwatch",
Subsystem: "market",
Name: "pull",
Help: "Market Pull Statistics",
Buckets: prometheus.ExponentialBuckets(10, 1.6, 20),
},
[]string{"locationID"},
)
)
func init() {
prometheus.MustRegister(
metricMarketTimePull,
)
}