-
Notifications
You must be signed in to change notification settings - Fork 1
/
address.go
177 lines (147 loc) · 4.37 KB
/
address.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
169
170
171
172
173
174
175
176
177
package fb
import (
"context"
"fmt"
"strconv"
"strings"
)
// Address is a string of the form [scheme]://[host]:[port]/[path]
type Address string
// String returns the Address as a string type.
func (a Address) String() string {
return string(a)
}
// Scheme returns the [scheme] portion of the Address. This may be an empty
// string if Address does not contain a scheme.
func (a Address) Scheme() string {
return parse(a).scheme
}
// HostPort returns the [host]:[port] portion of the Address; in other words,
// the Address stripped of any scheme and path.
func (a Address) HostPort() string {
return parse(a).hostPort()
}
// Host returns the [host] portion of the Address.
func (a Address) Host() string {
return parse(a).host
}
// Port returns the [port] portion of the Address. If the port values is invalid
// or does not exist, the returned value will default to 0.
func (a Address) Port() uint16 {
return parse(a).port
}
// Path returns the [path] portion of the Address.
func (a Address) Path() string {
return parse(a).path
}
// OverrideScheme overrides Address's current scheme with the one provided. If
// an empty scheme is provided, OverrideScheme will return just the
// host:port/path.
func (a Address) OverrideScheme(scheme string) string {
addr := parse(a)
if scheme == "" {
return addr.hostPortPath()
}
return scheme + "://" + addr.hostPortPath()
}
// WithScheme ensures that the string returned contains the scheme portion of a
// URL. Because an Address may not have a scheme (for example, it could be just
// "host:80"), this method can be applied to an address when it needs to be used
// as a URL. If the address's existing scheme is blank, the default scheme
// provided will be used. If address is blank, the default scheme will not be
// added; i.e. address will remain blank.
func (a Address) WithScheme(dflt string) string {
// If address is empty, don't add a scheme to it.
if a == "" {
return ""
}
addr := parse(a)
if addr.scheme != "" {
return a.String()
}
return dflt + "://" + addr.hostPortPath()
}
type addr struct {
scheme string
host string
port uint16
path string
}
// parse breaks the address up into scheme://host:port. It currently assumes
// that very rigid structure; in other words, if an address does not follow that
// format, return values may be unexpected.
func parse(a Address) addr {
var scheme string
var host string
var port uint16
var path string
aStr := string(a)
var hostPortPath string
if parts := strings.Split(aStr, "://"); len(parts) > 1 {
scheme = parts[0]
hostPortPath = parts[1]
} else {
hostPortPath = aStr
}
var hostPort string
if parts := strings.SplitN(hostPortPath, "/", 2); len(parts) == 2 {
hostPort = parts[0]
path = parts[1]
} else {
hostPort = parts[0]
}
if parts := strings.Split(hostPort, ":"); len(parts) == 2 {
host = parts[0]
portStr := parts[1]
port64, err := strconv.ParseInt(portStr, 10, 32)
if err == nil {
port = uint16(port64)
}
} else {
host = hostPort
}
return addr{
scheme: scheme,
host: host,
port: port,
path: path,
}
}
func (a addr) hostPort() string {
if a.port == 0 {
return a.host
}
return fmt.Sprintf("%s:%d", a.host, a.port)
}
func (a addr) hostPortPath() string {
ret := ""
if a.port == 0 {
ret = a.host
} else {
ret = fmt.Sprintf("%s:%d", a.host, a.port)
}
if a.path != "" {
ret += "/" + a.path
}
return ret
}
// Addresses is a sortable slice of Address.
type Addresses []Address
func (a Addresses) Len() int { return len(a) }
func (a Addresses) Less(i, j int) bool { return a[i] < a[j] }
func (a Addresses) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// AddressManager is an interface for any service which needs to maintain a list
// of addresses, and receive add/remove address requests from other services.
type AddressManager interface {
AddAddresses(context.Context, ...Address) error
RemoveAddresses(context.Context, ...Address) error
}
// Ensure type implements interface.
var _ AddressManager = &NopAddressManager{}
// NopAddressManager is a no-op implementation of the AddressManager interface.
type NopAddressManager struct{}
func NewNopAddressManager() *NopAddressManager {
return &NopAddressManager{}
}
func (a *NopAddressManager) AddAddresses(ctx context.Context, addrs ...Address) error { return nil }
func (a *NopAddressManager) RemoveAddresses(ctx context.Context, addrs ...Address) error { return nil }