Skip to content

Commit

Permalink
testbinance: Add flappy websocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Oct 8, 2024
1 parent c0d2403 commit bc30c1b
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions client/cmd/testbinance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (

walkingSpeedAdj float64
gapRange float64
flappyWS bool

xcInfo = &bntypes.ExchangeInfo{
Timezone: "UTC",
Expand Down Expand Up @@ -155,6 +156,7 @@ func main() {
flag.Float64Var(&gapRange, "gaprange", 0.04, "a ratio of how much the gap can vary. default is 0.04 => 4%")
flag.BoolVar(&logDebug, "debug", false, "use debug logging")
flag.BoolVar(&logTrace, "trace", false, "use trace logging")
flag.BoolVar(&flappyWS, "flappyws", false, "periodically drop websocket clients and delete subscriptions")
flag.Parse()

switch {
Expand Down Expand Up @@ -455,6 +457,35 @@ func (f *fakeBinance) run(ctx context.Context) {
}
}()

if flappyWS {
go func() {
tick := func() <-chan time.Time {
const minDelay = time.Minute
const delayRange = time.Minute * 5
return time.After(minDelay + time.Duration(rand.Float64()*float64(delayRange)))
}
for {
select {
case <-tick():
f.marketsMtx.Lock()
for addr, sub := range f.marketSubscribers {
sub.Disconnect()
delete(f.marketSubscribers, addr)
}
f.marketsMtx.Unlock()
f.accountSubscribersMtx.Lock()
for apiKey, sub := range f.accountSubscribers {
sub.Disconnect()
delete(f.accountSubscribers, apiKey)
}
f.accountSubscribersMtx.Unlock()
case <-ctx.Done():
return
}
}
}()
}

f.srv.Run(ctx)
}

Expand Down

0 comments on commit bc30c1b

Please sign in to comment.