From eb1ecce7e089a072a31d1ec51537964f707b02e2 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Wed, 11 Feb 2015 10:58:04 +0100 Subject: [PATCH] Avoid the need for another mutex in Client by moving cleanup client checks to Hub. --- src/app/spreed-webrtc-server/channelling_api.go | 6 +++--- src/app/spreed-webrtc-server/client.go | 12 +++--------- src/app/spreed-webrtc-server/hub.go | 14 ++++++++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/app/spreed-webrtc-server/channelling_api.go b/src/app/spreed-webrtc-server/channelling_api.go index 400c5d14..e3cbce04 100644 --- a/src/app/spreed-webrtc-server/channelling_api.go +++ b/src/app/spreed-webrtc-server/channelling_api.go @@ -32,7 +32,7 @@ const ( type ChannellingAPI interface { OnConnect(Client, *Session) - OnDisconnect(string) + OnDisconnect(Client, *Session) OnIncoming(ResponseSender, *Session, *DataIncoming) } @@ -65,8 +65,8 @@ func (api *channellingAPI) OnConnect(client Client, session *Session) { api.SendSelf(client, session) } -func (api *channellingAPI) OnDisconnect(sessionID string) { - api.Unicaster.OnDisconnect(sessionID) +func (api *channellingAPI) OnDisconnect(client Client, session *Session) { + api.Unicaster.OnDisconnect(client, session) } func (api *channellingAPI) OnIncoming(c ResponseSender, session *Session, msg *DataIncoming) { diff --git a/src/app/spreed-webrtc-server/client.go b/src/app/spreed-webrtc-server/client.go index f069dc90..8900df7b 100644 --- a/src/app/spreed-webrtc-server/client.go +++ b/src/app/spreed-webrtc-server/client.go @@ -50,12 +50,11 @@ type client struct { Codec ChannellingAPI Connection - session *Session - replaced bool + session *Session } func NewClient(codec Codec, api ChannellingAPI, session *Session) *client { - return &client{codec, api, nil, session, false} + return &client{codec, api, nil, session} } func (client *client) OnConnect(conn Connection) { @@ -65,11 +64,7 @@ func (client *client) OnConnect(conn Connection) { func (client *client) OnDisconnect() { client.session.Close() - if client.replaced { - log.Printf("Not cleaning up session %s as client %d was replaced\n", client.session.Id, client.Index()) - return - } - client.ChannellingAPI.OnDisconnect(client.session.Id) + client.ChannellingAPI.OnDisconnect(client, client.session) } func (client *client) OnText(b Buffer) { @@ -94,7 +89,6 @@ func (client *client) Session() *Session { } func (client *client) ReplaceAndClose() { - client.replaced = true client.session.Close() if client.Connection != nil { client.Connection.Close() diff --git a/src/app/spreed-webrtc-server/hub.go b/src/app/spreed-webrtc-server/hub.go index 74d04860..a5aefc12 100644 --- a/src/app/spreed-webrtc-server/hub.go +++ b/src/app/spreed-webrtc-server/hub.go @@ -48,8 +48,8 @@ type SessionStore interface { type Unicaster interface { SessionStore OnConnect(Client, *Session) + OnDisconnect(Client, *Session) Unicast(to string, outgoing *DataOutgoing) - OnDisconnect(sessionID string) } type ContactManager interface { @@ -161,10 +161,16 @@ func (h *hub) OnConnect(client Client, session *Session) { h.mutex.Unlock() } -func (h *hub) OnDisconnect(sessionID string) { - log.Printf("Cleaning up session id %s\n", sessionID) +func (h *hub) OnDisconnect(client Client, session *Session) { h.mutex.Lock() - delete(h.clients, sessionID) + if ec, ok := h.clients[session.Id]; ok { + if ec == client { + log.Printf("Cleaning up client %d for session id %s\n", ec.Index(), session.Id) + delete(h.clients, session.Id) + } else { + log.Printf("Not cleaning up session %s as client %d was replaced with %d\n", session.Id, client.Index(), ec.Index()) + } + } h.mutex.Unlock() }