1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-09-15 17:46:55 +00:00

Add /get_sub_count

This commit is contained in:
Kane York 2016-07-08 19:16:10 -07:00
parent 328363ab6b
commit 972437cd4a
3 changed files with 35 additions and 0 deletions

View file

@ -102,6 +102,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
serveMux.HandleFunc("/drop_backlog", HTTPBackendDropBacklog)
serveMux.HandleFunc("/uncached_pub", HTTPBackendUncachedPublish)
serveMux.HandleFunc("/cached_pub", HTTPBackendCachedPublish)
serveMux.HandleFunc("/get_sub_count", HTTPGetSubscriberCount)
announceForm, err := Backend.SealRequest(url.Values{
"startup": []string{"1"},

View file

@ -220,3 +220,20 @@ func HTTPBackendUncachedPublish(w http.ResponseWriter, r *http.Request) {
}
fmt.Fprint(w, count)
}
// HTTPGetSubscriberCount handles the /get_sub_count route.
// It replies with the number of clients subscribed to a pub/sub topic.
// A "global" option is not available, use fetch(/stats).CurrentClientCount instead.
func HTTPGetSubscriberCount(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
formData, err := Backend.UnsealRequest(r.Form)
if err != nil {
w.WriteHeader(403)
fmt.Fprintf(w, "Error: %v", err)
return
}
channel := formData.Get("channel")
fmt.Fprint(w, CountSubscriptions(strings.Split(channel, ",")))
}

View file

@ -19,6 +19,23 @@ var ChatSubscriptionLock sync.RWMutex
var GlobalSubscriptionInfo []*ClientInfo
var GlobalSubscriptionLock sync.RWMutex
func CountSubscriptions(channels []string) int {
ChatSubscriptionLock.RLock()
defer ChatSubscriptionLock.RUnlock()
count := 0
for _, channelName := range channels {
list := ChatSubscriptionInfo[channelName]
if list != nil {
list.RLock()
count += len(list.Members)
list.RUnlock()
}
}
return count
}
func SubscribeChannel(client *ClientInfo, channelName string) {
ChatSubscriptionLock.RLock()
_subscribeWhileRlocked(channelName, client.MessageChannel)