Exporting Variables for Debug

The variables are

* Total Channels
* Total Subscribers
* Total of unique Messages received by the system.
This commit is contained in:
claudemiro
2015-01-12 22:41:42 -03:00
parent 71d5099ac3
commit cadc72d482
4 changed files with 37 additions and 7 deletions
+2 -2
View File
@@ -1,14 +1,14 @@
IPÊ IPÊ
--- ---
* TODO [7/14] * TODO [8/14]
- [X] Autenticação API Rest - [X] Autenticação API Rest
- [X] Autenticação Websockets - [X] Autenticação Websockets
- [ ] Console de log - [ ] Console de log
- [X] Ping e Pong - [X] Ping e Pong
- [ ] Escrever testes automatizados - [ ] Escrever testes automatizados
- [ ] SSL - [ ] SSL
- [ ] Expvar - Canais, inscritos - [X] Expvar - Canais, inscritos
- [-] Otimizações [1/3] - [-] Otimizações [1/3]
- [ ] Refatorar partes do código, remover repetições - [ ] Refatorar partes do código, remover repetições
- [X] Alterar tipos de dados de slices para mapas em alguns locais. - [X] Alterar tipos de dados de slices para mapas em alguns locais.
+27 -3
View File
@@ -6,11 +6,28 @@ package main
import ( import (
"errors" "errors"
"expvar"
"sync" "sync"
log "github.com/golang/glog" 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 // An App
type App struct { type App struct {
sync.Mutex sync.Mutex
@@ -94,22 +111,25 @@ func (a *App) Disconnect(socketID string) {
// Remove from app // Remove from app
a.Lock() a.Lock()
defer a.Unlock()
_, exists := a.Subscribers[s.SocketID] _, exists := a.Subscribers[s.SocketID]
if !exists { if !exists {
return return
} }
delete(a.Subscribers, s.SocketID) delete(a.Subscribers, s.SocketID)
expSubscribers.Set(int64(len(a.Subscribers)))
a.Unlock()
} }
// Connect a new Subscriber // Connect a new Subscriber
func (a *App) Connect(s *Subscriber) { func (a *App) Connect(s *Subscriber) {
log.Infof("Adding a new Subscriber %s to app %s", s.SocketID, a.Name) log.Infof("Adding a new Subscriber %s to app %s", s.SocketID, a.Name)
a.Lock() a.Lock()
defer a.Unlock()
a.Subscribers[s.SocketID] = s a.Subscribers[s.SocketID] = s
a.Unlock() expSubscribers.Set(int64(len(a.Subscribers)))
} }
// Find a Subscriber on this app // Find a Subscriber on this app
@@ -131,6 +151,8 @@ func (a *App) AddChannel(c *Channel) {
a.Lock() a.Lock()
a.Channels[c.ChannelID] = c a.Channels[c.ChannelID] = c
a.Unlock() a.Unlock()
expChannels.Set(int64(len(a.Channels)))
} }
// Returns a Channel from this app // 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 { func (a *App) Publish(c *Channel, event RawEvent, ignore string) error {
expMessages.Add(1)
return c.Publish(a, event, ignore) return c.Publish(a, event, ignore)
} }
+3 -2
View File
@@ -8,8 +8,9 @@ import "errors"
// The config file // The config file
type ConfigFile struct { type ConfigFile struct {
Host string // The host, eg: :8080 will start on 0.0.0.0:8080 Host string // The host, eg: :8080 will start on 0.0.0.0:8080
Apps []*App Expvar bool
Apps []*App
} }
// Error for App not found // Error for App not found
+5
View File
@@ -5,6 +5,7 @@
package main package main
import ( import (
_ "expvar"
"net/http" "net/http"
"os" "os"
@@ -17,6 +18,10 @@ import (
func NewRouter() *mux.Router { func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true) router := mux.NewRouter().StrictSlash(true)
if Conf.Expvar {
router.Handle("/debug/vars", http.DefaultServeMux)
}
for _, route := range routes { for _, route := range routes {
var handler http.Handler var handler http.Handler