Add a server context that gets canceled when a server is deleted

This commit is contained in:
Dane Everitt
2020-12-25 11:21:09 -08:00
parent f7f5623c71
commit c0523df696
9 changed files with 93 additions and 104 deletions

View File

@@ -1,10 +1,28 @@
package system
import (
"context"
"sync"
"sync/atomic"
"time"
)
// Runs a given work function every "d" duration until the provided context is canceled.
func Every(ctx context.Context, d time.Duration, work func(t time.Time)) {
ticker := time.NewTicker(d)
go func() {
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case t := <-ticker.C:
work(t)
}
}
}()
}
type AtomicBool struct {
flag uint32
}
@@ -30,8 +48,8 @@ type AtomicString struct {
mu sync.RWMutex
}
func NewAtomicString(v string) *AtomicString {
return &AtomicString{v: v}
func NewAtomicString(v string) AtomicString {
return AtomicString{v: v}
}
// Stores the string value passed atomically.
@@ -53,6 +71,7 @@ func (as *AtomicString) UnmarshalText(b []byte) error {
return nil
}
func (as *AtomicString) MarshalText() ([]byte, error) {
//goland:noinspection GoVetCopyLock
func (as AtomicString) MarshalText() ([]byte, error) {
return []byte(as.Load()), nil
}