Close Leaked goroutine

This commit is contained in:
giongto35 2019-05-08 23:59:47 +08:00
parent 322b5bffe0
commit 0eea6cfcc8
11 changed files with 144 additions and 72 deletions

View file

@ -75,12 +75,11 @@ func (h *Handler) GetWeb(w http.ResponseWriter, r *http.Request) {
// WS handles normal traffic (from browser to host)
func (h *Handler) WS(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
defer c.Close()
if err != nil {
log.Print("[!] WS upgrade:", err)
return
}
defer c.Close()
client := NewBrowserClient(c)
sessionID := uuid.Must(uuid.NewV4()).String()
@ -91,6 +90,7 @@ func (h *Handler) WS(w http.ResponseWriter, r *http.Request) {
peerconnection: webrtc.NewWebRTC(),
handler: h,
}
defer wssession.Close()
if wssession.OverlordClient != nil {
wssession.RouteOverlord()

View file

@ -74,7 +74,7 @@ func NewRoom(roomID, gamepath, gameName string, onlineStorage *storage.Client) *
if !room.isGameOnLocal(savepath) {
// Fetch room from GCP to server
log.Println("Load room from online storage", savepath)
if err := room.loadRoomOnline(roomID, savepath); err != nil {
if err := room.saveOnlineRoomToLocal(roomID, savepath); err != nil {
log.Printf("Warn: Room %s is not in online storage, error %s", roomID, err)
}
}
@ -111,7 +111,7 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int
for {
select {
case <-r.Done:
log.Println("Close listening from peerconnection for room", r.ID)
log.Println("Detach peerconnection from room", r.ID)
return
case <-peerconnection.Done:
r.removeSession(peerconnection)
@ -196,7 +196,7 @@ func (r *Room) SaveGame() error {
return nil
}
func (r *Room) loadRoomOnline(roomID string, savepath string) error {
func (r *Room) saveOnlineRoomToLocal(roomID string, savepath string) error {
log.Println("Try loading game from cloud storage")
// If the game is not on local server
// Try to load from gcloud
@ -206,11 +206,7 @@ func (r *Room) loadRoomOnline(roomID string, savepath string) error {
}
// Save the data fetched from gcloud to local server
ioutil.WriteFile(savepath, data, 0644)
// Reload game again
//err = r.director.LoadGame(nil)
//if err != nil {
//return err
//}
return nil
}

View file

@ -5,7 +5,8 @@ import (
)
// Session represents a session connected from the browser to the current server
// It involves one connection to browser and one connection to the overlord
// It requires one connection to browser and one connection to the overlord
// connection to browser is 1-1. connection to overlord is n - 1
// Peerconnection can be from other server to ensure better latency
type Session struct {
ID string
@ -21,3 +22,7 @@ type Session struct {
RoomID string
PlayerIndex int
}
func (s *Session) Close() {
s.peerconnection.StopClient()
}