Skip to content

Commit

Permalink
proxy: unexport clock
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Dec 18, 2023
1 parent 5cbb56e commit 7927132
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions proxy/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package proxy

import "time"

// Clock is the interface for provider of current time. It's used to simplify
// clock is the interface for provider of current time. It's used to simplify
// testing.
//
// TODO(e.burkov): Move to golibs.
type Clock interface {
type clock interface {
// Now returns the current local time.
Now() (now time.Time)
}

// type check
var _ Clock = RealClock{}
var _ clock = realClock{}

// RealClock is the [Clock] which actually uses the [time] package.
type RealClock struct{}
// realClock is the [clock] which actually uses the [time] package.
type realClock struct{}

// Now implements the [Clock] interface for RealClock.
func (RealClock) Now() (now time.Time) { return time.Now() }
// Now implements the [clock] interface for RealClock.
func (realClock) Now() (now time.Time) { return time.Now() }
2 changes: 1 addition & 1 deletion proxy/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *Proxy) exchangeUpstreams(
// exchange returns the result of the DNS request exchange with the given
// upstream and the elapsed time in milliseconds. It uses the given clock to
// measure the request duration.
func exchange(u upstream.Upstream, req *dns.Msg, c Clock) (resp *dns.Msg, dur time.Duration, err error) {
func exchange(u upstream.Upstream, req *dns.Msg, c clock) (resp *dns.Msg, dur time.Duration, err error) {
startTime := c.Now()

reply, err := u.Exchange(req)
Expand Down
8 changes: 4 additions & 4 deletions proxy/exchange_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
"golang.org/x/exp/rand"
)

// fakeClock is the fake implementation of the [Clock] interface.
// fakeClock is the function-based implementation of the [clock] interface.
type fakeClock struct {
onNow func() (now time.Time)
}

// type check
var _ Clock = (*fakeClock)(nil)
var _ clock = (*fakeClock)(nil)

// Now implements the [Clock] interface for *fakeClock.
// Now implements the [clock] interface for *fakeClock.
func (c *fakeClock) Now() (now time.Time) { return c.onNow() }

// newUpstreamWithErrorRate returns an [upstream.Upstream] that responds with an
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestProxy_Exchange_loadBalance(t *testing.T) {

testCases := []struct {
wantStat map[upstream.Upstream]int64
clock Clock
clock clock
name string
servers []upstream.Upstream
}{{
Expand Down
4 changes: 2 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type Proxy struct {
requestGoroutinesSema semaphore

// time provides the current time.
time Clock
time clock

// randSrc provides the source of randomness.
randSrc rand.Source
Expand Down Expand Up @@ -246,7 +246,7 @@ func (p *Proxy) Init() (err error) {
p.RatelimitWhitelist = slices.Clone(p.RatelimitWhitelist)
slices.SortFunc(p.RatelimitWhitelist, netip.Addr.Compare)

p.time = RealClock{}
p.time = realClock{}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ func createTestProxy(t *testing.T, tlsConfig *tls.Config) (p *Proxy) {
t.Helper()

p = &Proxy{
time: RealClock{},
time: realClock{},
}

if ip := net.ParseIP(listenIP); tlsConfig != nil {
Expand Down

0 comments on commit 7927132

Please sign in to comment.