Merge pull request #2147 from lucas-clemente/deleting-sessions

refactor handling of closed session
This commit is contained in:
Marten Seemann
2019-10-01 13:17:44 +07:00
committed by GitHub
14 changed files with 171 additions and 180 deletions

View File

@@ -66,12 +66,8 @@ func newPacketHandlerMap(
func (h *packetHandlerMap) Add(id protocol.ConnectionID, handler packetHandler) {
h.mutex.Lock()
h.addLocked(id, handler)
h.mutex.Unlock()
}
func (h *packetHandlerMap) addLocked(id protocol.ConnectionID, handler packetHandler) {
h.handlers[string(id)] = handler
h.mutex.Unlock()
}
func (h *packetHandlerMap) Remove(id protocol.ConnectionID) {
@@ -80,14 +76,6 @@ func (h *packetHandlerMap) Remove(id protocol.ConnectionID) {
h.mutex.Unlock()
}
func (h *packetHandlerMap) ReplaceWithClosed(id protocol.ConnectionID, handler packetHandler) {
h.mutex.Lock()
h.removeByConnectionIDAsString(string(id))
h.addLocked(id, handler)
h.mutex.Unlock()
h.retireByConnectionIDAsString(string(id))
}
func (h *packetHandlerMap) removeByConnectionIDAsString(id string) {
delete(h.handlers, id)
}
@@ -99,6 +87,9 @@ func (h *packetHandlerMap) Retire(id protocol.ConnectionID) {
func (h *packetHandlerMap) retireByConnectionIDAsString(id string) {
time.AfterFunc(h.deleteRetiredSessionsAfter, func() {
h.mutex.Lock()
if sess, ok := h.handlers[id]; ok {
sess.destroy(errors.New("deleting"))
}
h.removeByConnectionIDAsString(id)
h.mutex.Unlock()
})