-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
68 lines (58 loc) · 1.79 KB
/
handlers.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
package main
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/btcsuite/btcd/rpcclient"
)
func caller(key string, client *rpcclient.Client, cache *Cache, expiration time.Duration, fn func(BlockChainInfoGetter) (*float64, error)) float64 {
result, exists := cache.Get(key)
if !exists {
resp, err := fn(client)
if resp == nil || err != nil {
log.Printf("Unable to fetch %s (nil or err): %s", key, err)
result = float64(0)
} else {
result = *resp
cache.Set(key, result, expiration)
}
}
return result
}
func handleHealthcheck(w http.ResponseWriter, r *http.Request, client *rpcclient.Client, expiration time.Duration, waitForTxIndex bool, waitForFeeEstimation bool, cache *Cache) {
w.Header().Set("Content-Type", "application/json")
resp := make(map[string]bool)
allTrue := true
vLog("handler.go: handling healthcheck")
vLog("handler.go: handling txindex")
if waitForTxIndex {
indexRes := caller("indexinfo", client, cache, expiration, getIndexInfo)
resp["gettxindexinfo"] = indexRes > 0.0
if !resp["gettxindexinfo"] {
allTrue = false
}
}
vLog("handler.go: handling fee estimation")
if waitForFeeEstimation {
feeRes := caller("estimatefee", client, cache, expiration, getFeeEstimation)
resp["estimatesmartfee"] = feeRes > 0.0
if !resp["estimatesmartfee"] {
allTrue = false
}
}
vLog("handler.go: handling blockchain info")
progressRes := caller("verificationprogress", client, cache, expiration, getBlockChainInfo)
resp["verificationprogress"] = progressRes > 0.9999
if !resp["verificationprogress"] {
allTrue = false
}
vLog("handler.go: sending response")
jsonResp, _ := json.Marshal(resp)
if !allTrue {
vLog("handler.go: not all checks passed")
http.Error(w, string(jsonResp), http.StatusServiceUnavailable)
return
}
w.Write(jsonResp)
}