Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Clean up connections after websocket close
Browse files Browse the repository at this point in the history
Make sure we clean up connections by just going through the map and
cleaning all the verkeys that match the connection. This is a suboptimal
solution, but a connection close is not something happennig often.
  • Loading branch information
borancar committed Aug 26, 2022
1 parent 6c0c224 commit a02a1b2
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pkg/didcomm/transport/ws/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ func (d *connPool) remove(verKey string) {
}

func (d *connPool) listener(conn *websocket.Conn, outbound bool) {
verKeys := []string{}

defer d.close(conn, verKeys)
defer d.close(conn)

go keepConnAlive(conn, outbound, pingFrequency)

Expand Down Expand Up @@ -154,14 +152,25 @@ func (d *connPool) addKey(unpackMsg *transport.Envelope, trans *decorator.Transp
}
}

func (d *connPool) close(conn *websocket.Conn, verKeys []string) {
func (d *connPool) close(conn *websocket.Conn) {
if err := conn.Close(websocket.StatusNormalClosure,
"closing the connection"); websocket.CloseStatus(err) != websocket.StatusNormalClosure {
logger.Errorf("connection close error")
}

for _, v := range verKeys {
d.remove(v)
d.Lock()
defer d.Unlock()

var toRemove []string

for k, v := range d.connMap {
if v == conn {
toRemove = append(toRemove, k)
}
}

for _, v := range toRemove {
delete(d.connMap, v)
}
}

Expand Down

0 comments on commit a02a1b2

Please sign in to comment.