mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-09-20 04:25:59 +08:00
chore: change time.Duration atomic using
This commit is contained in:
@ -5,24 +5,19 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DefaultValue[T any]() T {
|
|
||||||
var defaultValue T
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
type TypedValue[T any] struct {
|
type TypedValue[T any] struct {
|
||||||
value atomic.Pointer[T]
|
value atomic.Pointer[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) Load() T {
|
func (t *TypedValue[T]) Load() (v T) {
|
||||||
value, _ := t.LoadOk()
|
v, _ = t.LoadOk()
|
||||||
return value
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) LoadOk() (_ T, ok bool) {
|
func (t *TypedValue[T]) LoadOk() (v T, ok bool) {
|
||||||
value := t.value.Load()
|
value := t.value.Load()
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return DefaultValue[T](), false
|
return
|
||||||
}
|
}
|
||||||
return *value, true
|
return *value, true
|
||||||
}
|
}
|
||||||
@ -31,10 +26,10 @@ func (t *TypedValue[T]) Store(value T) {
|
|||||||
t.value.Store(&value)
|
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)
|
old := t.value.Swap(&new)
|
||||||
if old == nil {
|
if old == nil {
|
||||||
return DefaultValue[T]()
|
return
|
||||||
}
|
}
|
||||||
return *old
|
return *old
|
||||||
}
|
}
|
||||||
@ -42,7 +37,7 @@ func (t *TypedValue[T]) Swap(new T) T {
|
|||||||
func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
|
func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
|
||||||
for {
|
for {
|
||||||
currentP := t.value.Load()
|
currentP := t.value.Load()
|
||||||
currentValue := DefaultValue[T]()
|
var currentValue T
|
||||||
if currentP != nil {
|
if currentP != nil {
|
||||||
currentValue = *currentP
|
currentValue = *currentP
|
||||||
}
|
}
|
||||||
|
@ -10,27 +10,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
keepAliveIdle = atomic.NewTypedValue[time.Duration](0 * time.Second)
|
keepAliveIdle = atomic.NewInt64(0)
|
||||||
keepAliveInterval = atomic.NewTypedValue[time.Duration](0 * time.Second)
|
keepAliveInterval = atomic.NewInt64(0)
|
||||||
disableKeepAlive = atomic.NewBool(false)
|
disableKeepAlive = atomic.NewBool(false)
|
||||||
|
|
||||||
SetDisableKeepAliveCallback = utils.NewCallback[bool]()
|
SetDisableKeepAliveCallback = utils.NewCallback[bool]()
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetKeepAliveIdle(t time.Duration) {
|
func SetKeepAliveIdle(t time.Duration) {
|
||||||
keepAliveIdle.Store(t)
|
keepAliveIdle.Store(int64(t))
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetKeepAliveInterval(t time.Duration) {
|
func SetKeepAliveInterval(t time.Duration) {
|
||||||
keepAliveInterval.Store(t)
|
keepAliveInterval.Store(int64(t))
|
||||||
}
|
}
|
||||||
|
|
||||||
func KeepAliveIdle() time.Duration {
|
func KeepAliveIdle() time.Duration {
|
||||||
return keepAliveIdle.Load()
|
return time.Duration(keepAliveIdle.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func KeepAliveInterval() time.Duration {
|
func KeepAliveInterval() time.Duration {
|
||||||
return keepAliveInterval.Load()
|
return time.Duration(keepAliveInterval.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDisableKeepAlive(disable bool) {
|
func SetDisableKeepAlive(disable bool) {
|
||||||
|
Reference in New Issue
Block a user