1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-07-03 01:28:30 +00:00

lol commit messages

This commit is contained in:
Kane York 2015-10-27 21:21:06 -07:00
parent e9801e6ca3
commit 74bc15e0e9
8 changed files with 117 additions and 31 deletions

View file

@ -0,0 +1,47 @@
package main
import (
"../../internal/server"
"fmt"
"github.com/abiosoft/ishell"
"runtime"
)
func commandLineConsole() {
shell := ishell.NewShell()
shell.Register("clientcount", func(args ...string) (string, error) {
server.GlobalSubscriptionInfo.RLock()
count := len(server.GlobalSubscriptionInfo.Members)
server.GlobalSubscriptionInfo.RUnlock()
return fmt.Sprintln(count, "clients connected"), nil
})
shell.Register("globalnotice", func(args ...string) (string, error) {
msg := server.ClientMessage{
MessageID: -1,
Command: "message",
Arguments: args[0],
}
server.PublishToAll(msg)
return "Message sent.", nil
})
shell.Register("memstatsbysize", func(args ...string) (string, error) {
runtime.GC()
m := runtime.MemStats{}
runtime.ReadMemStats(&m)
for _, val := range m.BySize {
if val.Mallocs == 0 {
continue
}
shell.Println(val.Size, "bytes:", val.Mallocs, "allocs", val.Frees, "frees")
}
shell.Println(m.NumGC, "collections occurred")
return "", nil
})
shell.Start()
}

View file

@ -46,6 +46,8 @@ func main() {
server.SetupServerAndHandle(conf, httpServer.TLSConfig, nil) server.SetupServerAndHandle(conf, httpServer.TLSConfig, nil)
go commandLineConsole()
if conf.UseSSL { if conf.UseSSL {
err = httpServer.ListenAndServeTLS(conf.SSLCertificateFile, conf.SSLKeyFile) err = httpServer.ListenAndServeTLS(conf.SSLCertificateFile, conf.SSLKeyFile)
} else { } else {

View file

@ -214,6 +214,31 @@ func SendTimedBacklogMessages(client *ClientInfo, disconnectTime time.Time) {
client.Mutex.Unlock() client.Mutex.Unlock()
} }
func TimedBacklogJanitor() {
for {
time.Sleep(1 * time.Hour)
CleanupTimedBacklogMessages()
}
}
func CleanupTimedBacklogMessages() {
CacheListsLock.Lock()
oneHourAgo := time.Now().Add(-24 * time.Hour)
globIdx := FindFirstNewMessage(tgmarray(CachedGlobalMessages), oneHourAgo)
if globIdx != -1 {
newGlobMsgs := make([]TimestampedGlobalMessage, len(CachedGlobalMessages)-globIdx)
copy(newGlobMsgs, CachedGlobalMessages[globIdx:])
CachedGlobalMessages = newGlobMsgs
}
chanIdx := FindFirstNewMessage(tmmarray(CachedChannelMessages), oneHourAgo)
if chanIdx != -1 {
newChanMsgs := make([]TimestampedMultichatMessage, len(CachedChannelMessages)-chanIdx)
copy(newChanMsgs, CachedChannelMessages[chanIdx:])
CachedChannelMessages = newChanMsgs
}
CacheListsLock.Unlock()
}
func InsertionSort(ary sort.Interface) { func InsertionSort(ary sort.Interface) {
for i := 1; i < ary.Len(); i++ { for i := 1; i < ary.Len(); i++ {
for j := i; j > 0 && ary.Less(j, j-1); j-- { for j := i; j > 0 && ary.Less(j, j-1); j-- {

View file

@ -5,6 +5,10 @@ import (
"time" "time"
) )
func TestCleanupBacklogMessages(t *testing.T) {
}
func TestFindFirstNewMessageEmpty(t *testing.T) { func TestFindFirstNewMessageEmpty(t *testing.T) {
CachedGlobalMessages = []TimestampedGlobalMessage{} CachedGlobalMessages = []TimestampedGlobalMessage{}
i := FindFirstNewMessage(tgmarray(CachedGlobalMessages), time.Unix(10, 0)) i := FindFirstNewMessage(tgmarray(CachedGlobalMessages), time.Unix(10, 0))

View file

@ -127,13 +127,13 @@ func HandleSub(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rms
AddToSliceS(&client.CurrentChannels, channel) AddToSliceS(&client.CurrentChannels, channel)
client.PendingSubscriptionsBacklog = append(client.PendingSubscriptionsBacklog, channel) client.PendingSubscriptionsBacklog = append(client.PendingSubscriptionsBacklog, channel)
if client.MakePendingRequests == nil { // if client.MakePendingRequests == nil {
client.MakePendingRequests = time.AfterFunc(ChannelInfoDelay, GetSubscriptionBacklogFor(conn, client)) // client.MakePendingRequests = time.AfterFunc(ChannelInfoDelay, GetSubscriptionBacklogFor(conn, client))
} else { // } else {
if !client.MakePendingRequests.Reset(ChannelInfoDelay) { // if !client.MakePendingRequests.Reset(ChannelInfoDelay) {
client.MakePendingRequests = time.AfterFunc(ChannelInfoDelay, GetSubscriptionBacklogFor(conn, client)) // client.MakePendingRequests = time.AfterFunc(ChannelInfoDelay, GetSubscriptionBacklogFor(conn, client))
} // }
} // }
client.Mutex.Unlock() client.Mutex.Unlock()

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"io"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -74,43 +75,44 @@ var gconfig *ConfigFile
// Create a websocket.Server with the options from the provided Config. // Create a websocket.Server with the options from the provided Config.
func setupServer(config *ConfigFile, tlsConfig *tls.Config) *websocket.Server { func setupServer(config *ConfigFile, tlsConfig *tls.Config) *websocket.Server {
gconfig = config gconfig = config
sockConf, err := websocket.NewConfig("/", config.SocketOrigin) // sockConf, err := websocket.NewConfig("/", config.SocketOrigin)
if err != nil { // if err != nil {
log.Fatal(err) // log.Fatal(err)
} // }
SetupBackend(config) SetupBackend(config)
if config.UseSSL { // if config.UseSSL {
cert, err := tls.LoadX509KeyPair(config.SSLCertificateFile, config.SSLKeyFile) // cert, err := tls.LoadX509KeyPair(config.SSLCertificateFile, config.SSLKeyFile)
if err != nil { // if err != nil {
log.Fatal(err) // log.Fatal(err)
} // }
tlsConfig.Certificates = []tls.Certificate{cert} // tlsConfig.Certificates = []tls.Certificate{cert}
tlsConfig.ServerName = config.SocketOrigin // tlsConfig.ServerName = config.SocketOrigin
tlsConfig.BuildNameToCertificate() // tlsConfig.BuildNameToCertificate()
sockConf.TlsConfig = tlsConfig // sockConf.TlsConfig = tlsConfig
// }
} // sockServer := &websocket.Server{}
// sockServer.Config = *sockConf
sockServer := &websocket.Server{} // sockServer.Handler = HandleSocketConnection
sockServer.Config = *sockConf
sockServer.Handler = HandleSocketConnection
go deadChannelReaper() go deadChannelReaper()
return sockServer return nil
} }
// Set up a websocket listener and register it on /. // Set up a websocket listener and register it on /.
// (Uses http.DefaultServeMux .) // (Uses http.DefaultServeMux .)
func SetupServerAndHandle(config *ConfigFile, tlsConfig *tls.Config, serveMux *http.ServeMux) { func SetupServerAndHandle(config *ConfigFile, tlsConfig *tls.Config, serveMux *http.ServeMux) {
sockServer := setupServer(config, tlsConfig) _ = setupServer(config, tlsConfig)
log.Print("hi")
if serveMux == nil { if serveMux == nil {
serveMux = http.DefaultServeMux serveMux = http.DefaultServeMux
} }
serveMux.HandleFunc("/", ServeWebsocketOrCatbag(sockServer.ServeHTTP)) handler := websocket.Handler(HandleSocketConnection)
serveMux.HandleFunc("/", ServeWebsocketOrCatbag(handler.ServeHTTP))
serveMux.HandleFunc("/pub_msg", HBackendPublishRequest) serveMux.HandleFunc("/pub_msg", HBackendPublishRequest)
serveMux.HandleFunc("/dump_backlog", HBackendDumpBacklog) serveMux.HandleFunc("/dump_backlog", HBackendDumpBacklog)
serveMux.HandleFunc("/update_and_pub", HBackendUpdateAndPublish) serveMux.HandleFunc("/update_and_pub", HBackendUpdateAndPublish)
@ -132,6 +134,8 @@ func ServeWebsocketOrCatbag(sockfunc func(http.ResponseWriter, *http.Request)) h
func HandleSocketConnection(conn *websocket.Conn) { func HandleSocketConnection(conn *websocket.Conn) {
// websocket.Conn is a ReadWriteCloser // websocket.Conn is a ReadWriteCloser
fmt.Println("Got socket connection from", conn.Request().RemoteAddr)
var _closer sync.Once var _closer sync.Once
closer := func() { closer := func() {
_closer.Do(func() { _closer.Do(func() {
@ -156,6 +160,10 @@ func HandleSocketConnection(conn *websocket.Conn) {
} }
clientChan <- msg clientChan <- msg
} }
if err != io.EOF {
fmt.Println("Error while reading from client:", err)
}
errorChan <- err errorChan <- err
close(errorChan) close(errorChan)
close(clientChan) close(clientChan)
@ -198,6 +206,7 @@ RunLoop:
} }
// Exit // Exit
fmt.Println("End socket connection from", conn.Request().RemoteAddr)
// Launch message draining goroutine - we aren't out of the pub/sub records // Launch message draining goroutine - we aren't out of the pub/sub records
go func() { go func() {

View file

@ -37,7 +37,6 @@ FFZ.prototype.ws_iframe = function() {
FFZ.prototype.ws_create = function() { FFZ.prototype.ws_create = function() {
// Disable sockets for now. // Disable sockets for now.
return;
var f = this, ws; var f = this, ws;