-
Notifications
You must be signed in to change notification settings - Fork 0
/
sources.go
46 lines (40 loc) · 841 Bytes
/
sources.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
package main
import (
"fmt"
"github.com/go-redis/redis/v8"
"math/rand"
)
// Source Define some method who need to be impl for each source
type Source interface {
init()
refresh() []string
getRandom() string
}
// Redis Source for redis
type Redis struct {
rdb *redis.Client
*redis.Options
}
func (r *Redis) init() error {
if r.rdb == nil {
if r.Options == nil {
panic("Redis Options is null!!")
}
r.rdb = redis.NewClient(r.Options)
}
ping := r.rdb.Ping(ctx)
return ping.Err()
}
func (r *Redis) refresh() []string {
servers, err := r.rdb.LRange(ctx, "improvised:servers", 0, -1).Result()
checkError(err)
return servers
}
func (r *Redis) getRandom() *string {
servers := r.refresh()
if len(servers) == 0 {
fmt.Println("There is no server available")
return nil
}
return &servers[rand.Intn(len(servers))]
}