-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
42 lines (37 loc) · 1012 Bytes
/
content.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
package gaudius
import (
"errors"
"fmt"
"time"
"github.com/go-resty/resty/v2"
)
// type that manages the selected content node
type ContentNode struct {
ContentNodes []string
SelectedNode string
client *resty.Client
}
func NewContentNode(nodes []string) (*ContentNode, error) {
selectedNode, err := SelectHealthyContentNode(nodes)
if err != nil {
return nil, err
}
contentBaseUrl := selectedNode
contentClient := resty.New().SetBaseURL(contentBaseUrl)
return &ContentNode{ContentNodes: nodes, SelectedNode: selectedNode, client: contentClient}, nil
}
func SelectHealthyContentNode(contentNodes []string) (string, error) {
shuffle(contentNodes)
client := resty.New().SetTimeout(time.Second * 3).GetClient()
for _, endpoint := range contentNodes {
route := fmt.Sprintf("%s/health_check", endpoint)
res, err := client.Get(route)
if err != nil {
continue
}
if res.StatusCode == 200 {
return endpoint, nil
}
}
return "", errors.New("found no healthy content nodes")
}