mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
1cf9c80e97
For structs protected by a single mutex, embed the mutex for more concise usage. Also use a sync.Mutex directly, rather than a pointer, to avoid the need for initialization (because a Mutex's zero-value is valid and ready to be used).
41 lines
706 B
Go
41 lines
706 B
Go
package docker
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/dotcloud/docker/utils"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type State struct {
|
|
sync.Mutex
|
|
Running bool
|
|
Pid int
|
|
ExitCode int
|
|
StartedAt time.Time
|
|
Ghost bool
|
|
}
|
|
|
|
// String returns a human-readable description of the state
|
|
func (s *State) String() string {
|
|
if s.Running {
|
|
if s.Ghost {
|
|
return fmt.Sprintf("Ghost")
|
|
}
|
|
return fmt.Sprintf("Up %s", utils.HumanDuration(time.Now().Sub(s.StartedAt)))
|
|
}
|
|
return fmt.Sprintf("Exit %d", s.ExitCode)
|
|
}
|
|
|
|
func (s *State) setRunning(pid int) {
|
|
s.Running = true
|
|
s.ExitCode = 0
|
|
s.Pid = pid
|
|
s.StartedAt = time.Now()
|
|
}
|
|
|
|
func (s *State) setStopped(exitCode int) {
|
|
s.Running = false
|
|
s.Pid = 0
|
|
s.ExitCode = exitCode
|
|
}
|