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) Minor improvement #2004

Merged
merged 4 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions node/pkg/dal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ func (c *ThreadSafeClient) WriteJSON(data any) error {
return nil
}

// even though readjson is not thread safe, it is expected not be called concurrently
// even though readjson is not thread safe, it is expected not to be called concurrently
// since the only place it is called is from `HandleWebsocket` inner for loop
func (c *ThreadSafeClient) ReadJSON(data any) error {
if err := c.Conn.ReadJSON(&data); err != nil {
log.Error().Err(err).Msg("failed to read json msg")
return err
}
return nil
Expand Down
39 changes: 24 additions & 15 deletions node/pkg/dal/api/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ func (h *Hub) removeClient(client *ThreadSafeClient) {
}
}

func (h *Hub) getClientsSnapshotToNotify(symbol string) []*ThreadSafeClient {
h.mu.RLock()
defer h.mu.RUnlock()
result := []*ThreadSafeClient{}
for client, subscriptions := range h.clients {
if subscriptions[symbol] {
result = append(result, client)
}
}
return result
}

func (h *Hub) initializeBroadcastChannels(collector *collector.Collector) {
for configId, stream := range collector.OutgoingStream {
symbol := h.configIdToSymbol(configId)
Expand Down Expand Up @@ -153,22 +165,19 @@ func (h *Hub) broadcastDataForSymbol(symbol string) {
}

// pass by pointer to reduce memory copy time
func (c *Hub) castSubmissionData(data *dalcommon.OutgoingSubmissionData, symbol *string) {
func (h *Hub) castSubmissionData(data *dalcommon.OutgoingSubmissionData, symbol *string) {
var wg sync.WaitGroup

c.mu.Lock()
defer c.mu.Unlock()
for client, subscriptions := range c.clients {
if subscriptions[*symbol] {
wg.Add(1)
go func(entry *ThreadSafeClient) {
defer wg.Done()
if err := entry.WriteJSON(*data); err != nil {
log.Error().Err(err).Msg("failed to write message")
c.unregister <- entry
}
}(client)
}
clientsToNotify := h.getClientsSnapshotToNotify(*symbol)

for _, client := range clientsToNotify {
wg.Add(1)
go func(entry *ThreadSafeClient) {
defer wg.Done()
if err := entry.WriteJSON(*data); err != nil {
log.Error().Err(err).Msg("failed to write message")
h.unregister <- entry
}
}(client)
}
wg.Wait()
}
2 changes: 1 addition & 1 deletion node/pkg/dal/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Hub struct {
unregister chan *ThreadSafeClient
broadcast map[string]chan dalcommon.OutgoingSubmissionData
connPerIP map[string][]*ThreadSafeClient
mu sync.Mutex
mu sync.RWMutex

}

Expand Down