2018-11-19 23:06:15 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
var TopicListThaw ThawInt
|
|
|
|
|
|
|
|
type ThawInt interface {
|
|
|
|
Thawed() bool
|
|
|
|
Thaw()
|
|
|
|
|
|
|
|
Tick() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type SingleServerThaw struct {
|
|
|
|
DefaultThaw
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSingleServerThaw() *SingleServerThaw {
|
2019-09-29 04:56:39 +00:00
|
|
|
t := &SingleServerThaw{}
|
2018-11-19 23:06:15 +00:00
|
|
|
if Config.ServerCount == 1 {
|
2019-09-29 04:56:39 +00:00
|
|
|
AddScheduledSecondTask(t.Tick)
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
2019-09-29 04:56:39 +00:00
|
|
|
return t
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *SingleServerThaw) Thawed() bool {
|
2018-11-19 23:06:15 +00:00
|
|
|
if Config.ServerCount == 1 {
|
2019-09-29 04:56:39 +00:00
|
|
|
return t.DefaultThaw.Thawed()
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *SingleServerThaw) Thaw() {
|
2018-11-19 23:06:15 +00:00
|
|
|
if Config.ServerCount == 1 {
|
2019-09-29 04:56:39 +00:00
|
|
|
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 {
|
|
|
|
return &TestThaw{}
|
|
|
|
}
|
|
|
|
func (t *TestThaw) Thawed() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func (t *TestThaw) Thaw() {
|
|
|
|
}
|
|
|
|
func (t *TestThaw) Tick() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:06:15 +00:00
|
|
|
type DefaultThaw struct {
|
|
|
|
thawed int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultThaw() *DefaultThaw {
|
2019-09-29 04:56:39 +00:00
|
|
|
t := &DefaultThaw{}
|
|
|
|
AddScheduledSecondTask(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 {
|
|
|
|
prior := t.thawed
|
2018-11-19 23:06:15 +00:00
|
|
|
if prior > 0 {
|
2019-09-29 04:56:39 +00:00
|
|
|
atomic.StoreInt64(&t.thawed, prior-1)
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *DefaultThaw) Thawed() bool {
|
|
|
|
return t.thawed > 0
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (t *DefaultThaw) Thaw() {
|
2020-03-09 03:51:44 +00:00
|
|
|
atomic.StoreInt64(&t.thawed, 3)
|
2018-11-19 23:06:15 +00:00
|
|
|
}
|