gosora/common/thaw.go

82 lines
1.3 KiB
Go
Raw Normal View History

package common
import (
2022-02-21 03:32:53 +00:00
"sync/atomic"
)
var TopicListThaw ThawInt
type ThawInt interface {
2022-02-21 03:32:53 +00:00
Thawed() bool
Thaw()
2022-02-21 03:32:53 +00:00
Tick() error
}
type SingleServerThaw struct {
2022-02-21 03:32:53 +00:00
DefaultThaw
}
func NewSingleServerThaw() *SingleServerThaw {
2022-02-21 03:32:53 +00:00
t := &SingleServerThaw{}
if Config.ServerCount == 1 {
Tasks.Sec.Add(t.Tick)
}
return t
}
func (t *SingleServerThaw) Thawed() bool {
2022-02-21 03:32:53 +00:00
if Config.ServerCount == 1 {
return t.DefaultThaw.Thawed()
}
return true
}
func (t *SingleServerThaw) Thaw() {
2022-02-21 03:32:53 +00:00
if Config.ServerCount == 1 {
t.DefaultThaw.Thaw()
}
}
type TestThaw struct {
}
func NewTestThaw() *TestThaw {
2022-02-21 03:32:53 +00:00
return &TestThaw{}
}
func (t *TestThaw) Thawed() bool {
2022-02-21 03:32:53 +00:00
return true
}
func (t *TestThaw) Thaw() {
}
func (t *TestThaw) Tick() error {
2022-02-21 03:32:53 +00:00
return nil
}
type DefaultThaw struct {
2022-02-21 03:32:53 +00:00
thawed int64
}
func NewDefaultThaw() *DefaultThaw {
2022-02-21 03:32:53 +00:00
t := &DefaultThaw{}
Tasks.Sec.Add(t.Tick)
return t
}
// Decrement the thawed counter once a second until it goes cold
func (t *DefaultThaw) Tick() error {
2022-02-21 03:32:53 +00:00
prior := t.thawed
if prior > 0 {
atomic.StoreInt64(&t.thawed, prior-1)
}
return nil
}
func (t *DefaultThaw) Thawed() bool {
2022-02-21 03:32:53 +00:00
return t.thawed > 0
}
func (t *DefaultThaw) Thaw() {
2022-02-21 03:32:53 +00:00
atomic.StoreInt64(&t.thawed, 3)
}