From 61cf0b022d9aff11b8d57f7b7b812dbe6cb599da Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Fri, 25 Aug 2023 04:36:55 +0900 Subject: [PATCH 1/2] fix: misc --- time/time.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/time/time.go b/time/time.go index 0a44f584..dda439c0 100644 --- a/time/time.go +++ b/time/time.go @@ -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 @@ -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 } From 6d550af85ac4e8c15e668bf2d1edc78b0a8c0abe Mon Sep 17 00:00:00 2001 From: ginokent <29125616+ginokent@users.noreply.github.com> Date: Fri, 25 Aug 2023 05:05:19 +0900 Subject: [PATCH 2/2] test: fix net/http test --- net/http/realip_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/net/http/realip_test.go b/net/http/realip_test.go index de71a1a5..5b72bdb2 100644 --- a/net/http/realip_test.go +++ b/net/http/realip_test.go @@ -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" ) @@ -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), @@ -71,7 +69,7 @@ func TestNewXRealIPHandler(t *testing.T) { actualResponse := &httptest.ResponseRecorder{} middleware := httpz.NewXRealIPHandler( - []*net.IPNet{netz.PrivateIPAddressClassA}, + httpz.DefaultSetRealIPFrom(), testHeaderKey, true, ) @@ -103,7 +101,7 @@ func TestNewXRealIPHandler(t *testing.T) { actualResponse := &httptest.ResponseRecorder{} middleware := httpz.NewXRealIPHandler( - []*net.IPNet{netz.PrivateIPAddressClassA}, + httpz.DefaultSetRealIPFrom(), httpz.HeaderXForwardedFor, true, ) @@ -135,7 +133,7 @@ func TestNewXRealIPHandler(t *testing.T) { actualResponse := &httptest.ResponseRecorder{} middleware := httpz.NewXRealIPHandler( - []*net.IPNet{netz.PrivateIPAddressClassA}, + httpz.DefaultSetRealIPFrom(), httpz.HeaderXForwardedFor, false, )