-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaerospike.go
70 lines (55 loc) · 1.67 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
69
70
package main
import (
"fmt"
aerospike "github.com/aerospike/aerospike-client-go"
"sync"
"time"
)
type AerospikeStorage struct {
Client *aerospike.Client
Namespace string
}
type AeroResponse struct {
Bins *aerospike.BinMap `json:"bins"`
Generation uint32 `json:"version"`
Ttl uint32 `json:"ttl"`
PrimaryKey string `json:"pk"`
}
var aerospike_connection_lock sync.Mutex
var aerospike_storage *AerospikeStorage
func RecordToAeroResponse(record *aerospike.Record) *AeroResponse {
var response AeroResponse
response = AeroResponse{
Bins: &record.Bins,
Generation: record.Generation,
Ttl: record.Expiration}
if record.Key.Value() != nil {
response.PrimaryKey = record.Key.Value().String()
}
return &response
}
func InitAerospikeClient() Storage {
var Hosts []*aerospike.Host
var storage *AerospikeStorage
var err error
defer aerospike_connection_lock.Unlock()
aerospike_connection_lock.Lock()
if aerospike_storage == nil {
storage = new(AerospikeStorage)
for _, cfg := range config.Aerospike.Hosts {
Hosts = append(Hosts, aerospike.NewHost(cfg.Host, cfg.Port))
}
policy := aerospike.NewClientPolicy()
if config.Aerospike.ConnectionQueueSize > 0 {
policy.ConnectionQueueSize = config.Aerospike.ConnectionQueueSize
}
if config.Aerospike.ConnectionTimeout > 0 {
policy.Timeout = time.Duration(config.Aerospike.ConnectionTimeout) * time.Millisecond
}
if storage.Client, err = aerospike.NewClientWithPolicyAndHost(policy, Hosts...); err != nil {
panic(fmt.Sprintf("Unable to connect to aerospike (%s)\n", err))
}
aerospike_storage = storage
}
return aerospike_storage
}