Fix atomic string handling for JSON output
This commit is contained in:
parent
944d381778
commit
4b17ac4f1c
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
as.mu.RLock()
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user