exchange: update maskkey handling

This commit is contained in:
TonyQ 2021-12-23 01:16:08 +08:00
parent bcbf7c3f3b
commit d7ac645253
4 changed files with 15 additions and 15 deletions

View File

@ -1,2 +1 @@
package bbgo

View File

@ -2,12 +2,12 @@ package binance
import (
"context"
"github.com/c9s/bbgo/pkg/util"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
@ -419,7 +419,7 @@ func (s *Stream) connect(ctx context.Context) error {
return err
}
log.Infof("listen key is created: %s", MaskKey(listenKey))
log.Infof("listen key is created: %s", util.MaskKey(listenKey))
}
// when in public mode, the listen key is an empty string
@ -496,7 +496,7 @@ func (s *Stream) listenKeyKeepAlive(ctx context.Context, listenKey string) {
defer func() {
log.Debugf("keepalive worker stopped")
if err := s.invalidateListenKey(context.Background(), listenKey); err != nil {
log.WithError(err).Errorf("invalidate listen key error: %v key: %s", err, MaskKey(listenKey))
log.WithError(err).Errorf("invalidate listen key error: %v key: %s", err, util.MaskKey(listenKey))
}
}()
@ -514,12 +514,12 @@ func (s *Stream) listenKeyKeepAlive(ctx context.Context, listenKey string) {
} else {
switch err.(type) {
case net.Error:
log.WithError(err).Errorf("listen key keep-alive network error: %v key: %s", err, MaskKey(listenKey))
log.WithError(err).Errorf("listen key keep-alive network error: %v key: %s", err, util.MaskKey(listenKey))
time.Sleep(1 * time.Second)
continue
default:
log.WithError(err).Errorf("listen key keep-alive unexpected error: %v key: %s", err, MaskKey(listenKey))
log.WithError(err).Errorf("listen key keep-alive unexpected error: %v key: %s", err, util.MaskKey(listenKey))
s.Reconnect()
return
@ -637,7 +637,7 @@ func (s *Stream) read(ctx context.Context) {
func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) (err error) {
// should use background context to invalidate the user stream
log.Infof("closing listen key: %s", MaskKey(listenKey))
log.Infof("closing listen key: %s", util.MaskKey(listenKey))
if s.IsMargin {
if s.IsIsolatedMargin {
@ -657,7 +657,7 @@ func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) (err
}
if err != nil {
log.WithError(err).Errorf("error deleting listen key: %s", MaskKey(listenKey))
log.WithError(err).Errorf("error deleting listen key: %s", util.MaskKey(listenKey))
return err
}
@ -676,8 +676,3 @@ func (s *Stream) Close() error {
s.ConnLock.Unlock()
return err
}
func MaskKey(key string) string {
maskKey := key[0:5]
return maskKey + strings.Repeat("*", len(key)-1-5)
}

View File

@ -182,7 +182,7 @@ func NewStream(client *okexapi.RestClient) *Stream {
},
}
log.Infof("sending login request: %+v", op)
log.Infof("sending okex login request")
err := stream.Conn.WriteJSON(op)
if err != nil {
log.WithError(err).Errorf("can not send login message")
@ -241,7 +241,6 @@ func (s *Stream) connect(ctx context.Context) error {
url = okexapi.PrivateWebSocketURL
}
conn, err := s.StandardStream.Dial(url)
if err != nil {
return err

View File

@ -1,5 +1,7 @@
package util
import "strings"
func StringSliceContains(slice []string, needle string) bool {
for _, s := range slice {
if s == needle {
@ -9,3 +11,8 @@ func StringSliceContains(slice []string, needle string) bool {
return false
}
func MaskKey(key string) string {
maskKey := key[0:5]
return maskKey + strings.Repeat("*", len(key)-1-5)
}