Skip to content

Commit 3c880b6

Browse files
authored
Merge pull request #45 from gatewayd-io/return-errors
Return errors
2 parents 3c527fe + 485343a commit 3c880b6

File tree

5 files changed

+94
-28
lines changed

5 files changed

+94
-28
lines changed

.golangci.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
run:
2+
timeout: 5m
3+
tests: false
14
linters:
25
enable-all: true
36
disable:
@@ -20,5 +23,3 @@ linters:
2023
- musttag
2124
- nosnakecase
2225
- wrapcheck
23-
run:
24-
tests: false

plugin/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package plugin
2+
3+
import "errors"
4+
5+
var ErrInvalidAddressPortPair = errors.New("invalid address:port pair")

plugin/scheduler.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,43 @@ func (p *Plugin) PeriodicInvalidator() {
2121
p.Logger.Trace("Got proxies from GatewayD", "proxies", proxies)
2222

2323
// Get all the client keys and delete the ones that are not valid.
24+
// TODO: use scan instead of keys.
2425
for _, address := range p.RedisClient.Keys(context.Background(), "*:*").Val() {
25-
if validateAddressPort(address) || validateHostPort(address) {
26-
// If the connection is busy, it is not safe to delete the key.
27-
if isBusy(proxies, address) {
28-
p.Logger.Trace("Skipping connection because it is busy", "address", address)
26+
valid := false
27+
28+
// Validate the address if the address is an IP address.
29+
if ok, err := validateAddressPort(address); ok && err == nil {
30+
valid = true
31+
} else {
32+
p.Logger.Trace(
33+
"Skipping connection because it is invalid", "address", address, "error", err)
34+
}
35+
36+
if !valid {
37+
// Validate the address if the address is a hostname.
38+
if ok, err := validateHostPort(address); ok && err == nil {
39+
valid = true
40+
} else {
41+
p.Logger.Trace(
42+
"Skipping connection because it is invalid", "address", address, "error", err)
2943
continue
3044
}
45+
}
3146

32-
p.RedisClient.Del(context.Background(), address)
33-
p.Logger.Trace("Deleted address", "address", address)
34-
CacheDeletesCounter.Inc()
47+
// If the address is not valid, skip it.
48+
if !valid {
49+
continue
3550
}
51+
52+
// If the connection is busy (a client is connected), it is not safe to delete the key.
53+
if isBusy(proxies, address) {
54+
p.Logger.Trace("Skipping connection because it is busy", "address", address)
55+
continue
56+
}
57+
58+
p.RedisClient.Del(context.Background(), address)
59+
p.Logger.Trace("Deleted stale address", "address", address)
60+
CacheDeletesCounter.Inc()
3661
}
3762
}); err != nil {
3863
p.Logger.Error("Failed to start periodic invalidator",

plugin/utils.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,52 +152,53 @@ func validateIP(ip net.IP) bool {
152152
}
153153

154154
// validateAddressPort validates an address:port string.
155-
func validateAddressPort(addressPort string) bool {
155+
func validateAddressPort(addressPort string) (bool, error) {
156156
// Split the address and port.
157+
// TODO: Support IPv6.
157158
data := strings.Split(strings.TrimSpace(addressPort), ":")
158159
if len(data) != AddressPortPairLength {
159-
return false
160+
return false, ErrInvalidAddressPortPair
160161
}
161162

162163
// Validate the port.
163164
port, err := strconv.ParseUint(data[1], 10, 16)
164165
if err != nil {
165-
return false
166+
return false, err
166167
}
167168

168169
// Resolve the IP address, if it is a host.
169170
ipAddress, err := net.ResolveIPAddr("ip", data[0])
170171
if err != nil {
171-
return false
172+
return false, err
172173
}
173174

174175
// Validate the IP address and port.
175176
if (validateIP(net.ParseIP(data[0])) || validateIP(ipAddress.IP)) && (port > 0 && port <= 65535) {
176-
return true
177+
return true, nil
177178
}
178179

179-
return false
180+
return false, nil
180181
}
181182

182183
// validateHostPort validates a host:port string.
183184
// TODO: Add support for IPv6.
184-
func validateHostPort(hostPort string) bool {
185+
func validateHostPort(hostPort string) (bool, error) {
185186
data := strings.Split(hostPort, ":")
186187
if len(data) != AddressPortPairLength {
187-
return false
188+
return false, ErrInvalidAddressPortPair
188189
}
189190

190191
port, err := strconv.ParseUint(data[1], 10, 16)
191192
if err != nil {
192-
return false
193+
return false, err
193194
}
194195

195196
// FIXME: There is not much to validate on the host side.
196197
if data[0] != "" && port > 0 && port <= 65535 {
197-
return true
198+
return true, nil
198199
}
199200

200-
return false
201+
return false, nil
201202
}
202203

203204
// isBusy checks if a client address exists in cache by matching the address

plugin/utils_test.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,51 @@ func Test_GetTablesFromQuery_AlterMultiple(t *testing.T) {
283283
}
284284

285285
func Test_validateAddressPort_Hostname(t *testing.T) {
286-
assert.True(t, validateAddressPort("localhost:5432"))
287-
assert.True(t, validateAddressPort(" localhost:5432"))
288-
assert.True(t, validateAddressPort("localhost:5432 "))
289-
assert.True(t, validateAddressPort(" localhost:5432 "))
286+
valid, err := validateAddressPort("localhost:5432")
287+
assert.True(t, valid)
288+
assert.Nil(t, err)
289+
290+
valid, err = validateAddressPort(" localhost:5432")
291+
assert.True(t, valid)
292+
assert.Nil(t, err)
293+
294+
valid, err = validateAddressPort("localhost:5432 ")
295+
assert.True(t, valid)
296+
assert.Nil(t, err)
297+
298+
valid, err = validateAddressPort(" localhost:5432 ")
299+
assert.True(t, valid)
300+
assert.Nil(t, err)
301+
}
302+
303+
func Test_validateAddressPort_Fails(t *testing.T) {
304+
valid, err := validateAddressPort("localhost")
305+
assert.False(t, valid)
306+
assert.NotNil(t, err)
307+
}
308+
309+
func Test_validateHostPort_Fails(t *testing.T) {
310+
valid, err := validateHostPort("127.0.0.1")
311+
assert.False(t, valid)
312+
assert.NotNil(t, err)
290313
}
291314

292315
func Test_validateAddressPort_IPv4(t *testing.T) {
293-
assert.True(t, validateAddressPort("127.0.0.1:5432"))
294-
assert.True(t, validateAddressPort(" 127.0.0.1:5432"))
295-
assert.True(t, validateAddressPort("127.0.0.1:5432 "))
296-
assert.True(t, validateAddressPort(" 127.0.0.1:5432 "))
316+
valid, err := validateAddressPort("127.0.0.1:5432")
317+
assert.True(t, valid)
318+
assert.Nil(t, err)
319+
320+
valid, err = validateAddressPort(" 127.0.0.1:5432")
321+
assert.True(t, valid)
322+
assert.Nil(t, err)
323+
324+
valid, err = validateAddressPort("127.0.0.1:5432 ")
325+
assert.True(t, valid)
326+
assert.Nil(t, err)
327+
328+
valid, err = validateAddressPort(" 127.0.0.1:5432 ")
329+
assert.True(t, valid)
330+
assert.Nil(t, err)
297331
}
298332

299333
func Test_isBusy(t *testing.T) {

0 commit comments

Comments
 (0)