-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(DAL) Refactor package structure (#1925)
* refactor: refactor package structure * fix: update based on feedback * Update node/pkg/dal/utils/initializer/initializer.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update node/pkg/dal/utils/initializer/initializer.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix: remove redundant code * Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update node/pkg/dal/api/controller.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * feat: delete also subscription on unregister (#1928) * feat: closeHandler update (#1927) * (DAL) writeControl (#1926) * feat: writeControl * Update node/pkg/dal/api/hub.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix: remove unnecessary codes * feat: modularize, update readability * fix: remove unnecessary code --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a278af1
commit 0bc41a5
Showing
8 changed files
with
196 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"bisonai.com/orakl/node/pkg/common/types" | ||
"bisonai.com/orakl/node/pkg/dal/collector" | ||
dalcommon "bisonai.com/orakl/node/pkg/dal/common" | ||
"github.com/gofiber/contrib/websocket" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func HubSetup(ctx context.Context, configs []types.Config) *Hub { | ||
configMap := make(map[string]types.Config) | ||
for _, config := range configs { | ||
configMap[config.Name] = config | ||
} | ||
|
||
hub := NewHub(configMap) | ||
return hub | ||
} | ||
|
||
func NewHub(configs map[string]types.Config) *Hub { | ||
return &Hub{ | ||
configs: configs, | ||
|
||
clients: make(map[*websocket.Conn]map[string]bool), | ||
register: make(chan *websocket.Conn), | ||
unregister: make(chan *websocket.Conn), | ||
broadcast: make(map[string]chan dalcommon.OutgoingSubmissionData), | ||
} | ||
} | ||
|
||
func (c *Hub) Start(ctx context.Context, collector *collector.Collector) { | ||
go c.handleClientRegistration() | ||
|
||
c.initializeBroadcastChannels(collector) | ||
|
||
for symbol := range c.configs { | ||
go c.broadcastDataForSymbol(symbol) | ||
} | ||
} | ||
|
||
func (c *Hub) handleClientRegistration() { | ||
for { | ||
select { | ||
case conn := <-c.register: | ||
c.addClient(conn) | ||
case conn := <-c.unregister: | ||
c.removeClient(conn) | ||
} | ||
} | ||
} | ||
|
||
func (c *Hub) addClient(conn *websocket.Conn) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if _, ok := c.clients[conn]; ok { | ||
return | ||
} | ||
c.clients[conn] = make(map[string]bool) | ||
} | ||
|
||
func (c *Hub) removeClient(conn *websocket.Conn) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if _, ok := c.clients[conn]; ok { | ||
for symbol := range c.clients[conn] { | ||
delete(c.clients[conn], symbol) | ||
} | ||
delete(c.clients, conn) | ||
} | ||
if err := conn.WriteControl( | ||
websocket.CloseMessage, | ||
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), | ||
time.Now().Add(time.Second), | ||
); err != nil { | ||
log.Error().Err(err).Msg("failed to send close message") | ||
} | ||
conn.Close() | ||
} | ||
|
||
func (c *Hub) initializeBroadcastChannels(collector *collector.Collector) { | ||
for configId, stream := range collector.OutgoingStream { | ||
symbol := c.configIdToSymbol(configId) | ||
c.broadcast[symbol] = stream | ||
} | ||
} | ||
|
||
func (c *Hub) configIdToSymbol(id int32) string { | ||
for symbol, config := range c.configs { | ||
if config.ID == id { | ||
return symbol | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func (c *Hub) broadcastDataForSymbol(symbol string) { | ||
for data := range c.broadcast[symbol] { | ||
c.castSubmissionData(&data, &symbol) | ||
} | ||
} | ||
|
||
// pass by pointer to reduce memory copy time | ||
func (c *Hub) castSubmissionData(data *dalcommon.OutgoingSubmissionData, symbol *string) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
for conn := range c.clients { | ||
if _, ok := c.clients[conn][*symbol]; ok { | ||
if err := conn.WriteJSON(*data); err != nil { | ||
log.Error().Err(err).Msg("failed to write message") | ||
c.unregister <- conn | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.