Skip to content

Commit

Permalink
feat(checker): change http to use round tripper (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfalkowski authored Aug 7, 2024
1 parent c34556e commit 681d6f9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 33 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
vendor
*.prof
*.cov
*.out
test/reports/*.cov
test/reports/*.xml
4 changes: 2 additions & 2 deletions checker/db_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

// NewDBChecker for SQL.
func NewDBChecker(pinger sql.Pinger, timeout time.Duration) *DBChecker {
return &DBChecker{pinger: pinger, timeout: timeout}
func NewDBChecker(pinger sql.Pinger, t time.Duration) *DBChecker {
return &DBChecker{pinger: pinger, timeout: timeout(t)}
}

// DBChecker for SQL.
Expand Down
5 changes: 3 additions & 2 deletions checker/http_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"errors"
"fmt"
"net/http"
"time"
)

// ErrInvalidStatusCode when we have errors that are not in the 200 range.
var ErrInvalidStatusCode = errors.New("invalid status code")

// NewHTTPChecker with URL and client.
func NewHTTPChecker(url string, client *http.Client) *HTTPChecker {
return &HTTPChecker{url: url, client: client}
func NewHTTPChecker(url string, rt http.RoundTripper, t time.Duration) *HTTPChecker {
return &HTTPChecker{url: url, client: &http.Client{Transport: rt, Timeout: timeout(t)}}
}

// HTTPChecker for a URL.
Expand Down
10 changes: 5 additions & 5 deletions checker/tcp_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package checker
import (
"context"
"fmt"
snet "net"
"net"
"time"

"github.com/alexfalkowski/go-health/net"
hn "github.com/alexfalkowski/go-health/net"
)

// NewTCPChecker for address.
func NewTCPChecker(address string, timeout time.Duration) *TCPChecker {
return NewTCPCheckerWithDialer(address, timeout, &snet.Dialer{})
return NewTCPCheckerWithDialer(address, timeout, &net.Dialer{})
}

// NewTCPCheckerWithDialer for address.
func NewTCPCheckerWithDialer(address string, timeout time.Duration, dialer net.Dialer) *TCPChecker {
func NewTCPCheckerWithDialer(address string, timeout time.Duration, dialer hn.Dialer) *TCPChecker {
return &TCPChecker{address: address, timeout: timeout, dialer: dialer}
}

// TCPChecker for an address.
type TCPChecker struct {
dialer net.Dialer
dialer hn.Dialer
address string
timeout time.Duration
}
Expand Down
13 changes: 13 additions & 0 deletions checker/timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package checker

import (
"time"
)

func timeout(t time.Duration) time.Duration {
if t == 0 {
t = 30 * time.Second
}

return t
}
66 changes: 45 additions & 21 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
. "github.com/smartystreets/goconvey/convey" //nolint:revive
)

const google = "google"

func defaultTimeout() time.Duration {
return 2 * time.Second
}
Expand All @@ -29,13 +31,12 @@ func TestDoubleStart(t *testing.T) {
s := server.NewServer()
defer s.Stop()

name := "google"
checker := checker.NewHTTPChecker("https://www.google.com/", &http.Client{Timeout: defaultTimeout()})
r := server.NewRegistration(name, defaultPeriod(), checker)
checker := checker.NewHTTPChecker("https://www.google.com/", http.DefaultTransport, defaultTimeout())
r := server.NewRegistration(google, defaultPeriod(), checker)

s.Register(r)

ob := s.Observe(name)
ob := s.Observe(google)

Convey("When I start the server", func() {
s.Start()
Expand All @@ -55,13 +56,12 @@ func TestValidHTTPChecker(t *testing.T) {
s := server.NewServer()
defer s.Stop()

name := "google"
checker := checker.NewHTTPChecker("https://www.google.com/", &http.Client{Timeout: defaultTimeout()})
r := server.NewRegistration(name, defaultPeriod(), checker)
checker := checker.NewHTTPChecker("https://www.google.com/", http.DefaultTransport, 0)
r := server.NewRegistration(google, defaultPeriod(), checker)

s.Register(r)

ob := s.Observe(name)
ob := s.Observe(google)

Convey("When I start the server", func() {
s.Start()
Expand All @@ -83,7 +83,7 @@ func TestInvalidURLHTTPChecker(t *testing.T) {
defer s.Stop()

name := "assaaasss"
checker := checker.NewHTTPChecker("https://www.assaaasss.com/", &http.Client{Timeout: defaultTimeout()})
checker := checker.NewHTTPChecker("https://www.assaaasss.com/", http.DefaultTransport, defaultTimeout())
r := server.NewRegistration(name, defaultPeriod(), checker)

s.Register(r)
Expand All @@ -102,13 +102,13 @@ func TestInvalidURLHTTPChecker(t *testing.T) {
})
}

func TestMallformedURLHTTPChecker(t *testing.T) {
func TestMalformedURLHTTPChecker(t *testing.T) {
Convey("Given we have a new server", t, func() {
s := server.NewServer()
defer s.Stop()

name := "assaaasss"
checker := checker.NewHTTPChecker(string([]byte{0x7f}), &http.Client{Timeout: defaultTimeout()})
checker := checker.NewHTTPChecker(string([]byte{0x7f}), http.DefaultTransport, defaultTimeout())
r := server.NewRegistration(name, defaultPeriod(), checker)

s.Register(r)
Expand All @@ -132,8 +132,8 @@ func TestInvalidCodeHTTPChecker(t *testing.T) {
s := server.NewServer()
defer s.Stop()

name := "httpstat400"
checker := checker.NewHTTPChecker("http://localhost:6000/v1/status/400", &http.Client{Timeout: defaultTimeout()})
name := "http400"
checker := checker.NewHTTPChecker("http://localhost:6000/v1/status/400", http.DefaultTransport, defaultTimeout())
r := server.NewRegistration(name, defaultPeriod(), checker)

s.Register(r)
Expand All @@ -157,9 +157,9 @@ func TestTimeoutHTTPChecker(t *testing.T) {
s := server.NewServer()
defer s.Stop()

name := "httpstat200"
c := &http.Client{Timeout: defaultTimeout()}
checker := checker.NewHTTPChecker("http://localhost:6000/v1/status/200?sleep=5s", c)
name := "http200"
checker := checker.NewHTTPChecker("http://localhost:6000/v1/status/200?sleep=5s",
http.DefaultTransport, defaultTimeout())
r := server.NewRegistration(name, defaultPeriod(), checker)

s.Register(r)
Expand Down Expand Up @@ -259,7 +259,6 @@ func TestValidDBChecker(t *testing.T) {
})
}

//nolint:goerr113
func TestValidReadyChecker(t *testing.T) {
Convey("Given we have a new server", t, func() {
s := server.NewServer()
Expand Down Expand Up @@ -322,7 +321,7 @@ func TestInvalidObserver(t *testing.T) {
s := server.NewServer()
defer s.Stop()

cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/400", &http.Client{Timeout: defaultTimeout()})
cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/400", http.DefaultTransport, defaultTimeout())
hr := server.NewRegistration("http1", defaultPeriod(), cc)
tc := checker.NewTCPChecker("httpstat.us:9000", defaultTimeout())
tr := server.NewRegistration("tcp1", defaultPeriod(), tc)
Expand All @@ -348,7 +347,7 @@ func TestValidObserver(t *testing.T) {
s := server.NewServer()
defer s.Stop()

cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/200", &http.Client{Timeout: defaultTimeout()})
cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/200", http.DefaultTransport, defaultTimeout())
hr := server.NewRegistration("http", defaultPeriod(), cc)
tc := checker.NewTCPChecker("httpstat.us:80", defaultTimeout())
tr := server.NewRegistration("tcp", defaultPeriod(), tc)
Expand All @@ -374,7 +373,7 @@ func TestOneInvalidObserver(t *testing.T) {
s := server.NewServer()
defer s.Stop()

cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/500", &http.Client{Timeout: defaultTimeout()})
cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/500", http.DefaultTransport, defaultTimeout())
hr := server.NewRegistration("http", defaultPeriod(), cc)
tc := checker.NewTCPChecker("httpstat.us:80", defaultTimeout())
tr := server.NewRegistration("tcp", defaultPeriod(), tc)
Expand All @@ -400,7 +399,7 @@ func TestNonExistentObserver(t *testing.T) {
s := server.NewServer()
defer s.Stop()

cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/200", &http.Client{Timeout: defaultTimeout()})
cc := checker.NewHTTPChecker("http://localhost:6000/v1/status/200", http.DefaultTransport, defaultTimeout())
hr := server.NewRegistration("http", defaultPeriod(), cc)
tc := checker.NewTCPChecker("httpstat.us:80", defaultTimeout())
tr := server.NewRegistration("tcp", defaultPeriod(), tc)
Expand All @@ -416,3 +415,28 @@ func TestNonExistentObserver(t *testing.T) {
})
})
}

func BenchmarkValidHTTPChecker(b *testing.B) {
b.ReportAllocs()

s := server.NewServer()
defer s.Stop()

checker := checker.NewHTTPChecker("https://www.google.com/", http.DefaultTransport, defaultPeriod())

r := server.NewRegistration(google, defaultPeriod(), checker)
s.Register(r)

ob := s.Observe(google)

s.Start()
time.Sleep(defaultWait())

b.Run(google, func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := ob.Error(); err != nil {
b.Fail()
}
}
})
}

0 comments on commit 681d6f9

Please sign in to comment.