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:
parent
328363ab6b
commit
972437cd4a
3 changed files with 35 additions and 0 deletions
|
@ -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"},
|
||||
|
|
|
@ -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, ",")))
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue