This repository was archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcat.go
More file actions
80 lines (74 loc) · 2.08 KB
/
cat.go
File metadata and controls
80 lines (74 loc) · 2.08 KB
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
package composeaddresstranslator
import (
"encoding/json"
"fmt"
"net"
"strconv"
)
type ComposeAddressTranslator struct {
Map map[string]string
}
// New creates a ComposeAddressTranslator with the given translation map form.
// The keys and values for the translation map are the host with port.
// e.g.
// New(map[string]string{
// "127.0.0.1:9042": "192.168.1.1:1234",
// "127.0.0.2:9042": "192.168.1.2:5678",
// "127.0.0.3:9042": "192.168.1.3:9012",
// })
func New(m map[string]string) ComposeAddressTranslator {
return ComposeAddressTranslator{
Map: m,
}
}
// NewFromJSONString creates a ComposeAddressTranslator with the given translation map in JSON string form.
// e.g.
// NewFromJSONString(`{
// "127.0.0.1:9042": "192.168.1.1:1234",
// "127.0.0.2:9042": "192.168.1.2:5678",
// "127.0.0.3:9042": "192.168.1.3:9012"
// }`)
func NewFromJSONString(s string) (ComposeAddressTranslator, error) {
cat := ComposeAddressTranslator{
Map: make(map[string]string),
}
err := json.Unmarshal([]byte(s), &cat.Map)
return cat, err
}
// Translate implements the AddressTranslator interface for the gocql driver.
// In the case that the translation map contains
func (cat ComposeAddressTranslator) Translate(addr net.IP, port int) (net.IP, int) {
if host, ok := cat.Map[fmt.Sprintf("%s:%s", addr.String(), strconv.Itoa(port))]; ok {
a, p, err := net.SplitHostPort(host)
if err != nil {
return addr, port
}
port, err = strconv.Atoi(p)
if err != nil {
return addr, port
}
// See if it was passed as IP addresses
ip := net.ParseIP(a)
if ip != nil {
return ip, port
}
// We have a hostname, do DNS lookup and use first response (gocql makes same assumption
// about multiple A records)
ips, err := net.LookupIP(a)
if err != nil || len(ips) < 1 {
return addr, port
}
return ips[0], port
}
return addr, port
}
func (cat ComposeAddressTranslator) ContactPoints() []string {
s := make([]string, len(cat.Map))
i := 0
// Return the _internal_ IP addresses as gocql seems to expect these
for h := range cat.Map {
s[i] = h
i++
}
return s
}