2018-11-19 23:06:15 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"sync/atomic"
|
2018-11-19 23:06:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var TopicListThaw ThawInt
|
|
|
|
|
|
|
|
type ThawInt interface {
|
2022-02-21 03:32:53 +00:00
|
|
|
Thawed() bool
|
|
|
|
Thaw()
|
2018-11-19 23:06:15 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
Tick() error
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SingleServerThaw struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
DefaultThaw
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSingleServerThaw() *SingleServerThaw {
|
2022-02-21 03:32:53 +00:00
|
|
|
t := &SingleServerThaw{}
|
|
|
|
if Config.ServerCount == 1 {
|
|
|
|
Tasks.Sec.Add(t.Tick)
|
|
|
|
}
|
|
|
|
return t
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *SingleServerThaw) Thawed() bool {
|
2022-02-21 03:32:53 +00:00
|
|
|
if Config.ServerCount == 1 {
|
|
|
|
return t.DefaultThaw.Thawed()
|
|
|
|
}
|
|
|
|
return true
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *SingleServerThaw) Thaw() {
|
2022-02-21 03:32:53 +00:00
|
|
|
if Config.ServerCount == 1 {
|
|
|
|
t.DefaultThaw.Thaw()
|
|
|
|
}
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 11:45:18 +00:00
|
|
|
type TestThaw struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestThaw() *TestThaw {
|
2022-02-21 03:32:53 +00:00
|
|
|
return &TestThaw{}
|
2021-03-24 11:45:18 +00:00
|
|
|
}
|
|
|
|
func (t *TestThaw) Thawed() bool {
|
2022-02-21 03:32:53 +00:00
|
|
|
return true
|
2021-03-24 11:45:18 +00:00
|
|
|
}
|
|
|
|
func (t *TestThaw) Thaw() {
|
|
|
|
}
|
|
|
|
func (t *TestThaw) Tick() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
return nil
|
2021-03-24 11:45:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 23:06:15 +00:00
|
|
|
type DefaultThaw struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
thawed int64
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultThaw() *DefaultThaw {
|
2022-02-21 03:32:53 +00:00
|
|
|
t := &DefaultThaw{}
|
|
|
|
Tasks.Sec.Add(t.Tick)
|
|
|
|
return t
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement the thawed counter once a second until it goes cold
|
2019-09-29 04:56:39 +00:00
|
|
|
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
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *DefaultThaw) Thawed() bool {
|
2022-02-21 03:32:53 +00:00
|
|
|
return t.thawed > 0
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *DefaultThaw) Thaw() {
|
2022-02-21 03:32:53 +00:00
|
|
|
atomic.StoreInt64(&t.thawed, 3)
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|