Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binance: Retry keep alive. #2958

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 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 Expand Up @@ -507,6 +538,11 @@ func (f *fakeBinance) handleAccountSubscription(w http.ResponseWriter, r *http.R
}()
}

type listSubsResp struct {
ID uint64 `json:"id"`
Result []string `json:"result"`
}

func (f *fakeBinance) handleMarketStream(w http.ResponseWriter, r *http.Request) {
streamsStr := r.URL.Query().Get("streams")
if streamsStr == "" {
Expand Down Expand Up @@ -556,6 +592,25 @@ func (f *fakeBinance) handleMarketStream(w http.ResponseWriter, r *http.Request)
f.cleanMarkets()
}

listSubscriptions := func(id uint64) {
f.marketsMtx.Lock()
defer f.marketsMtx.Unlock()
var streams []string
for mktID := range cl.markets {
streams = append(streams, fmt.Sprintf("%s@depth", mktID))
}
resp := listSubsResp{
ID: id,
Result: streams,
}
b, err := json.Marshal(resp)
if err != nil {
log.Errorf("LIST_SUBSCRIBE marshal error: %v", err)
}
cl.WSLink.SendRaw(b)
f.cleanMarkets()
}

conn, cm := f.newWSLink(w, r, func(b []byte) {
var req bntypes.StreamSubscription
if err := json.Unmarshal(b, &req); err != nil {
Expand All @@ -567,6 +622,8 @@ func (f *fakeBinance) handleMarketStream(w http.ResponseWriter, r *http.Request)
subscribe(req.Params)
case "UNSUBSCRIBE":
unsubscribe(req.Params)
case "LIST_SUBSCRIPTIONS":
listSubscriptions(req.ID)
}
})
if conn == nil {
Expand Down
Loading
Loading