Skip to content

Commit

Permalink
fix: misc (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Aug 24, 2023
2 parents 05589b7 + 6d550af commit d4ce3f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
10 changes: 4 additions & 6 deletions net/http/realip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package httpz_test
import (
"bytes"
"context"
"net"
"net/http"
"net/http/httptest"
"testing"

netz "github.com/kunitsucom/util.go/net"
httpz "github.com/kunitsucom/util.go/net/http"
)

Expand Down Expand Up @@ -36,7 +34,7 @@ func TestNewXRealIPHandler(t *testing.T) {
actualResponse := &httptest.ResponseRecorder{}

middleware := httpz.NewXRealIPHandler(
[]*net.IPNet{netz.PrivateIPAddressClassA},
httpz.DefaultSetRealIPFrom(),
httpz.HeaderXForwardedFor,
true,
httpz.WithClientIPAddressHeader(header),
Expand Down Expand Up @@ -71,7 +69,7 @@ func TestNewXRealIPHandler(t *testing.T) {
actualResponse := &httptest.ResponseRecorder{}

middleware := httpz.NewXRealIPHandler(
[]*net.IPNet{netz.PrivateIPAddressClassA},
httpz.DefaultSetRealIPFrom(),
testHeaderKey,
true,
)
Expand Down Expand Up @@ -103,7 +101,7 @@ func TestNewXRealIPHandler(t *testing.T) {
actualResponse := &httptest.ResponseRecorder{}

middleware := httpz.NewXRealIPHandler(
[]*net.IPNet{netz.PrivateIPAddressClassA},
httpz.DefaultSetRealIPFrom(),
httpz.HeaderXForwardedFor,
true,
)
Expand Down Expand Up @@ -135,7 +133,7 @@ func TestNewXRealIPHandler(t *testing.T) {
actualResponse := &httptest.ResponseRecorder{}

middleware := httpz.NewXRealIPHandler(
[]*net.IPNet{netz.PrivateIPAddressClassA},
httpz.DefaultSetRealIPFrom(),
httpz.HeaderXForwardedFor,
false,
)
Expand Down
21 changes: 11 additions & 10 deletions time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
type ctxKeyNow struct{}

func Now(ctx context.Context) time.Time {
nowFuncRWMu.RLock()
defer nowFuncRWMu.RUnlock()
return nowFunc(ctx)
_nowFuncRWMu.RLock()
t := _nowFunc(ctx)
_nowFuncRWMu.RUnlock()
return t
}

//nolint:gochecknoglobals
var (
nowFunc = DefaultNowFunc
nowFuncRWMu sync.RWMutex
_nowFunc NowFunc = DefaultNowFunc //nolint:revive
_nowFuncRWMu sync.RWMutex
)

type NowFunc = func(ctx context.Context) time.Time
Expand All @@ -30,11 +31,11 @@ func DefaultNowFunc(ctx context.Context) time.Time {
return time.Now()
}

func SetNowFunc(now NowFunc) (backup NowFunc) {
nowFuncRWMu.Lock()
defer nowFuncRWMu.Unlock()
backup = nowFunc
nowFunc = now
func SetNowFunc(nowFunc NowFunc) (backup NowFunc) {
_nowFuncRWMu.Lock()
backup = _nowFunc
_nowFunc = nowFunc
_nowFuncRWMu.Unlock()
return backup
}

Expand Down

0 comments on commit d4ce3f1

Please sign in to comment.