-
Notifications
You must be signed in to change notification settings - Fork 2k
/
instance_key.go
98 lines (84 loc) · 2.96 KB
/
instance_key.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
/*
Copyright 2015 Shlomi Noach, courtesy Booking.com
See https://github.com/github/freno/blob/master/LICENSE
*/
package mysql
import (
"fmt"
"strconv"
"strings"
)
// InstanceKey is an instance indicator, identified by hostname and port
type InstanceKey struct {
Hostname string
Port int
}
// SelfInstanceKey is a special indicator for "this instance", e.g. denoting the MySQL server associated with local tablet
// The values of this key are immaterial and are intentionally descriptive
var SelfInstanceKey = &InstanceKey{Hostname: "(self)", Port: 1}
// newRawInstanceKey will parse an InstanceKey from a string representation such as 127.0.0.1:3306
// It expects such format and returns with error if input differs in format
func newRawInstanceKey(hostPort string) (*InstanceKey, error) {
tokens := strings.SplitN(hostPort, ":", 2)
if len(tokens) != 2 {
return nil, fmt.Errorf("Cannot parse InstanceKey from %s. Expected format is host:port", hostPort)
}
instanceKey := &InstanceKey{Hostname: tokens[0]}
var err error
if instanceKey.Port, err = strconv.Atoi(tokens[1]); err != nil {
return instanceKey, fmt.Errorf("Invalid port: %s", tokens[1])
}
return instanceKey, nil
}
// ParseInstanceKey will parse an InstanceKey from a string representation such as 127.0.0.1:3306 or some.hostname
// `defaultPort` is used if `hostPort` does not include a port.
func ParseInstanceKey(hostPort string, defaultPort int) (*InstanceKey, error) {
if !strings.Contains(hostPort, ":") {
return &InstanceKey{Hostname: hostPort, Port: defaultPort}, nil
}
return newRawInstanceKey(hostPort)
}
// Equals tests equality between this key and another key
func (i *InstanceKey) Equals(other *InstanceKey) bool {
if other == nil {
return false
}
return i.Hostname == other.Hostname && i.Port == other.Port
}
// SmallerThan returns true if this key is dictionary-smaller than another.
// This is used for consistent sorting/ordering; there's nothing magical about it.
func (i *InstanceKey) SmallerThan(other *InstanceKey) bool {
if i.Hostname < other.Hostname {
return true
}
if i.Hostname == other.Hostname && i.Port < other.Port {
return true
}
return false
}
// IsValid uses simple heuristics to see whether this key represents an actual instance
func (i *InstanceKey) IsValid() bool {
if i.Hostname == "_" {
return false
}
return len(i.Hostname) > 0 && i.Port > 0
}
// IsSelf checks if this is the special "self" instance key
func (i *InstanceKey) IsSelf() bool {
if SelfInstanceKey == i {
return true
}
return SelfInstanceKey.Equals(i)
}
// StringCode returns an official string representation of this key
func (i *InstanceKey) StringCode() string {
return fmt.Sprintf("%s:%d", i.Hostname, i.Port)
}
// DisplayString returns a user-friendly string representation of this key
func (i *InstanceKey) DisplayString() string {
return i.StringCode()
}
// String returns a user-friendly string representation of this key
func (i InstanceKey) String() string {
return i.StringCode()
}