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

(DAL) Pooling broadcast #2246

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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 = 20
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.work(poolCtx)
}
}

func (p *Pool) worker(ctx context.Context) {
func (p *Pool) work(ctx context.Context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worker in this context refers to goroutines that run the actual task/job/work. And seems like "In the context of a worker pool, the worker term is a widely accepted convention".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought function name should be verb rather than noun, since it does some action rather than just a property or a value. how do you think? if it is worker, at first sight I thought it should be some sort of struct or value

Copy link
Collaborator Author

@nick-bisonai nick-bisonai Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well it seems trivial and minor so I'm not insisting, either is good for me. I'll rollback if you mind

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a valid point. But i found work a bit weird as well lol. So maybe runWorker? 😄

for {
select {
case job := <-p.jobChannel:
Expand Down