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
// 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
@ -65,10 +65,9 @@ func New(id string, m *Metadata, c *environment.Configuration) (*Environment, er
Configuration: c,
meta: m,
client: cli,
st: system.NewAtomicString(environment.ProcessOfflineState),
}
e.st.Store(environment.ProcessOfflineState)
return e, nil
}

View File

@ -18,7 +18,7 @@ type ResourceUsage struct {
environment.Stats
// 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
// 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
import (
"sync"
"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
// to control access to the value.
type AtomicString struct {
v atomic.Value
v string
mu sync.RWMutex
}
// Returns a new instance of an AtomicString.
func NewAtomicString(v string) *AtomicString {
as := &AtomicString{}
if v != "" {
as.Store(v)
}
return as
return &AtomicString{v: v}
}
// Stores the string value passed atomically.
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.
func (as *AtomicString) Load() string {
if v := as.v.Load(); v != nil {
return v.(string)
}
return ""
as.mu.RLock()
defer as.mu.RUnlock()
return as.v
}
func (as *AtomicString) UnmarshalText(b []byte) error {
as.Store(string(b))
return nil
}
func (as *AtomicString) MarshalText() ([]byte, error) {
return []byte(as.Load()), nil
}