From 0836ec6ee3eeebbde2b69037520023ffcc61f433 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 14 Aug 2025 16:01:20 +0800 Subject: [PATCH] chore: change time.Duration atomic using --- common/atomic/value.go | 21 ++++++++------------- component/keepalive/tcp_keepalive.go | 12 ++++++------ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/common/atomic/value.go b/common/atomic/value.go index b3306939..6480ee47 100644 --- a/common/atomic/value.go +++ b/common/atomic/value.go @@ -5,24 +5,19 @@ import ( "sync/atomic" ) -func DefaultValue[T any]() T { - var defaultValue T - return defaultValue -} - type TypedValue[T any] struct { value atomic.Pointer[T] } -func (t *TypedValue[T]) Load() T { - value, _ := t.LoadOk() - return value +func (t *TypedValue[T]) Load() (v T) { + v, _ = t.LoadOk() + return } -func (t *TypedValue[T]) LoadOk() (_ T, ok bool) { +func (t *TypedValue[T]) LoadOk() (v T, ok bool) { value := t.value.Load() if value == nil { - return DefaultValue[T](), false + return } return *value, true } @@ -31,10 +26,10 @@ func (t *TypedValue[T]) Store(value T) { t.value.Store(&value) } -func (t *TypedValue[T]) Swap(new T) T { +func (t *TypedValue[T]) Swap(new T) (v T) { old := t.value.Swap(&new) if old == nil { - return DefaultValue[T]() + return } return *old } @@ -42,7 +37,7 @@ func (t *TypedValue[T]) Swap(new T) T { func (t *TypedValue[T]) CompareAndSwap(old, new T) bool { for { currentP := t.value.Load() - currentValue := DefaultValue[T]() + var currentValue T if currentP != nil { currentValue = *currentP } diff --git a/component/keepalive/tcp_keepalive.go b/component/keepalive/tcp_keepalive.go index ddb853e9..fee06a94 100644 --- a/component/keepalive/tcp_keepalive.go +++ b/component/keepalive/tcp_keepalive.go @@ -10,27 +10,27 @@ import ( ) var ( - keepAliveIdle = atomic.NewTypedValue[time.Duration](0 * time.Second) - keepAliveInterval = atomic.NewTypedValue[time.Duration](0 * time.Second) + keepAliveIdle = atomic.NewInt64(0) + keepAliveInterval = atomic.NewInt64(0) disableKeepAlive = atomic.NewBool(false) SetDisableKeepAliveCallback = utils.NewCallback[bool]() ) func SetKeepAliveIdle(t time.Duration) { - keepAliveIdle.Store(t) + keepAliveIdle.Store(int64(t)) } func SetKeepAliveInterval(t time.Duration) { - keepAliveInterval.Store(t) + keepAliveInterval.Store(int64(t)) } func KeepAliveIdle() time.Duration { - return keepAliveIdle.Load() + return time.Duration(keepAliveIdle.Load()) } func KeepAliveInterval() time.Duration { - return keepAliveInterval.Load() + return time.Duration(keepAliveInterval.Load()) } func SetDisableKeepAlive(disable bool) {