-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
45 lines (36 loc) · 1.15 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
package hypersyncgo
import (
"context"
"fmt"
"github.com/enviodev/hypersync-client-go/types"
"math/big"
"math/rand"
"net/http"
"time"
)
type ArchiveHeight struct {
Height *big.Int `json:"height"`
}
func (c *Client) GetHeight(ctx context.Context) (*big.Int, error) {
base := c.opts.RetryBaseMs
for i := 0; i < c.opts.MaxNumRetries+1; i++ {
response, err := Do[struct{}, ArchiveHeight](ctx, c, c.GeUrlFromNodeAndPath(c.opts, "height"), http.MethodGet, struct{}{})
if err == nil {
return response.Height, nil
}
// TODO: Implement proper logger...
fmt.Printf("Failed to get height from server, retrying... Error: %v\n", err)
baseMs := base * time.Millisecond
jitter := time.Duration(rand.Int63n(int64(c.opts.RetryBackoffMs))) * time.Millisecond
select {
case <-time.After(baseMs + jitter):
base = min(base+c.opts.RetryBackoffMs, c.opts.RetryCeilingMs)
case <-ctx.Done():
return big.NewInt(0), ctx.Err()
}
}
return big.NewInt(0), fmt.Errorf("failed to get height after retries: %d", c.opts.MaxNumRetries)
}
func (c *Client) Get(ctx context.Context, query *types.Query) (*types.QueryResponse, error) {
return c.GetArrow(ctx, query)
}