forked from gateway-fm/service-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthchecks.go
67 lines (53 loc) · 1.59 KB
/
healthchecks.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
package pool
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/gateway-fm/prover-pool-lib/prover"
srv "github.com/gateway-fm/prover-pool-lib/service"
"github.com/gateway-fm/scriptorium/logger"
)
const (
maxHCNumTries = 5
hcRetrySleepInterval = time.Millisecond * 200
)
type HealthcheckFunc func(timeOut time.Duration, p prover.IProver) (bool, error)
func ProverMockHealthcheck(timeOut time.Duration) func(iProver prover.IProver) error {
return func(p prover.IProver) error {
return healthcheckWithRetry(timeOut, p, 0, nil, proverMockHealthcheck)
}
}
func proverMockHealthcheck(timeOut time.Duration, p prover.IProver) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
_ = ctx
load := rand.Float32()
p.SetStatus(srv.StatusHealthy)
p.SetLoad(load)
return false, nil
}
func healthcheckWithRetry(
timeOut time.Duration, p prover.IProver,
try int, lastErr error,
hcFunc HealthcheckFunc) error {
if try >= maxHCNumTries {
p.SetStatus(srv.StatusUnHealthy)
return lastErr
}
if try > 0 {
logger.Log().Warn(fmt.Sprintf("retrying healthcheck to service %s %s... current try is %d out of %d, the last error was: %s", p.NodeName(), p.ID(), try+1, maxHCNumTries, lastErr.Error()))
}
retryNeeded, err := hcFunc(timeOut, p)
if err == nil {
if try > 0 {
logger.Log().Info(fmt.Sprintf("the healthcheck to service %s %s is recovered after retry (•‿•)", p.NodeName(), p.ID()))
}
return nil
}
if !retryNeeded {
return err
}
time.Sleep(hcRetrySleepInterval)
return healthcheckWithRetry(timeOut, p, try+1, err, hcFunc)
}