mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-07-03 17:48:30 +00:00
Add Health to /stats, add irc reconnect
This commit is contained in:
parent
88b356177a
commit
d49c88a222
4 changed files with 63 additions and 9 deletions
|
@ -37,7 +37,11 @@ var serverID int
|
||||||
|
|
||||||
var messageBufferPool sync.Pool
|
var messageBufferPool sync.Pool
|
||||||
|
|
||||||
|
var lastBackendSuccess map[string]time.Time
|
||||||
|
|
||||||
func setupBackend(config *ConfigFile) {
|
func setupBackend(config *ConfigFile) {
|
||||||
|
serverID = config.ServerID
|
||||||
|
|
||||||
backendHTTPClient.Timeout = 60 * time.Second
|
backendHTTPClient.Timeout = 60 * time.Second
|
||||||
backendURL = config.BackendURL
|
backendURL = config.BackendURL
|
||||||
if responseCache != nil {
|
if responseCache != nil {
|
||||||
|
@ -48,13 +52,20 @@ func setupBackend(config *ConfigFile) {
|
||||||
announceStartupURL = fmt.Sprintf("%s%s", backendURL, bPathAnnounceStartup)
|
announceStartupURL = fmt.Sprintf("%s%s", backendURL, bPathAnnounceStartup)
|
||||||
addTopicURL = fmt.Sprintf("%s%s", backendURL, bPathAddTopic)
|
addTopicURL = fmt.Sprintf("%s%s", backendURL, bPathAddTopic)
|
||||||
postStatisticsURL = fmt.Sprintf("%s%s", backendURL, bPathAggStats)
|
postStatisticsURL = fmt.Sprintf("%s%s", backendURL, bPathAggStats)
|
||||||
|
epochTime := time.Unix(0, 0)
|
||||||
|
lastBackendSuccess = map[string]time.Time{
|
||||||
|
bPathAnnounceStartup: epochTime,
|
||||||
|
bPathAddTopic: epochTime,
|
||||||
|
bPathAggStats: epochTime,
|
||||||
|
bPathOtherCommand: epochTime,
|
||||||
|
}
|
||||||
|
Statistics.Health.Backend = lastBackendSuccess
|
||||||
|
|
||||||
messageBufferPool.New = New4KByteBuffer
|
messageBufferPool.New = New4KByteBuffer
|
||||||
|
|
||||||
var theirPublic, ourPrivate [32]byte
|
var theirPublic, ourPrivate [32]byte
|
||||||
copy(theirPublic[:], config.BackendPublicKey)
|
copy(theirPublic[:], config.BackendPublicKey)
|
||||||
copy(ourPrivate[:], config.OurPrivateKey)
|
copy(ourPrivate[:], config.OurPrivateKey)
|
||||||
serverID = config.ServerID
|
|
||||||
|
|
||||||
box.Precompute(&backendSharedKey, &theirPublic, &ourPrivate)
|
box.Precompute(&backendSharedKey, &theirPublic, &ourPrivate)
|
||||||
}
|
}
|
||||||
|
@ -197,6 +208,8 @@ func SendRemoteCommand(remoteCommand, data string, auth AuthInfo) (responseStr s
|
||||||
responseCache.Set(getCacheKey(remoteCommand, data), responseStr, duration)
|
responseCache.Set(getCacheKey(remoteCommand, data), responseStr, duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastBackendSuccess[bPathOtherCommand] = time.Now()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,6 +224,8 @@ func SendAggregatedData(sealedForm url.Values) error {
|
||||||
return httpError(resp.StatusCode)
|
return httpError(resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastBackendSuccess[bPathAggStats] = time.Now()
|
||||||
|
|
||||||
return resp.Body.Close()
|
return resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,6 +286,8 @@ func sendTopicNotice(topic string, added bool) error {
|
||||||
return ErrBackendNotOK{Code: resp.StatusCode, Response: respStr}
|
return ErrBackendNotOK{Code: resp.StatusCode, Response: respStr}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastBackendSuccess[bPathAddTopic] = time.Now()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -94,6 +94,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
||||||
|
|
||||||
serveMux.HandleFunc("/", HTTPHandleRootURL)
|
serveMux.HandleFunc("/", HTTPHandleRootURL)
|
||||||
serveMux.Handle("/.well-known/", http.FileServer(http.Dir("/tmp/letsencrypt/")))
|
serveMux.Handle("/.well-known/", http.FileServer(http.Dir("/tmp/letsencrypt/")))
|
||||||
|
serveMux.HandleFunc("/healthcheck", HTTPSayOK)
|
||||||
serveMux.HandleFunc("/stats", HTTPShowStatistics)
|
serveMux.HandleFunc("/stats", HTTPShowStatistics)
|
||||||
serveMux.HandleFunc("/hll/", HTTPShowHLL)
|
serveMux.HandleFunc("/hll/", HTTPShowHLL)
|
||||||
serveMux.HandleFunc("/hll_force_write", HTTPWriteHLL)
|
serveMux.HandleFunc("/hll_force_write", HTTPWriteHLL)
|
||||||
|
@ -113,6 +114,7 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
||||||
log.Println("could not announce startup to backend:", err)
|
log.Println("could not announce startup to backend:", err)
|
||||||
} else {
|
} else {
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
lastBackendSuccess[bPathAnnounceStartup] = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
if Configuration.UseESLogStashing {
|
if Configuration.UseESLogStashing {
|
||||||
|
@ -167,7 +169,7 @@ func shutdownHandler() {
|
||||||
func dumpStackOnCtrlZ() {
|
func dumpStackOnCtrlZ() {
|
||||||
ch := make(chan os.Signal)
|
ch := make(chan os.Signal)
|
||||||
signal.Notify(ch, syscall.SIGTSTP)
|
signal.Notify(ch, syscall.SIGTSTP)
|
||||||
for _ = range ch {
|
for range ch {
|
||||||
fmt.Println("Got ^Z")
|
fmt.Println("Got ^Z")
|
||||||
|
|
||||||
buf := make([]byte, 10000)
|
buf := make([]byte, 10000)
|
||||||
|
@ -176,6 +178,13 @@ func dumpStackOnCtrlZ() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPSayOK replies with 200 and a body of "ok\n".
|
||||||
|
func HTTPSayOK(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.(interface {
|
||||||
|
WriteString(string) error
|
||||||
|
}).WriteString("ok\n")
|
||||||
|
}
|
||||||
|
|
||||||
// SocketUpgrader is the websocket.Upgrader currently in use.
|
// SocketUpgrader is the websocket.Upgrader currently in use.
|
||||||
var SocketUpgrader = websocket.Upgrader{
|
var SocketUpgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
|
|
|
@ -88,15 +88,38 @@ const AuthChannelName = "frankerfacezauthorizer"
|
||||||
const AuthChannel = "#" + AuthChannelName
|
const AuthChannel = "#" + AuthChannelName
|
||||||
const AuthCommand = "AUTH"
|
const AuthCommand = "AUTH"
|
||||||
|
|
||||||
|
var authIrcConnection *irc.Conn
|
||||||
|
|
||||||
// is_init_func
|
// is_init_func
|
||||||
func ircConnection() {
|
func ircConnection() {
|
||||||
|
|
||||||
c := irc.SimpleClient("justinfan123")
|
c := irc.SimpleClient("justinfan123")
|
||||||
|
c.Config().Server = "irc.chat.twitch.tv"
|
||||||
|
authIrcConnection = c
|
||||||
|
|
||||||
|
var reconnect func(conn *irc.Conn)
|
||||||
|
connect := func(conn *irc.Conn) {
|
||||||
|
err := c.Connect()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("irc: failed to connect to IRC:", err)
|
||||||
|
go reconnect(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reconnect = func(conn *irc.Conn) {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
log.Println("irc: Reconnecting…")
|
||||||
|
connect(conn)
|
||||||
|
}
|
||||||
|
|
||||||
c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
|
c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
|
||||||
conn.Join(AuthChannel)
|
conn.Join(AuthChannel)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
c.HandleFunc(irc.DISCONNECTED, func(conn *irc.Conn, line *irc.Line) {
|
||||||
|
log.Println("irc: Disconnected. Reconnecting in 5 seconds.")
|
||||||
|
go reconnect(conn)
|
||||||
|
})
|
||||||
|
|
||||||
c.HandleFunc(irc.PRIVMSG, func(conn *irc.Conn, line *irc.Line) {
|
c.HandleFunc(irc.PRIVMSG, func(conn *irc.Conn, line *irc.Line) {
|
||||||
channel := line.Args[0]
|
channel := line.Args[0]
|
||||||
msg := line.Args[1]
|
msg := line.Args[1]
|
||||||
|
@ -115,11 +138,7 @@ func ircConnection() {
|
||||||
submitAuth(submittedUser, submittedChallenge)
|
submitAuth(submittedUser, submittedChallenge)
|
||||||
})
|
})
|
||||||
|
|
||||||
err := c.ConnectTo("irc.chat.twitch.tv")
|
connect(c)
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Cannot connect to IRC:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func submitAuth(user, challenge string) {
|
func submitAuth(user, challenge string) {
|
||||||
|
|
|
@ -21,6 +21,11 @@ type StatsData struct {
|
||||||
|
|
||||||
CachedStatsLastUpdate time.Time
|
CachedStatsLastUpdate time.Time
|
||||||
|
|
||||||
|
Health struct {
|
||||||
|
IRC bool
|
||||||
|
Backend map[string]time.Time
|
||||||
|
}
|
||||||
|
|
||||||
CurrentClientCount uint64
|
CurrentClientCount uint64
|
||||||
|
|
||||||
PubSubChannelCount int
|
PubSubChannelCount int
|
||||||
|
@ -76,7 +81,7 @@ func commandCounter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// StatsDataVersion is the version of the StatsData struct.
|
// StatsDataVersion is the version of the StatsData struct.
|
||||||
const StatsDataVersion = 5
|
const StatsDataVersion = 6
|
||||||
const pageSize = 4096
|
const pageSize = 4096
|
||||||
|
|
||||||
var cpuUsage struct {
|
var cpuUsage struct {
|
||||||
|
@ -154,6 +159,10 @@ func updatePeriodicStats() {
|
||||||
{
|
{
|
||||||
Statistics.Uptime = nowUpdate.Sub(Statistics.StartTime).String()
|
Statistics.Uptime = nowUpdate.Sub(Statistics.StartTime).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Statistics.Health.IRC = authIrcConnection.Connected()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sysMemLastUpdate time.Time
|
var sysMemLastUpdate time.Time
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue