github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/thaw.go (about) 1 package common 2 3 import ( 4 "sync/atomic" 5 ) 6 7 var TopicListThaw ThawInt 8 9 type ThawInt interface { 10 Thawed() bool 11 Thaw() 12 13 Tick() error 14 } 15 16 type SingleServerThaw struct { 17 DefaultThaw 18 } 19 20 func NewSingleServerThaw() *SingleServerThaw { 21 t := &SingleServerThaw{} 22 if Config.ServerCount == 1 { 23 Tasks.Sec.Add(t.Tick) 24 } 25 return t 26 } 27 28 func (t *SingleServerThaw) Thawed() bool { 29 if Config.ServerCount == 1 { 30 return t.DefaultThaw.Thawed() 31 } 32 return true 33 } 34 35 func (t *SingleServerThaw) Thaw() { 36 if Config.ServerCount == 1 { 37 t.DefaultThaw.Thaw() 38 } 39 } 40 41 type TestThaw struct { 42 } 43 44 func NewTestThaw() *TestThaw { 45 return &TestThaw{} 46 } 47 func (t *TestThaw) Thawed() bool { 48 return true 49 } 50 func (t *TestThaw) Thaw() { 51 } 52 func (t *TestThaw) Tick() error { 53 return nil 54 } 55 56 type DefaultThaw struct { 57 thawed int64 58 } 59 60 func NewDefaultThaw() *DefaultThaw { 61 t := &DefaultThaw{} 62 Tasks.Sec.Add(t.Tick) 63 return t 64 } 65 66 // Decrement the thawed counter once a second until it goes cold 67 func (t *DefaultThaw) Tick() error { 68 prior := t.thawed 69 if prior > 0 { 70 atomic.StoreInt64(&t.thawed, prior-1) 71 } 72 return nil 73 } 74 75 func (t *DefaultThaw) Thawed() bool { 76 return t.thawed > 0 77 } 78 79 func (t *DefaultThaw) Thaw() { 80 atomic.StoreInt64(&t.thawed, 3) 81 }