Fix atomic string handling for JSON output

This commit is contained in:
Dane Everitt 2020-11-06 22:22:33 -08:00
parent 944d381778
commit 4b17ac4f1c
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 22 additions and 16 deletions

View File

@ -48,7 +48,7 @@ type Environment struct {
emitter *events.EventBus emitter *events.EventBus
// Tracks the environment state. // Tracks the environment state.
st system.AtomicString st *system.AtomicString
} }
// Creates a new base Docker environment. The ID passed through will be the ID that is used to // Creates a new base Docker environment. The ID passed through will be the ID that is used to
@ -65,10 +65,9 @@ func New(id string, m *Metadata, c *environment.Configuration) (*Environment, er
Configuration: c, Configuration: c,
meta: m, meta: m,
client: cli, client: cli,
st: system.NewAtomicString(environment.ProcessOfflineState),
} }
e.st.Store(environment.ProcessOfflineState)
return e, nil return e, nil
} }

View File

@ -18,7 +18,7 @@ type ResourceUsage struct {
environment.Stats environment.Stats
// The current server status. // The current server status.
State system.AtomicString `json:"state"` State *system.AtomicString `json:"state" default:"{}"`
// The current disk space being used by the server. This value is not guaranteed to be accurate // The current disk space being used by the server. This value is not guaranteed to be accurate
// at all times. It is "manually" set whenever server.Proc() is called. This is kind of just a // at all times. It is "manually" set whenever server.Proc() is called. This is kind of just a

View File

@ -1,6 +1,7 @@
package system package system
import ( import (
"sync"
"sync/atomic" "sync/atomic"
) )
@ -25,27 +26,33 @@ func (ab *AtomicBool) Get() bool {
// about a potential race condition scenario. Under the hood it uses a simple sync.RWMutex // about a potential race condition scenario. Under the hood it uses a simple sync.RWMutex
// to control access to the value. // to control access to the value.
type AtomicString struct { type AtomicString struct {
v atomic.Value v string
mu sync.RWMutex
} }
// Returns a new instance of an AtomicString.
func NewAtomicString(v string) *AtomicString { func NewAtomicString(v string) *AtomicString {
as := &AtomicString{} return &AtomicString{v: v}
if v != "" {
as.Store(v)
}
return as
} }
// Stores the string value passed atomically. // Stores the string value passed atomically.
func (as *AtomicString) Store(v string) { func (as *AtomicString) Store(v string) {
as.v.Store(v) as.mu.Lock()
as.v = v
as.mu.Unlock()
} }
// Loads the string value and returns it. // Loads the string value and returns it.
func (as *AtomicString) Load() string { func (as *AtomicString) Load() string {
if v := as.v.Load(); v != nil { as.mu.RLock()
return v.(string) defer as.mu.RUnlock()
} return as.v
return "" }
func (as *AtomicString) UnmarshalText(b []byte) error {
as.Store(string(b))
return nil
}
func (as *AtomicString) MarshalText() ([]byte, error) {
return []byte(as.Load()), nil
} }