mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
connectivity: refactor and drop AnyDisconnected
This commit is contained in:
parent
e497b8b5e5
commit
d22d16ff95
|
@ -173,28 +173,7 @@ func (g *ConnectivityGroup) Add(con *Connectivity) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *ConnectivityGroup) AnyDisconnected(ctx context.Context) bool {
|
func (g *ConnectivityGroup) waitForState(ctx context.Context, c chan struct{}, expected ConnectivityState) {
|
||||||
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{}) {
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
@ -202,7 +181,7 @@ func (g *ConnectivityGroup) waitAllAuthed(ctx context.Context, c chan struct{})
|
||||||
|
|
||||||
default:
|
default:
|
||||||
state := g.GetState()
|
state := g.GetState()
|
||||||
if state == ConnectivityStateAuthed {
|
if state == expected {
|
||||||
close(c)
|
close(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -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)
|
// 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{} {
|
func (g *ConnectivityGroup) AllAuthedC(ctx context.Context) <-chan struct{} {
|
||||||
c := make(chan struct{})
|
c := make(chan struct{})
|
||||||
go g.waitAllAuthed(ctx, c)
|
go g.waitForState(ctx, c, ConnectivityStateAuthed)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user