Memory leak counter measure test and web ui bug fix + improvements

This commit is contained in:
da
2017-01-22 00:20:04 +01:00
parent 1e50475a46
commit 077e3b7a8d
7 changed files with 77 additions and 39 deletions
+36 -6
View File
@@ -39,6 +39,9 @@ var (
// mutex for checking if voice connection already exists // mutex for checking if voice connection already exists
mutex = &sync.Mutex{} mutex = &sync.Mutex{}
// connection refresher counter
rcCounter int = 0
) )
// Play represents an individual use of the !airhorn command // Play represents an individual use of the !airhorn command
@@ -57,11 +60,10 @@ type Play struct {
// SoundCollection of Sounds // SoundCollection of Sounds
type SoundCollection struct { type SoundCollection struct {
Prefix string Prefix string
Commands []string Commands []string
Sounds []*Sound Sounds []*Sound
ChainWith *SoundCollection ChainWith *SoundCollection
soundRange int soundRange int
} }
@@ -351,8 +353,8 @@ func playSound(play *Play, vc *discordgo.VoiceConnection) (err error) {
time.Sleep(time.Millisecond * time.Duration(play.Sound.PartDelay)) time.Sleep(time.Millisecond * time.Duration(play.Sound.PartDelay))
mutex.Lock() mutex.Lock()
delete(queues, play.GuildID) delete(queues, play.GuildID)
mutex.Unlock()
vc.Disconnect() vc.Disconnect()
mutex.Unlock()
return nil return nil
} }
@@ -458,7 +460,35 @@ func setIdleStatus() {
} }
} }
func checkReconnectCounter() {
mutex.Lock()
if rcCounter >= 20 {
reconnect()
rcCounter = 0
} else {
rcCounter++
}
mutex.Unlock()
}
func reconnect() {
err := discord.Close()
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Failed to close connection.")
return
}
err = discord.Open()
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Failed to create discord websocket connection")
}
}
func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
go checkReconnectCounter()
if m.Content == "ping" || m.Content == "pong" { if m.Content == "ping" || m.Content == "pong" {
// If the message is "ping" reply with "Pong!" // If the message is "ping" reply with "Pong!"
if m.Content == "ping" { if m.Content == "ping" {
+6 -7
View File
@@ -15,11 +15,12 @@
body { body {
padding-top: 50px; padding-top: 50px;
} }
.vcenter { .itembuffer {
display: inline-block; margin: 1px;
vertical-align: middle; padding: 5px;
float: none; border: 1px solid black;
} background-color: #f2f2f2;
}
</style> </style>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
@@ -55,5 +56,3 @@
<h1>Gidbig</h1> <h1>Gidbig</h1>
<p class="lead">{{ template "sessionHandler" . }}</p> <p class="lead">{{ template "sessionHandler" . }}</p>
{{ end }} {{ end }}
+1
View File
@@ -0,0 +1 @@
<div class="col-sm-3 text-center"><div class="itembuffer">{{ .Itemtext }}<br/><a class="btn btn-default" href="javascript:playSound('{{ .Itemcommand }}', '{{ .Itemsoundname }}')" role="button">Play!</a></div></div>
-5
View File
@@ -1,5 +0,0 @@
<div class="row">
{{ range . }}
<div class="col-sm-3 text-center">{{ .Itemtext }}<br/><a class="btn btn-primary" href="javascript:playSound('{{ .Itemcommand }}', '{{ .Itemsoundname }}')" role="button">Play!</a></div>
{{ end }}
</div>
+1
View File
@@ -0,0 +1 @@
</div>
+1
View File
@@ -0,0 +1 @@
<div class="row">
+32 -21
View File
@@ -48,7 +48,9 @@ type SoundItem struct {
func startWebServer(port string, ci string, cs string, redirectURL string) { func startWebServer(port string, ci string, cs string, redirectURL string) {
tmpls["home.html"] = template.Must(template.ParseFiles(templateDir+"home.html", header, footer)) tmpls["home.html"] = template.Must(template.ParseFiles(templateDir+"home.html", header, footer))
tmpls["internal.html"] = template.Must(template.ParseFiles(templateDir+"internal.html", header, footer)) tmpls["internal.html"] = template.Must(template.ParseFiles(templateDir+"internal.html", header, footer))
tmpls["itemrow.html"] = template.Must(template.ParseFiles(templateDir + "itemrow.html")) tmpls["item.html"] = template.Must(template.ParseFiles(templateDir + "item.html"))
tmpls["itemrowstart.html"] = template.Must(template.ParseFiles(templateDir + "itemrowstart.html"))
tmpls["itemrowend.html"] = template.Must(template.ParseFiles(templateDir + "itemrowend.html"))
store = sessions.NewCookieStore([]byte(cs)) store = sessions.NewCookieStore([]byte(cs))
discordOauthConfig.ClientID = ci discordOauthConfig.ClientID = ci
discordOauthConfig.ClientSecret = cs discordOauthConfig.ClientSecret = cs
@@ -67,7 +69,7 @@ func startWebServer(port string, ci string, cs string, redirectURL string) {
func handlePlaySound(w http.ResponseWriter, r *http.Request) { func handlePlaySound(w http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
sound, soundCollection := findSoundAndCollection(r.FormValue("command"), r.FormValue("soundname")) sound, soundCollection := findSoundAndCollection(r.FormValue("command"), r.FormValue("soundname"))
session, _ := store.Get(r, "session-name") session, _ := store.Get(r, "gidbig-session")
var guild *discordgo.Guild var guild *discordgo.Guild
user, _ := discord.User(session.Values["discordUserID"].(string)) user, _ := discord.User(session.Values["discordUserID"].(string))
for _, g := range discord.State.Guilds { for _, g := range discord.State.Guilds {
@@ -83,36 +85,45 @@ func handlePlaySound(w http.ResponseWriter, r *http.Request) {
} }
func handleMain(w http.ResponseWriter, r *http.Request) { func handleMain(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name") session, _ := store.Get(r, "gidbig-session")
if session.Values["discordUsername"] != nil { if session.Values["discordUsername"] != nil {
err := tmpls["internal.html"].ExecuteTemplate(w, "header", map[string]interface{}{}) err := tmpls["internal.html"].ExecuteTemplate(w, "header", map[string]interface{}{})
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
var si [4]SoundItem
cnt := 0 var si []SoundItem
for _, sc := range COLLECTIONS { for _, sc := range COLLECTIONS {
for _, snd := range sc.Sounds { for _, snd := range sc.Sounds {
si[cnt%4].Itemcommand = "!" + sc.Prefix si = append(si, SoundItem{
si[cnt%4].Itemsoundname = snd.Name Itemcommand: "!" + sc.Prefix,
si[cnt%4].Itemtext = si[cnt%4].Itemcommand + " " + si[cnt%4].Itemsoundname Itemsoundname: snd.Name,
if cnt != 0 && cnt%4 == 3 { Itemtext: "!" + sc.Prefix + " " + snd.Name,
err = tmpls["itemrow.html"].Execute(w, si) })
if err != nil {
fmt.Println(err)
return
}
}
cnt++
} }
} }
if cnt%4 != 0 {
err = tmpls["itemrow.html"].Execute(w, si) for i, snd := range si {
if i%4 == 0 {
err = tmpls["itemrowstart.html"].Execute(w, nil)
if err != nil {
fmt.Println(err)
return
}
}
err = tmpls["item.html"].Execute(w, snd)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
if i%4 == 3 {
err = tmpls["itemrowend.html"].Execute(w, nil)
if err != nil {
fmt.Println(err)
return
}
}
} }
err = tmpls["internal.html"].ExecuteTemplate(w, "footer", map[string]interface{}{}) err = tmpls["internal.html"].ExecuteTemplate(w, "footer", map[string]interface{}{})
@@ -127,7 +138,7 @@ func handleMain(w http.ResponseWriter, r *http.Request) {
} }
func handleLogout(w http.ResponseWriter, r *http.Request) { func handleLogout(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{ cookie := &http.Cookie{
Name: "session-name", Name: "gidbig-session",
Value: "", Value: "",
Path: "/", Path: "/",
MaxAge: -1, MaxAge: -1,
@@ -140,7 +151,7 @@ func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
rand.Read(b) rand.Read(b)
oauthStateString = base64.URLEncoding.EncodeToString(b) oauthStateString = base64.URLEncoding.EncodeToString(b)
session, _ := store.Get(r, "session-name") session, _ := store.Get(r, "gidbig-session")
session.Values["state"] = oauthStateString session.Values["state"] = oauthStateString
session.Save(r, w) session.Save(r, w)
@@ -149,7 +160,7 @@ func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
} }
func handlediscordCallback(w http.ResponseWriter, r *http.Request) { func handlediscordCallback(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session-name") session, err := store.Get(r, "gidbig-session")
if err != nil { if err != nil {
fmt.Fprintln(w, "aborted") fmt.Fprintln(w, "aborted")
return return