Skip to content

Commit

Permalink
Prevent concurrent write for websocket data stream
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed May 29, 2018
1 parent dc11f87 commit 7b60bdd
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 173 deletions.
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ func main() {
bot.portfolio.SeedPortfolio(bot.config.Portfolio)
SeedExchangeAccountInfo(GetAllEnabledExchangeAccountInfo().Data)
go portfolio.StartPortfolioWatcher()

log.Println("Starting websocket handler.")
go WebsocketHandler()
go TickerUpdaterRoutine()
go OrderbookUpdaterRoutine()

Expand All @@ -135,8 +132,18 @@ func main() {
"HTTP Webserver support enabled. Listen URL: http://%s:%d/\n",
common.ExtractHost(listenAddr), common.ExtractPort(listenAddr),
)

router := NewRouter(bot.exchanges)
log.Fatal(http.ListenAndServe(listenAddr, router))
go func() {
err = http.ListenAndServe(listenAddr, router)
if err != nil {
log.Fatal(err)
}
}()

log.Println("HTTP Webserver started successfully.")
log.Println("Starting websocket handler.")
StartWebsocketHandler()
} else {
log.Println("HTTP RESTful Webserver support disabled.")
}
Expand Down
4 changes: 2 additions & 2 deletions routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TickerUpdaterRoutine() {
result, err = exch.GetTickerPrice(c, assetType)
}
printTickerSummary(result, c, assetType, exchangeName, err)
if err == nil {
if bot.config.Webserver.Enabled && err == nil {
relayWebsocketEvent(result, "ticker_update", assetType, exchangeName)
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func OrderbookUpdaterRoutine() {
processOrderbook := func(exch exchange.IBotExchange, c pair.CurrencyPair, assetType string) {
result, err := exch.UpdateOrderbook(c, assetType)
printOrderbookSummary(result, c, assetType, exchangeName, err)
if err == nil {
if bot.config.Webserver.Enabled && err == nil {
relayWebsocketEvent(result, "orderbook_update", assetType, exchangeName)
}
}
Expand Down
Loading

0 comments on commit 7b60bdd

Please sign in to comment.