From cadc72d482e482c3e386e02843c6d11d2b2ddc71 Mon Sep 17 00:00:00 2001 From: claudemiro Date: Mon, 12 Jan 2015 22:41:42 -0300 Subject: [PATCH] Exporting Variables for Debug The variables are * Total Channels * Total Subscribers * Total of unique Messages received by the system. --- TODO.org | 4 ++-- app.go | 30 +++++++++++++++++++++++++++--- config.go | 5 +++-- router.go | 5 +++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/TODO.org b/TODO.org index b007ee5..ecd3d1d 100644 --- a/TODO.org +++ b/TODO.org @@ -1,14 +1,14 @@ IPÊ --- - * TODO [7/14] + * TODO [8/14] - [X] Autenticação API Rest - [X] Autenticação Websockets - [ ] Console de log - [X] Ping e Pong - [ ] Escrever testes automatizados - [ ] SSL - - [ ] Expvar - Canais, inscritos + - [X] Expvar - Canais, inscritos - [-] Otimizações [1/3] - [ ] Refatorar partes do código, remover repetições - [X] Alterar tipos de dados de slices para mapas em alguns locais. diff --git a/app.go b/app.go index bf9f008..1e4b331 100644 --- a/app.go +++ b/app.go @@ -6,11 +6,28 @@ package main import ( "errors" + "expvar" "sync" log "github.com/golang/glog" ) +var ( + // Exports the quantity of subscribers + expSubscribers *expvar.Int + + // Exports the quantity of channels + expChannels *expvar.Int + + expMessages *expvar.Int +) + +func init() { + expSubscribers = expvar.NewInt("TotalSubscribers") + expChannels = expvar.NewInt("TotalChannels") + expMessages = expvar.NewInt("TotalMessagesPublished") +} + // An App type App struct { sync.Mutex @@ -94,22 +111,25 @@ func (a *App) Disconnect(socketID string) { // Remove from app a.Lock() + defer a.Unlock() + _, exists := a.Subscribers[s.SocketID] if !exists { return } delete(a.Subscribers, s.SocketID) - - a.Unlock() + expSubscribers.Set(int64(len(a.Subscribers))) } // Connect a new Subscriber func (a *App) Connect(s *Subscriber) { log.Infof("Adding a new Subscriber %s to app %s", s.SocketID, a.Name) a.Lock() + defer a.Unlock() + a.Subscribers[s.SocketID] = s - a.Unlock() + expSubscribers.Set(int64(len(a.Subscribers))) } // Find a Subscriber on this app @@ -131,6 +151,8 @@ func (a *App) AddChannel(c *Channel) { a.Lock() a.Channels[c.ChannelID] = c a.Unlock() + + expChannels.Set(int64(len(a.Channels))) } // Returns a Channel from this app @@ -159,6 +181,8 @@ func (a *App) FindChannelByChannelID(n string) (*Channel, error) { } func (a *App) Publish(c *Channel, event RawEvent, ignore string) error { + expMessages.Add(1) + return c.Publish(a, event, ignore) } diff --git a/config.go b/config.go index 6388c6d..c8a3274 100644 --- a/config.go +++ b/config.go @@ -8,8 +8,9 @@ import "errors" // The config file type ConfigFile struct { - Host string // The host, eg: :8080 will start on 0.0.0.0:8080 - Apps []*App + Host string // The host, eg: :8080 will start on 0.0.0.0:8080 + Expvar bool + Apps []*App } // Error for App not found diff --git a/router.go b/router.go index ec4d5f1..52f2b40 100644 --- a/router.go +++ b/router.go @@ -5,6 +5,7 @@ package main import ( + _ "expvar" "net/http" "os" @@ -17,6 +18,10 @@ import ( func NewRouter() *mux.Router { router := mux.NewRouter().StrictSlash(true) + if Conf.Expvar { + router.Handle("/debug/vars", http.DefaultServeMux) + } + for _, route := range routes { var handler http.Handler