Skip to content

Commit

Permalink
Update when aPeerConnStatusChanged is closed
Browse files Browse the repository at this point in the history
Verison 29+ introduce new functions for the built-in relay that notify
when peer state has changed. Similarly for this feature to work, there
are handlers that need to close the aPeerConnStatusChanged channel to
signal a change.
  • Loading branch information
hurricanehrndz committed Sep 23, 2024
1 parent 25e47ff commit 814369b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions client/internal/peer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (d *Status) UpdatePeerState(receivedState State) error {
}

func (d *Status) UpdatePeerICEState(receivedState State) error {
var connStatusChanged bool
d.mux.Lock()
defer d.mux.Unlock()

Expand All @@ -294,6 +295,9 @@ func (d *Status) UpdatePeerICEState(receivedState State) error {
}

skipNotification := shouldSkipNotify(receivedState.ConnStatus, peerState)
if receivedState.ConnStatus != peerState.ConnStatus {
connStatusChanged = true
}

peerState.ConnStatus = receivedState.ConnStatus
peerState.ConnStatusUpdate = receivedState.ConnStatusUpdate
Expand All @@ -306,6 +310,11 @@ func (d *Status) UpdatePeerICEState(receivedState State) error {

d.peers[receivedState.PubKey] = peerState

if connStatusChanged && d.aPeerConnStatusChanged != nil && (peerState.ConnStatus == StatusConnected || peerState.ConnStatus == StatusDisconnected) {
close(d.aPeerConnStatusChanged)
d.aPeerConnStatusChanged = nil
}

if skipNotification {
return nil
}
Expand All @@ -321,6 +330,7 @@ func (d *Status) UpdatePeerICEState(receivedState State) error {
}

func (d *Status) UpdatePeerRelayedState(receivedState State) error {
var connStatusChanged bool
d.mux.Lock()
defer d.mux.Unlock()

Expand All @@ -330,6 +340,9 @@ func (d *Status) UpdatePeerRelayedState(receivedState State) error {
}

skipNotification := shouldSkipNotify(receivedState.ConnStatus, peerState)
if receivedState.ConnStatus != peerState.ConnStatus {
connStatusChanged = true
}

peerState.ConnStatus = receivedState.ConnStatus
peerState.ConnStatusUpdate = receivedState.ConnStatusUpdate
Expand All @@ -339,6 +352,11 @@ func (d *Status) UpdatePeerRelayedState(receivedState State) error {

d.peers[receivedState.PubKey] = peerState

if connStatusChanged && d.aPeerConnStatusChanged != nil && (peerState.ConnStatus == StatusConnected || peerState.ConnStatus == StatusDisconnected) {
close(d.aPeerConnStatusChanged)
d.aPeerConnStatusChanged = nil
}

if skipNotification {
return nil
}
Expand All @@ -354,6 +372,7 @@ func (d *Status) UpdatePeerRelayedState(receivedState State) error {
}

func (d *Status) UpdatePeerRelayedStateToDisconnected(receivedState State) error {
var connStatusChanged bool
d.mux.Lock()
defer d.mux.Unlock()

Expand All @@ -363,6 +382,9 @@ func (d *Status) UpdatePeerRelayedStateToDisconnected(receivedState State) error
}

skipNotification := shouldSkipNotify(receivedState.ConnStatus, peerState)
if receivedState.ConnStatus != peerState.ConnStatus {
connStatusChanged = true
}

peerState.ConnStatus = receivedState.ConnStatus
peerState.Relayed = receivedState.Relayed
Expand All @@ -371,6 +393,11 @@ func (d *Status) UpdatePeerRelayedStateToDisconnected(receivedState State) error

d.peers[receivedState.PubKey] = peerState

if connStatusChanged && d.aPeerConnStatusChanged != nil && (peerState.ConnStatus == StatusConnected || peerState.ConnStatus == StatusDisconnected) {
close(d.aPeerConnStatusChanged)
d.aPeerConnStatusChanged = nil
}

if skipNotification {
return nil
}
Expand All @@ -386,6 +413,7 @@ func (d *Status) UpdatePeerRelayedStateToDisconnected(receivedState State) error
}

func (d *Status) UpdatePeerICEStateToDisconnected(receivedState State) error {
var connStatusChanged bool
d.mux.Lock()
defer d.mux.Unlock()

Expand All @@ -395,6 +423,9 @@ func (d *Status) UpdatePeerICEStateToDisconnected(receivedState State) error {
}

skipNotification := shouldSkipNotify(receivedState.ConnStatus, peerState)
if receivedState.ConnStatus != peerState.ConnStatus {
connStatusChanged = true
}

peerState.ConnStatus = receivedState.ConnStatus
peerState.Relayed = receivedState.Relayed
Expand All @@ -406,6 +437,11 @@ func (d *Status) UpdatePeerICEStateToDisconnected(receivedState State) error {

d.peers[receivedState.PubKey] = peerState

if connStatusChanged && d.aPeerConnStatusChanged != nil && (peerState.ConnStatus == StatusConnected || peerState.ConnStatus == StatusDisconnected) {
close(d.aPeerConnStatusChanged)
d.aPeerConnStatusChanged = nil
}

if skipNotification {
return nil
}
Expand Down

0 comments on commit 814369b

Please sign in to comment.