1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2025-09-19 12:06:01 +08:00

chore: change time.Duration atomic using

This commit is contained in:
wwqgtxx
2025-08-14 16:01:20 +08:00
parent eeb2ad8dae
commit 0836ec6ee3
2 changed files with 14 additions and 19 deletions

View File

@ -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
}

View File

@ -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) {