Skip to content

Commit c947e7c

Browse files
committed
Fail when cannot resolve IPs and replace format statements with logrus
Signed-off-by: Doğukan Teber <[email protected]>
1 parent a140bef commit c947e7c

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

gateway/load_balancer.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package gateway
22

33
import (
44
"fmt"
5-
"log"
65
"net"
76
"net/http"
87
"strings"
98
"sync"
109
"time"
10+
11+
"github.com/sirupsen/logrus"
1112
)
1213

1314
type DNSResolver interface {
@@ -29,7 +30,7 @@ type roundRobinLoadBalancer struct {
2930
sync.RWMutex
3031
}
3132

32-
func newRoundRobinLoadBalancer(hostname string, resolver func(hostname string) ([]net.IP, error)) *roundRobinLoadBalancer {
33+
func newRoundRobinLoadBalancer(hostname string, resolver func(hostname string) ([]net.IP, error)) (*roundRobinLoadBalancer, error) {
3334
lb := &roundRobinLoadBalancer{
3435
hostname: hostname,
3536
transport: http.DefaultTransport,
@@ -39,21 +40,23 @@ func newRoundRobinLoadBalancer(hostname string, resolver func(hostname string) (
3940
// Resolve IPs initially
4041
ips, err := lb.resolveIPs(hostname)
4142
if err != nil {
42-
log.Printf("Failed to resolve IPs for hostname %s: %v", lb.hostname, err)
43+
logrus.Errorf("failed to resolve IPs for hostname %s: %v", lb.hostname, err)
44+
return nil, err
4345
} else {
4446
lb.ips = ipsToStrings(ips)
4547
}
4648

47-
return lb
49+
return lb, nil
4850
}
4951

5052
func (lb *roundRobinLoadBalancer) roundTrip(req *http.Request) (*http.Response, error) {
5153
lb.Lock()
5254
defer lb.Unlock()
5355

5456
if len(lb.ips) == 0 {
55-
// TODO: replace format error with a log statement
56-
return nil, fmt.Errorf("no IP addresses available")
57+
errMsg := fmt.Sprintln("no IP addresses available")
58+
logrus.Errorf(errMsg)
59+
return nil, fmt.Errorf(errMsg)
5760
}
5861

5962
ip := lb.getNextIP()
@@ -79,8 +82,7 @@ func (lb *roundRobinLoadBalancer) refreshIPs(refreshInterval time.Duration) {
7982
for {
8083
ips, err := lb.resolveIPs(lb.hostname)
8184
if err != nil {
82-
// TODO: replace std library log package with logrus
83-
log.Printf("Failed to resolve IPs for hostname %s: %v", lb.hostname, err)
85+
logrus.Errorf("failed to resolve IPs for hostname %s: %v", lb.hostname, err)
8486
} else {
8587
lb.Lock()
8688
lb.ips = ipsToStrings(ips)

gateway/load_balancer_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ func TestDistribution(t *testing.T) {
113113
Err: nil,
114114
}
115115

116-
lb := newRoundRobinLoadBalancer(hostname, mockResolver.LookupIP)
116+
lb, err := newRoundRobinLoadBalancer(hostname, mockResolver.LookupIP)
117+
if err != nil {
118+
t.Fatal(err)
119+
}
117120
lb.transport = &customRoundTripper{}
118121

119122
go lb.refreshIPs(tc.refreshInterval)

gateway/proxy.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewProxy(targetURL string, upstream Upstream, component string) (*Proxy, er
6767
return nil, err
6868
}
6969
if url.Scheme == "" {
70-
return nil, fmt.Errorf("invalid URL scheme: %s", targetURL)
70+
return nil, fmt.Errorf("invalid URL scheme when creating a proxy for the %s: %s", component, targetURL)
7171
}
7272

7373
reverseProxy := httputil.NewSingleHostReverseProxy(url)
@@ -114,14 +114,18 @@ func customTransport(component string, upstream Upstream) http.RoundTripper {
114114

115115
url, err := url.Parse(upstream.URL)
116116
if err != nil {
117-
logrus.Errorf("unexpected error when parsing the upstream url: %s", err)
117+
logrus.Errorf("unexpected error when parsing the upstream url: %v", err)
118118
os.Exit(1)
119119
}
120120

121121
resolver := DefaultDNSResolver{}
122+
lb, err := newRoundRobinLoadBalancer(url.Hostname(), resolver.LookupIP)
123+
if err != nil {
124+
logrus.Errorf("unexpected error when creating the load balancer: %v", err)
125+
}
122126
t := &CustomTransport{
123127
Transport: *http.DefaultTransport.(*http.Transport).Clone(),
124-
lb: newRoundRobinLoadBalancer(url.Hostname(), resolver.LookupIP),
128+
lb: lb,
125129
}
126130
go t.lb.refreshIPs(upstream.DNSRefreshInterval)
127131

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.20
55
require (
66
github.com/go-kit/log v0.2.1
77
github.com/prometheus/client_golang v1.14.0
8+
github.com/sirupsen/logrus v1.6.0
89
github.com/stretchr/testify v1.8.2
910
gopkg.in/yaml.v2 v2.4.0
1011
)
@@ -16,12 +17,13 @@ require (
1617
github.com/go-logfmt/logfmt v0.5.1 // indirect
1718
github.com/golang/protobuf v1.5.2 // indirect
1819
github.com/google/go-cmp v0.5.9 // indirect
20+
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
1921
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
2022
github.com/pmezard/go-difflib v1.0.0 // indirect
2123
github.com/prometheus/client_model v0.3.0 // indirect
2224
github.com/prometheus/common v0.37.0 // indirect
2325
github.com/prometheus/procfs v0.8.0 // indirect
24-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
26+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
2527
google.golang.org/protobuf v1.28.1 // indirect
2628
gopkg.in/yaml.v3 v3.0.1 // indirect
2729
)

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
113113
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
114114
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
115115
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
116-
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
117116
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
118117
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
119118
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -143,6 +142,7 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
143142
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
144143
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
145144
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
145+
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
146146
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
147147
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
148148
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -193,6 +193,7 @@ github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0ua
193193
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
194194
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
195195
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
196+
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
196197
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
197198
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
198199
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -334,8 +335,8 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
334335
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
335336
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
336337
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
337-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
338-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
338+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
339+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
339340
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
340341
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
341342
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)