Storage has now it's own package.
Remove global `sessions` map and use configured storage. Add a `types` package so both `pwd` and `storage` can access without circular dependencies. Now the session is prepared when requested and not on load.
This commit is contained in:
68
pwd/pwd.go
68
pwd/pwd.go
@@ -1,10 +1,11 @@
|
||||
package pwd
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/play-with-docker/play-with-docker/docker"
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
"github.com/play-with-docker/play-with-docker/storage"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
@@ -33,65 +34,58 @@ func observeAction(action string, start time.Time) {
|
||||
latencyHistogramVec.WithLabelValues(action).Observe(float64(time.Since(start).Nanoseconds()) / 1000000)
|
||||
}
|
||||
|
||||
var sessions map[string]*Session
|
||||
var sessionsMutex sync.Mutex
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(sessionsGauge)
|
||||
prometheus.MustRegister(clientsGauge)
|
||||
prometheus.MustRegister(instancesGauge)
|
||||
prometheus.MustRegister(latencyHistogramVec)
|
||||
|
||||
sessions = make(map[string]*Session)
|
||||
}
|
||||
|
||||
type pwd struct {
|
||||
docker docker.DockerApi
|
||||
tasks SchedulerApi
|
||||
broadcast BroadcastApi
|
||||
storage StorageApi
|
||||
storage storage.StorageApi
|
||||
}
|
||||
|
||||
type PWDApi interface {
|
||||
SessionNew(duration time.Duration, stack string, stackName, imageName string) (*Session, error)
|
||||
SessionClose(session *Session) error
|
||||
SessionGetSmallestViewPort(session *Session) ViewPort
|
||||
SessionDeployStack(session *Session) error
|
||||
SessionGet(id string) *Session
|
||||
SessionLoadAndPrepare() error
|
||||
SessionSetup(session *Session, conf SessionSetupConf) error
|
||||
SessionNew(duration time.Duration, stack string, stackName, imageName string) (*types.Session, error)
|
||||
SessionClose(session *types.Session) error
|
||||
SessionGetSmallestViewPort(session *types.Session) types.ViewPort
|
||||
SessionDeployStack(session *types.Session) error
|
||||
SessionGet(id string) *types.Session
|
||||
SessionSetup(session *types.Session, conf SessionSetupConf) error
|
||||
|
||||
InstanceNew(session *Session, conf InstanceConfig) (*Instance, error)
|
||||
InstanceResizeTerminal(instance *Instance, cols, rows uint) error
|
||||
InstanceAttachTerminal(instance *Instance) error
|
||||
InstanceUploadFromUrl(instance *Instance, url string) error
|
||||
InstanceGet(session *Session, name string) *Instance
|
||||
InstanceFindByIP(ip string) *Instance
|
||||
InstanceFindByAlias(sessionPrefix, alias string) *Instance
|
||||
InstanceDelete(session *Session, instance *Instance) error
|
||||
InstanceWriteToTerminal(instance *Instance, data string)
|
||||
InstanceNew(session *types.Session, conf InstanceConfig) (*types.Instance, error)
|
||||
InstanceResizeTerminal(instance *types.Instance, cols, rows uint) error
|
||||
InstanceAttachTerminal(instance *types.Instance) error
|
||||
InstanceUploadFromUrl(instance *types.Instance, url string) error
|
||||
InstanceGet(session *types.Session, name string) *types.Instance
|
||||
InstanceFindByIP(ip string) *types.Instance
|
||||
InstanceFindByAlias(sessionPrefix, alias string) *types.Instance
|
||||
InstanceDelete(session *types.Session, instance *types.Instance) error
|
||||
InstanceWriteToTerminal(instance *types.Instance, data string)
|
||||
InstanceAllowedImages() []string
|
||||
InstanceExec(instance *Instance, cmd []string) (int, error)
|
||||
InstanceExec(instance *types.Instance, cmd []string) (int, error)
|
||||
|
||||
ClientNew(id string, session *Session) *Client
|
||||
ClientResizeViewPort(client *Client, cols, rows uint)
|
||||
ClientClose(client *Client)
|
||||
ClientNew(id string, session *types.Session) *types.Client
|
||||
ClientResizeViewPort(client *types.Client, cols, rows uint)
|
||||
ClientClose(client *types.Client)
|
||||
}
|
||||
|
||||
func NewPWD(d docker.DockerApi, t SchedulerApi, b BroadcastApi, s StorageApi) *pwd {
|
||||
func NewPWD(d docker.DockerApi, t SchedulerApi, b BroadcastApi, s storage.StorageApi) *pwd {
|
||||
return &pwd{docker: d, tasks: t, broadcast: b, storage: s}
|
||||
}
|
||||
|
||||
func setGauges() {
|
||||
var ins float64
|
||||
var cli float64
|
||||
|
||||
for _, s := range sessions {
|
||||
ins += float64(len(s.Instances))
|
||||
cli += float64(len(s.clients))
|
||||
}
|
||||
func (p *pwd) setGauges() {
|
||||
s, _ := p.storage.SessionCount()
|
||||
ses := float64(s)
|
||||
i, _ := p.storage.InstanceCount()
|
||||
ins := float64(i)
|
||||
c, _ := p.storage.ClientCount()
|
||||
cli := float64(c)
|
||||
|
||||
clientsGauge.Set(cli)
|
||||
instancesGauge.Set(ins)
|
||||
sessionsGauge.Set(float64(len(sessions)))
|
||||
sessionsGauge.Set(ses)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user