Skip to content

Commit

Permalink
The better design pattern would be to handle channel operations witho…
Browse files Browse the repository at this point in the history
…ut launching unnecessary goroutines
  • Loading branch information
dyamin committed Jun 3, 2024
1 parent 9058b2a commit 1501845
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 112 deletions.
38 changes: 10 additions & 28 deletions api/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,37 +377,27 @@ func (c *ClientWs) process(data []byte, e *events.Basic) bool {
e := events.Error{}
_ = json.Unmarshal(data, &e)
if c.ErrChan != nil {
go func() {
c.ErrChan <- &e
}()
c.ErrChan <- &e
}
return true
case "subscribe":
e := events.Subscribe{}
_ = json.Unmarshal(data, &e)
if c.SubscribeChan != nil {
go func() {
c.SubscribeChan <- &e
}()
c.SubscribeChan <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "unsubscribe":
e := events.Unsubscribe{}
_ = json.Unmarshal(data, &e)
if c.UnsubscribeCh != nil {
go func() {
c.UnsubscribeCh <- &e
}()
c.UnsubscribeCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "login":
Expand All @@ -420,14 +410,10 @@ func (c *ClientWs) process(data []byte, e *events.Basic) bool {
e := events.Login{}
_ = json.Unmarshal(data, &e)
if c.LoginChan != nil {
go func() {
c.LoginChan <- &e
}()
c.LoginChan <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
}
Expand All @@ -446,17 +432,13 @@ func (c *ClientWs) process(data []byte, e *events.Basic) bool {
e := events.Success{}
_ = json.Unmarshal(data, &e)
if c.SuccessChan != nil {
go func() {
c.SuccessChan <- &e
}()
c.SuccessChan <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
}
go func() { c.RawEventChan <- e }()
c.RawEventChan <- e
return false
}
112 changes: 28 additions & 84 deletions api/ws/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.iCh != nil {
go func() {
c.iCh <- &e
}()
c.iCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "tickers":
Expand All @@ -399,14 +395,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.tCh != nil {
go func() {
c.tCh <- &e
}()
c.tCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "open-interest":
Expand All @@ -415,14 +407,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.oiCh != nil {
go func() {
c.oiCh <- &e
}()
c.oiCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "trades":
Expand All @@ -431,14 +419,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.trCh != nil {
go func() {
c.trCh <- &e
}()
c.trCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "estimated-price":
Expand All @@ -447,14 +431,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.edepCh != nil {
go func() {
c.edepCh <- &e
}()
c.edepCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "mark-price":
Expand All @@ -463,14 +443,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.mpCh != nil {
go func() {
c.mpCh <- &e
}()
c.mpCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "price-limit":
Expand All @@ -479,14 +455,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.plCh != nil {
go func() {
c.plCh <- &e
}()
c.plCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "opt-summary":
Expand All @@ -495,14 +467,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.osCh != nil {
go func() {
c.osCh <- &e
}()
c.osCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "funding-rate":
Expand All @@ -511,14 +479,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.frCh != nil {
go func() {
c.frCh <- &e
}()
c.frCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
case "index-tickers":
Expand All @@ -527,14 +491,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.itCh != nil {
go func() {
c.itCh <- &e
}()
c.itCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
default:
Expand All @@ -545,14 +505,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.mpcCh != nil {
go func() {
c.mpcCh <- &e
}()
c.mpcCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
}
Expand All @@ -562,14 +518,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.icCh != nil {
go func() {
c.icCh <- &e
}()
c.icCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
}
Expand All @@ -579,14 +531,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.cCh != nil {
go func() {
c.cCh <- &e
}()
c.cCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
}
Expand All @@ -596,14 +544,10 @@ func (c *Public) Process(data []byte, e *events.Basic) bool {
return false
}
if c.obCh != nil {
go func() {
c.obCh <- &e
}()
c.obCh <- &e
}
if c.StructuredEventChan != nil {
go func() {
c.StructuredEventChan <- e
}()
c.StructuredEventChan <- e
}
return true
}
Expand Down

0 comments on commit 1501845

Please sign in to comment.