-
Notifications
You must be signed in to change notification settings - Fork 13
/
elasticache.go
168 lines (133 loc) · 3.57 KB
/
elasticache.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package elasticache
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"strings"
"github.com/bradfitz/gomemcache/memcache"
"github.com/integralist/go-findroot/find"
)
// Node is a single ElastiCache node
type Node struct {
URL string
Host string
IP string
Port int
}
// Item embeds the memcache client's type of the same name
type Item memcache.Item
// Client embeds the memcache client so we can hide those details away
type Client struct {
*memcache.Client
}
// Set abstracts the memcache client details away,
// by copying over the values provided by the user into the Set method,
// as coercing the custom Item type to the required memcache.Item type isn't possible.
// Downside is if memcache client fields ever change, it'll introduce a break
func (c *Client) Set(item *Item) error {
return c.Client.Set(&memcache.Item{
Key: item.Key,
Value: item.Value,
Expiration: item.Expiration,
})
}
var logger *log.Logger
func init() {
logger = log.New(os.Stdout, "go-elasticache: ", log.Ldate|log.Ltime|log.Lshortfile)
if env := os.Getenv("APP_ENV"); env == "test" {
root, err := find.Repo()
if err != nil {
log.Printf("Repo Error: %s", err.Error())
}
path := fmt.Sprintf("%s/go-elasticache.log", root.Path)
file, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Printf("Open File Error: %s", err.Error())
}
logger = log.New(file, "go-elasticache: ", log.Ldate|log.Ltime|log.Lshortfile)
}
}
// New returns an instance of the memcache client
func New() (*Client, error) {
urls, err := clusterNodes()
if err != nil {
return &Client{Client: memcache.New()}, err
}
return &Client{Client: memcache.New(urls...)}, nil
}
func clusterNodes() ([]string, error) {
endpoint, err := elasticache()
if err != nil {
return nil, err
}
conn, err := net.Dial("tcp", endpoint)
if err != nil {
logger.Printf("Socket Dial (%s): %s", endpoint, err.Error())
return nil, err
}
defer conn.Close()
command := "config get cluster\r\n"
fmt.Fprintf(conn, command)
response, err := parseNodes(conn)
if err != nil {
return nil, err
}
urls, err := parseURLs(response)
if err != nil {
return nil, err
}
return urls, nil
}
func elasticache() (string, error) {
var endpoint string
endpoint = os.Getenv("ELASTICACHE_ENDPOINT")
if len(endpoint) == 0 {
logger.Println("ElastiCache endpoint not set")
return "", errors.New("ElastiCache endpoint not set")
}
return endpoint, nil
}
func parseNodes(conn io.Reader) (string, error) {
var response string
count := 0
location := 3 // AWS docs suggest that nodes will always be listed on line 3
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
count++
if count == location {
response = scanner.Text()
}
if scanner.Text() == "END" {
break
}
}
if err := scanner.Err(); err != nil {
logger.Println("Scanner: ", err.Error())
return "", err
}
logger.Println("ElastiCache nodes found: ", response)
return response, nil
}
func parseURLs(response string) ([]string, error) {
var urls []string
var nodes []Node
items := strings.Split(response, " ")
for _, v := range items {
fields := strings.Split(v, "|") // ["host", "ip", "port"]
port, err := strconv.Atoi(fields[2])
if err != nil {
logger.Println("Integer conversion: ", err.Error())
return nil, err
}
node := Node{fmt.Sprintf("%s:%d", fields[1], port), fields[0], fields[1], port}
nodes = append(nodes, node)
urls = append(urls, node.URL)
logger.Printf("Host: %s, IP: %s, Port: %d, URL: %s", node.Host, node.IP, node.Port, node.URL)
}
return urls, nil
}