From b27395f6f4adde9e96d6e74e6ddd3f15b03c9ff9 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 8 Aug 2023 14:11:19 +0800 Subject: [PATCH] pkg/exchange: avoiding GC panic caused by a rapid creation/removal slice of pointers --- pkg/exchange/bybit/stream.go | 16 ++++++++-------- pkg/exchange/bybit/stream_callbacks.go | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/exchange/bybit/stream.go b/pkg/exchange/bybit/stream.go index e38872279..9af2bff14 100644 --- a/pkg/exchange/bybit/stream.go +++ b/pkg/exchange/bybit/stream.go @@ -33,8 +33,8 @@ type Stream struct { types.StandardStream bookEventCallbacks []func(e BookEvent) - walletEventCallbacks []func(e []*WalletEvent) - orderEventCallbacks []func(e []*OrderEvent) + walletEventCallbacks []func(e []WalletEvent) + orderEventCallbacks []func(e []OrderEvent) } func NewStream(key, secret string) *Stream { @@ -77,10 +77,10 @@ func (s *Stream) dispatchEvent(event interface{}) { case *BookEvent: s.EmitBookEvent(*e) - case []*WalletEvent: + case []WalletEvent: s.EmitWalletEvent(e) - case []*OrderEvent: + case []OrderEvent: s.EmitOrderEvent(e) } } @@ -110,11 +110,11 @@ func (s *Stream) parseWebSocketEvent(in []byte) (interface{}, error) { return &book, nil case TopicTypeWallet: - var wallets []*WalletEvent + var wallets []WalletEvent return wallets, json.Unmarshal(e.WebSocketTopicEvent.Data, &wallets) case TopicTypeOrder: - var orders []*OrderEvent + var orders []OrderEvent return orders, json.Unmarshal(e.WebSocketTopicEvent.Data, &orders) } @@ -239,7 +239,7 @@ func (s *Stream) handleBookEvent(e BookEvent) { } } -func (s *Stream) handleWalletEvent(events []*WalletEvent) { +func (s *Stream) handleWalletEvent(events []WalletEvent) { bm := types.BalanceMap{} for _, event := range events { if event.AccountType != AccountTypeSpot { @@ -258,7 +258,7 @@ func (s *Stream) handleWalletEvent(events []*WalletEvent) { s.StandardStream.EmitBalanceSnapshot(bm) } -func (s *Stream) handleOrderEvent(events []*OrderEvent) { +func (s *Stream) handleOrderEvent(events []OrderEvent) { for _, event := range events { if event.Category != bybitapi.CategorySpot { return diff --git a/pkg/exchange/bybit/stream_callbacks.go b/pkg/exchange/bybit/stream_callbacks.go index 79b1bd7be..33e7516ff 100644 --- a/pkg/exchange/bybit/stream_callbacks.go +++ b/pkg/exchange/bybit/stream_callbacks.go @@ -14,21 +14,21 @@ func (s *Stream) EmitBookEvent(e BookEvent) { } } -func (s *Stream) OnWalletEvent(cb func(e []*WalletEvent)) { +func (s *Stream) OnWalletEvent(cb func(e []WalletEvent)) { s.walletEventCallbacks = append(s.walletEventCallbacks, cb) } -func (s *Stream) EmitWalletEvent(e []*WalletEvent) { +func (s *Stream) EmitWalletEvent(e []WalletEvent) { for _, cb := range s.walletEventCallbacks { cb(e) } } -func (s *Stream) OnOrderEvent(cb func(e []*OrderEvent)) { +func (s *Stream) OnOrderEvent(cb func(e []OrderEvent)) { s.orderEventCallbacks = append(s.orderEventCallbacks, cb) } -func (s *Stream) EmitOrderEvent(e []*OrderEvent) { +func (s *Stream) EmitOrderEvent(e []OrderEvent) { for _, cb := range s.orderEventCallbacks { cb(e) }