Skip to content

Commit 4bbdf14

Browse files
committed
fix(api): in-memory refresh, date range bug, and server cleanup
Extract InMemoryHandler.refresh() to deduplicate init and ticker logic; on error keep the previously-held value instead of zeroing it. Fix GetVolumeByDate date range check which was computing StartDate.Sub(EndDate) instead of EndDate.Sub(StartDate), making the 30-day guard always pass. Call inMemoryHandler.Stop() on server startup failure so the background goroutine does not leak.
1 parent 4cea545 commit 4bbdf14

3 files changed

Lines changed: 26 additions & 33 deletions

File tree

api/handlers/in_memory.go

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,40 @@ func NewInMemoryHandler(db InMemoryDbHandler, chainName string) *InMemoryHandler
5252
// - none
5353
func (h *InMemoryHandler) Start() {
5454
// initialize the data
55+
h.refresh()
56+
57+
go func() {
58+
for {
59+
select {
60+
case <-h.done:
61+
return
62+
case <-time.After(h.interval):
63+
h.refresh()
64+
}
65+
}
66+
}()
67+
}
68+
69+
// refresh fetches the latest constant data from the database.
70+
// On error it logs and keeps the previously held value, so a transient
71+
// failure does not wipe otherwise-good data.
72+
func (h *InMemoryHandler) refresh() {
5573
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(15*time.Second))
5674
defer cancel()
75+
5776
avgBlockProdTime, err := h.db.GetAvgBlockProdTime(ctx, h.chainName)
5877
if err != nil {
5978
log.Printf("error getting average block production time: %v", err)
60-
h.data.AvgBlockProdTime = 0
61-
return
79+
} else {
80+
h.data.AvgBlockProdTime = avgBlockProdTime
6281
}
63-
h.data.AvgBlockProdTime = avgBlockProdTime
6482

6583
totalAddressesCount, err := h.db.GetTotalAddressesCount(ctx, h.chainName)
6684
if err != nil {
6785
log.Printf("error getting total addresses count: %v", err)
68-
h.data.TotalAddressesCount = 0
69-
return
86+
} else {
87+
h.data.TotalAddressesCount = totalAddressesCount
7088
}
71-
h.data.TotalAddressesCount = totalAddressesCount
72-
73-
go func() {
74-
for {
75-
select {
76-
case <-h.done:
77-
return
78-
case <-time.After(h.interval):
79-
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(15*time.Second))
80-
defer cancel()
81-
avgBlockProdTime, err := h.db.GetAvgBlockProdTime(ctx, h.chainName)
82-
if err != nil {
83-
log.Printf("error getting average block production time: %v", err)
84-
h.data.AvgBlockProdTime = 0
85-
continue
86-
}
87-
h.data.AvgBlockProdTime = avgBlockProdTime
88-
totalAddressesCount, err := h.db.GetTotalAddressesCount(ctx, h.chainName)
89-
if err != nil {
90-
log.Printf("error getting total addresses count: %v", err)
91-
h.data.TotalAddressesCount = 0
92-
continue
93-
}
94-
h.data.TotalAddressesCount = totalAddressesCount
95-
}
96-
}
97-
}()
9889
}
9990

10091
func (h *InMemoryHandler) Stop() {

api/handlers/transactions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (h *TransactionsHandler) GetVolumeByDate(ctx context.Context, input *humaty
287287
if !input.StartDate.Before(input.EndDate.Time) {
288288
return nil, badRequest("start_date must be before end_date")
289289
}
290-
if input.StartDate.Sub(input.EndDate.Time) > 24*time.Hour*30 {
290+
if input.EndDate.Sub(input.StartDate.Time) > 24*time.Hour*30 {
291291
return nil, badRequest("end_date must be within 30 days of start_date")
292292
}
293293

api/serve.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ func runServe(cmd *cobra.Command, args []string) {
205205
log.Printf("Starting server on %s with HTTPS", addr)
206206
err = http.ListenAndServeTLS(addr, certFilePath, keyFilePath, mux)
207207
if err != nil {
208+
inMemoryHandler.Stop()
208209
log.Fatalf("failed to start server: %v", err)
209210
}
210211
} else {
211212
log.Printf("Starting server on %s with HTTP", addr)
212213
err = http.ListenAndServe(addr, mux)
213214
if err != nil {
215+
inMemoryHandler.Stop()
214216
log.Fatalf("failed to start server: %v", err)
215217
}
216218
}

0 commit comments

Comments
 (0)