Refactor Export variables

Now the variables are separated by app and other stats were added.
This commit is contained in:
claudemiro
2015-01-13 00:00:34 -03:00
parent cadc72d482
commit ef1070887b
2 changed files with 27 additions and 23 deletions
+23 -21
View File
@@ -7,27 +7,12 @@ package main
import (
"errors"
"expvar"
"fmt"
"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
@@ -44,12 +29,15 @@ type App struct {
Channels map[string]*Channel `json:"-"`
Subscribers map[string]*Subscriber `json:"-"`
Stats *expvar.Map `json:"-"`
}
// Alloc memory for Subscribers and Channels
func (a *App) Init() {
a.Subscribers = make(map[string]*Subscriber)
a.Channels = make(map[string]*Channel)
a.Stats = expvar.NewMap(fmt.Sprintf("%s (%s)", a.Name, a.AppID))
}
// Only Presence channels
@@ -119,7 +107,8 @@ func (a *App) Disconnect(socketID string) {
}
delete(a.Subscribers, s.SocketID)
expSubscribers.Set(int64(len(a.Subscribers)))
a.Stats.Add("TotalSubscribers", -1)
}
// Connect a new Subscriber
@@ -129,7 +118,8 @@ func (a *App) Connect(s *Subscriber) {
defer a.Unlock()
a.Subscribers[s.SocketID] = s
expSubscribers.Set(int64(len(a.Subscribers)))
a.Stats.Add("TotalSubscribers", 1)
}
// Find a Subscriber on this app
@@ -149,10 +139,22 @@ func (a *App) AddChannel(c *Channel) {
log.Infof("Adding a new channel %s to app %s", c.ChannelID, a.Name)
a.Lock()
defer a.Unlock()
a.Channels[c.ChannelID] = c
a.Unlock()
expChannels.Set(int64(len(a.Channels)))
if c.IsPresence() {
a.Stats.Add("TotalPresenceChannels", 1)
}
if c.IsPrivate() {
a.Stats.Add("TotalPrivateChannels", 1)
}
if c.IsPublic() {
a.Stats.Add("TotalPublicChannels", 1)
}
a.Stats.Add("TotalChannels", 1)
}
// Returns a Channel from this app
@@ -181,7 +183,7 @@ func (a *App) FindChannelByChannelID(n string) (*Channel, error) {
}
func (a *App) Publish(c *Channel, event RawEvent, ignore string) error {
expMessages.Add(1)
a.Stats.Add("TotalUniqueMessages", 1)
return c.Publish(a, event, ignore)
}
+4 -2
View File
@@ -11,6 +11,8 @@ import (
"sync"
"time"
"strconv"
log "github.com/golang/glog"
"github.com/gorilla/websocket"
)
@@ -168,14 +170,14 @@ func newID() string {
currentID += 1
return string(currentID)
return strconv.Itoa(currentID)
}
// Create a new Subscriber
func NewSubscriber(socketID string, s *websocket.Conn) *Subscriber {
id := newID()
log.Infof("Creating a new Subscriber %+v with id %d", socketID, id)
log.Infof("Creating a new Subscriber %+v with id %s", socketID, id)
return &Subscriber{Id: id, SocketID: socketID, Socket: s}
}