forked from vladaionescu/leveros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aerospike.go
68 lines (61 loc) · 1.6 KB
/
aerospike.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 store
import (
"strconv"
"strings"
"time"
aerospike "github.com/aerospike/aerospike-client-go"
"github.com/leveros/leveros/config"
"github.com/leveros/leveros/scale"
)
const leverOSNamespace = "leveros"
var (
// AerospikeFromConsulFlag indicates whether Consul should be used to get
// an Aerospike client.
AerospikeFromConsulFlag = config.DeclareBool(
PackageName, "aerospikeFromConsul")
// AerospikeFixedAddrFlag indicates a fixed address which is used if
// AerospikeFromConsulFlag is false.
AerospikeFixedAddrFlag = config.DeclareString(
PackageName, "aerospikeFixedAddr", "leverosaerospike:3000")
)
// NewAerospike returns a new Aerospike client.
func NewAerospike() (as *aerospike.Client, err error) {
var target string
if AerospikeFromConsulFlag.Get() {
for retry := 0; retry < 15; retry++ {
target, _, err = scale.DereferenceService("aerospike")
if err == nil {
break
}
if err == scale.ErrServiceNotFound ||
strings.Contains(err.Error(), "network is unreachable") ||
strings.Contains(err.Error(), "no such host") {
time.Sleep(1 * time.Second)
continue
}
return nil, err
}
if err != nil {
return nil, err
}
} else {
target = AerospikeFixedAddrFlag.Get()
}
ipPort := strings.Split(target, ":")
port, err := strconv.Atoi(ipPort[1])
if err != nil {
return nil, err
}
for retry := 0; retry < 15; retry++ {
as, err = aerospike.NewClient(ipPort[0], port)
if err == nil {
return as, nil
}
if strings.Contains(err.Error(), "Failed to connect") {
time.Sleep(1 * time.Second)
continue
}
return nil, err
}
return nil, err
}