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