Skip to content

Commit

Permalink
connectivity: refactor and drop AnyDisconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 18, 2024
1 parent e497b8b commit d22d16f
Showing 1 changed file with 3 additions and 24 deletions.
27 changes: 3 additions & 24 deletions pkg/types/connectivitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,36 +173,15 @@ func (g *ConnectivityGroup) Add(con *Connectivity) {
})
}

func (g *ConnectivityGroup) AnyDisconnected(ctx context.Context) bool {
g.mu.Lock()
conns := g.connections
g.mu.Unlock()

for _, conn := range conns {
select {
case <-ctx.Done():
return false

case <-conn.connectedC:
continue

case <-conn.disconnectedC:
return true
}
}

return false
}

func (g *ConnectivityGroup) waitAllAuthed(ctx context.Context, c chan struct{}) {
func (g *ConnectivityGroup) waitForState(ctx context.Context, c chan struct{}, expected ConnectivityState) {
for {
select {
case <-ctx.Done():
return

default:
state := g.GetState()
if state == ConnectivityStateAuthed {
if state == expected {
close(c)
return
}
Expand All @@ -217,6 +196,6 @@ func (g *ConnectivityGroup) waitAllAuthed(ctx context.Context, c chan struct{})
// and the channel can only be used once (because we can't close a channel twice)
func (g *ConnectivityGroup) AllAuthedC(ctx context.Context) <-chan struct{} {
c := make(chan struct{})
go g.waitAllAuthed(ctx, c)
go g.waitForState(ctx, c, ConnectivityStateAuthed)
return c
}

0 comments on commit d22d16f

Please sign in to comment.