Skip to content

Commit 7e3acd9

Browse files
authored
Merge pull request #1461 from breml/early-return
chore: Happy path on the left, early return
2 parents 5b62065 + b9f7d5b commit 7e3acd9

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

internal/server/db/query/retry.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,37 @@ const maxRetries = 250
2323
//
2424
// This should by typically used to wrap transactions.
2525
func Retry(ctx context.Context, f func(ctx context.Context) error) error {
26-
// TODO: the retry loop should be configurable.
2726
var err error
2827
for i := 0; i < maxRetries; i++ {
2928
err = f(ctx)
30-
if err != nil {
31-
if errors.Is(err, context.Canceled) {
32-
break
33-
}
34-
35-
// No point in re-trying or logging a no-row or not found error.
36-
if errors.Is(err, sql.ErrNoRows) || api.StatusErrorCheck(err, http.StatusNotFound) {
37-
break
38-
}
39-
40-
// Process actual errors.
41-
if IsRetriableError(err) {
42-
if i == maxRetries {
43-
logger.Warn("Database error, giving up", logger.Ctx{"attempt": i, "err": err})
44-
break
45-
}
46-
47-
logger.Debug("Database error, retrying", logger.Ctx{"attempt": i, "err": err})
48-
time.Sleep(jitter.Deviation(nil, 0.8)(100 * time.Millisecond))
49-
continue
50-
} else {
51-
logger.Debug("Database error", logger.Ctx{"err": err})
52-
}
29+
if err == nil {
30+
// The function succeeded, we're done here.
31+
break
5332
}
54-
break
33+
34+
if errors.Is(err, context.Canceled) {
35+
// The function was canceled, don't retry.
36+
break
37+
}
38+
39+
// No point in re-trying or logging a no-row or not found error.
40+
if errors.Is(err, sql.ErrNoRows) || api.StatusErrorCheck(err, http.StatusNotFound) {
41+
break
42+
}
43+
44+
// Process actual errors.
45+
if !IsRetriableError(err) {
46+
logger.Debug("Database error", logger.Ctx{"err": err})
47+
break
48+
}
49+
50+
if i == maxRetries {
51+
logger.Warn("Database error, giving up", logger.Ctx{"attempt": i, "err": err})
52+
break
53+
}
54+
55+
logger.Debug("Database error, retrying", logger.Ctx{"attempt": i, "err": err})
56+
time.Sleep(jitter.Deviation(nil, 0.8)(100 * time.Millisecond))
5557
}
5658

5759
return err

internal/util/network.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ func CanonicalNetworkAddress(address string, defaultPort int) string {
1717
if ip != nil {
1818
// If the input address is a bare IP address, then convert it to a proper listen address
1919
// using the canonical IP with default port and wrap IPv6 addresses in square brackets.
20-
address = net.JoinHostPort(ip.String(), fmt.Sprintf("%d", defaultPort))
21-
} else {
22-
// Otherwise assume this is either a host name or a partial address (e.g `[::]`) without
23-
// a port number, so append the default port.
24-
address = fmt.Sprintf("%s:%d", address, defaultPort)
20+
return net.JoinHostPort(ip.String(), fmt.Sprintf("%d", defaultPort))
2521
}
26-
} else if port == "" && address[len(address)-1] == ':' {
22+
23+
// Otherwise assume this is either a host name or a partial address (e.g `[::]`) without
24+
// a port number, so append the default port.
25+
return fmt.Sprintf("%s:%d", address, defaultPort)
26+
}
27+
28+
if port == "" && address[len(address)-1] == ':' {
2729
// An address that ends with a trailing colon will be parsed as having an empty port.
28-
address = net.JoinHostPort(host, fmt.Sprintf("%d", defaultPort))
30+
return net.JoinHostPort(host, fmt.Sprintf("%d", defaultPort))
2931
}
3032

3133
return address

0 commit comments

Comments
 (0)