mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-13 09:12:43 +08:00
Fixes prometheus gauges
Fix session close. Now PWD gets disconnected from network before deleting it.
This commit is contained in:
parent
70eaf37d4b
commit
93226e30ff
@ -4,20 +4,8 @@ import (
|
||||
"log"
|
||||
|
||||
"github.com/googollee/go-socket.io"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
clientsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "clients",
|
||||
Help: "Clients",
|
||||
})
|
||||
)
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(clientsGauge)
|
||||
}
|
||||
|
||||
type ViewPort struct {
|
||||
Rows uint
|
||||
Cols uint
|
||||
@ -35,7 +23,6 @@ func (c *Client) ResizeViewPort(cols, rows uint) {
|
||||
}
|
||||
|
||||
func NewClient(so socketio.Socket, session *Session) *Client {
|
||||
clientsGauge.Inc()
|
||||
so.Join(session.Id)
|
||||
|
||||
c := &Client{so: so, Id: so.Id()}
|
||||
@ -67,7 +54,6 @@ func NewClient(so socketio.Socket, session *Session) *Client {
|
||||
})
|
||||
|
||||
so.On("disconnection", func() {
|
||||
clientsGauge.Dec()
|
||||
// Client has disconnected. Remove from session and recheck terminal sizes.
|
||||
for i, cl := range session.clients {
|
||||
if cl.Id == c.Id {
|
||||
@ -83,6 +69,7 @@ func NewClient(so socketio.Socket, session *Session) *Client {
|
||||
instance.ResizeTerminal(vp.Cols, vp.Rows)
|
||||
}
|
||||
}
|
||||
setGauges()
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
@ -142,6 +142,17 @@ func ConnectNetwork(containerId, networkId string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
func DisconnectNetwork(containerId, networkId string) error {
|
||||
err := c.NetworkDisconnect(context.Background(), networkId, containerId, true)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Disconnection of container from network err [%s]\n", err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteNetwork(id string) error {
|
||||
err := c.NetworkRemove(context.Background(), id)
|
||||
|
@ -11,22 +11,10 @@ import (
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var rw sync.Mutex
|
||||
|
||||
var (
|
||||
instancesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "instances",
|
||||
Help: "Instances",
|
||||
})
|
||||
)
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(instancesGauge)
|
||||
}
|
||||
|
||||
type Instance struct {
|
||||
session *Session `json:"-"`
|
||||
Name string `json:"name"`
|
||||
@ -102,7 +90,7 @@ func NewInstance(session *Session) (*Instance, error) {
|
||||
|
||||
wsServer.BroadcastTo(session.Id, "new instance", instance.Name, instance.IP, instance.Hostname)
|
||||
|
||||
instancesGauge.Inc()
|
||||
setGauges()
|
||||
|
||||
return instance, nil
|
||||
}
|
||||
@ -156,7 +144,7 @@ func DeleteInstance(session *Session, instance *Instance) error {
|
||||
|
||||
wsServer.BroadcastTo(session.Id, "delete instance", instance.Name)
|
||||
|
||||
instancesGauge.Dec()
|
||||
setGauges()
|
||||
|
||||
return err
|
||||
}
|
||||
|
@ -23,10 +23,20 @@ var (
|
||||
Name: "sessions",
|
||||
Help: "Sessions",
|
||||
})
|
||||
clientsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "clients",
|
||||
Help: "Clients",
|
||||
})
|
||||
instancesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "instances",
|
||||
Help: "Instances",
|
||||
})
|
||||
)
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(sessionsGauge)
|
||||
prometheus.MustRegister(clientsGauge)
|
||||
prometheus.MustRegister(instancesGauge)
|
||||
}
|
||||
|
||||
var wsServer *socketio.Server
|
||||
@ -64,6 +74,7 @@ func (s *Session) GetSmallestViewPort() ViewPort {
|
||||
|
||||
func (s *Session) AddNewClient(c *Client) {
|
||||
s.clients = append(s.clients, c)
|
||||
setGauges()
|
||||
}
|
||||
|
||||
func (s *Session) SchedulePeriodicTasks() {
|
||||
@ -141,14 +152,12 @@ func CloseSession(s *Session) error {
|
||||
s.rw.Lock()
|
||||
defer s.rw.Unlock()
|
||||
|
||||
sessionsGauge.Dec()
|
||||
if s.ticker != nil {
|
||||
s.ticker.Stop()
|
||||
}
|
||||
wsServer.BroadcastTo(s.Id, "session end")
|
||||
for _, c := range s.clients {
|
||||
c.so.Emit("disconnect")
|
||||
clientsGauge.Dec()
|
||||
}
|
||||
log.Printf("Starting clean up of session [%s]\n", s.Id)
|
||||
for _, i := range s.Instances {
|
||||
@ -160,6 +169,12 @@ func CloseSession(s *Session) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Disconnect PWD daemon from the network
|
||||
if err := DisconnectNetwork("pwd", s.Id); err != nil {
|
||||
log.Println("ERROR NETWORKING")
|
||||
return err
|
||||
}
|
||||
log.Printf("Connected pwd to network [%s]\n", s.Id)
|
||||
if err := DeleteNetwork(s.Id); err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
@ -170,6 +185,7 @@ func CloseSession(s *Session) error {
|
||||
if err := saveSessionsToDisk(); err != nil {
|
||||
return err
|
||||
}
|
||||
setGauges()
|
||||
log.Printf("Cleaned up session [%s]\n", s.Id)
|
||||
return nil
|
||||
}
|
||||
@ -225,7 +241,7 @@ func NewSession() (*Session, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sessionsGauge.Inc()
|
||||
setGauges()
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@ -243,6 +259,20 @@ func GetSession(sessionId string) *Session {
|
||||
return s
|
||||
}
|
||||
|
||||
func setGauges() {
|
||||
var ins float64
|
||||
var cli float64
|
||||
|
||||
for _, s := range sessions {
|
||||
ins += float64(len(s.Instances))
|
||||
cli += float64(len(s.clients))
|
||||
}
|
||||
|
||||
clientsGauge.Set(cli)
|
||||
instancesGauge.Set(ins)
|
||||
sessionsGauge.Set(float64(len(sessions)))
|
||||
}
|
||||
|
||||
func LoadSessionsFromDisk() error {
|
||||
file, err := os.Open("./pwd/sessions.gob")
|
||||
if err == nil {
|
||||
@ -255,7 +285,6 @@ func LoadSessionsFromDisk() error {
|
||||
|
||||
// schedule session expiration
|
||||
for _, s := range sessions {
|
||||
sessionsGauge.Inc()
|
||||
timeLeft := s.ExpiresAt.Sub(time.Now())
|
||||
CloseSessionAfter(s, timeLeft)
|
||||
|
||||
@ -263,7 +292,6 @@ func LoadSessionsFromDisk() error {
|
||||
for _, i := range s.Instances {
|
||||
// wire the session back to the instance
|
||||
i.session = s
|
||||
instancesGauge.Inc()
|
||||
}
|
||||
|
||||
// Connect PWD daemon to the new network
|
||||
@ -278,6 +306,7 @@ func LoadSessionsFromDisk() error {
|
||||
}
|
||||
}
|
||||
file.Close()
|
||||
setGauges()
|
||||
return err
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user