Skip to content

Commit

Permalink
(DAL) Pooling broadcast (#2246)
Browse files Browse the repository at this point in the history
* feat: pooling broadcast

* refactor: rename function name

* fix: update based on feedback

* feat: increase worker count
  • Loading branch information
nick-bisonai authored Aug 28, 2024
1 parent 8782295 commit 12378ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions node/pkg/dal/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bisonai.com/miko/node/pkg/dal/collector"
dalcommon "bisonai.com/miko/node/pkg/dal/common"
"bisonai.com/miko/node/pkg/dal/utils/stats"
"bisonai.com/miko/node/pkg/utils/pool"
"github.com/rs/zerolog/log"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
Expand All @@ -29,10 +30,12 @@ type Hub struct {
Unregister chan *websocket.Conn
broadcast map[string]chan *dalcommon.OutgoingSubmissionData
mu sync.RWMutex
pool *pool.Pool
}

const (
MAX_CONNECTIONS = 10
WORKER_COUNT = 100
CleanupInterval = time.Hour
)

Expand All @@ -53,10 +56,13 @@ func NewHub(configs map[string]Config) *Hub {
Register: make(chan *websocket.Conn),
Unregister: make(chan *websocket.Conn),
broadcast: make(map[string]chan *dalcommon.OutgoingSubmissionData),
pool: pool.NewPool(WORKER_COUNT),
}
}

func (h *Hub) Start(ctx context.Context, collector *collector.Collector) {
h.pool.Run(ctx)

go h.handleClientRegistration()

h.initializeBroadcastChannels(collector)
Expand Down Expand Up @@ -151,24 +157,19 @@ func (h *Hub) broadcastDataForSymbol(ctx context.Context, symbol string) {
}

func (h *Hub) castSubmissionData(ctx context.Context, data *dalcommon.OutgoingSubmissionData, symbol *string) {
var wg sync.WaitGroup

h.mu.RLock()
defer h.mu.RUnlock()

for client, subscriptions := range h.Clients {
if _, ok := subscriptions[*symbol]; ok {
wg.Add(1)
go func(entry *websocket.Conn) {
defer wg.Done()
err := wsjson.Write(ctx, entry, data)
h.pool.AddJob(func() {
err := wsjson.Write(ctx, client, data)
if err != nil {
log.Warn().Err(err).Msg("failed to write message to client")
}
}(client)
})
}
}
wg.Wait()
}

func (h *Hub) cleanupJob(ctx context.Context) {
Expand Down
4 changes: 2 additions & 2 deletions node/pkg/utils/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func (p *Pool) Run(ctx context.Context) {
p.IsRunning = true

for i := 0; i < p.workerCount; i++ {
go p.worker(poolCtx)
go p.runWorker(poolCtx)
}
}

func (p *Pool) worker(ctx context.Context) {
func (p *Pool) runWorker(ctx context.Context) {
for {
select {
case job := <-p.jobChannel:
Expand Down

0 comments on commit 12378ae

Please sign in to comment.