Skip to content
Snippets Groups Projects
Commit 963f23ec authored by capossele's avatar capossele
Browse files

:bug: fix data races on ui

parent c534f236
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
) )
var logMutex = sync.Mutex{} var logMutex = sync.RWMutex{}
var logHistory = make([]*statusMessage, 0) var logHistory = make([]*statusMessage, 0)
type statusMessage struct { type statusMessage struct {
......
package ui package ui
import ( import (
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
...@@ -14,6 +15,7 @@ var receivedTpsCounter uint64 ...@@ -14,6 +15,7 @@ var receivedTpsCounter uint64
var solidTpsCounter uint64 var solidTpsCounter uint64
var tpsQueue []uint32 var tpsQueue []uint32
var tpsQueueMutex sync.RWMutex
const maxQueueSize int = 3600 const maxQueueSize int = 3600
...@@ -31,10 +33,12 @@ type nodeInfo struct { ...@@ -31,10 +33,12 @@ type nodeInfo struct {
func gatherInfo() nodeInfo { func gatherInfo() nodeInfo {
// update tps queue // update tps queue
tpsQueue = append(tpsQueue, uint32(receivedTpsCounter)) tpsQueueMutex.Lock()
tpsQueue = append(tpsQueue, uint32(atomic.LoadUint64(&receivedTpsCounter)))
if len(tpsQueue) > maxQueueSize { if len(tpsQueue) > maxQueueSize {
tpsQueue = tpsQueue[1:] tpsQueue = tpsQueue[1:]
} }
tpsQueueMutex.Unlock()
// update neighbors // update neighbors
chosenNeighbors := []string{} chosenNeighbors := []string{}
......
...@@ -2,6 +2,7 @@ package ui ...@@ -2,6 +2,7 @@ package ui
import ( import (
"strings" "strings"
"sync"
"github.com/iotaledger/goshimmer/packages/model/value_transaction" "github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/iota.go/transaction" "github.com/iotaledger/iota.go/transaction"
...@@ -9,16 +10,21 @@ import ( ...@@ -9,16 +10,21 @@ import (
// cleared every second // cleared every second
var transactions []transaction.Transaction var transactions []transaction.Transaction
var tMutex sync.Mutex
var emptyTag = strings.Repeat("9", 27) var emptyTag = strings.Repeat("9", 27)
func logTransactions() interface{} { func logTransactions() interface{} {
tMutex.Lock()
defer tMutex.Unlock()
a := transactions a := transactions
transactions = make([]transaction.Transaction, 0) transactions = make([]transaction.Transaction, 0)
return a return a
} }
func saveTx(tx *value_transaction.ValueTransaction) { func saveTx(tx *value_transaction.ValueTransaction) {
tMutex.Lock()
defer tMutex.Unlock()
transactions = append(transactions, transaction.Transaction{ transactions = append(transactions, transaction.Transaction{
Hash: tx.MetaTransaction.GetHash(), Hash: tx.MetaTransaction.GetHash(),
Address: tx.GetAddress(), Address: tx.GetAddress(),
......
...@@ -27,9 +27,13 @@ func configure(plugin *node.Plugin) { ...@@ -27,9 +27,13 @@ func configure(plugin *node.Plugin) {
webapi.AddEndpoint("ws", upgrader) webapi.AddEndpoint("ws", upgrader)
webapi.AddEndpoint("loghistory", func(c echo.Context) error { webapi.AddEndpoint("loghistory", func(c echo.Context) error {
logMutex.RLock()
defer logMutex.RUnlock()
return c.JSON(http.StatusOK, logHistory) return c.JSON(http.StatusOK, logHistory)
}) })
webapi.AddEndpoint("tpsqueue", func(c echo.Context) error { webapi.AddEndpoint("tpsqueue", func(c echo.Context) error {
tpsQueueMutex.RLock()
defer tpsQueueMutex.RUnlock()
return c.JSON(http.StatusOK, tpsQueue) return c.JSON(http.StatusOK, tpsQueue)
}) })
...@@ -74,10 +78,12 @@ func run(plugin *node.Plugin) { ...@@ -74,10 +78,12 @@ func run(plugin *node.Plugin) {
case <-daemon.ShutdownSignal: case <-daemon.ShutdownSignal:
return return
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
wsMutex.Lock()
ws.send(resp{ ws.send(resp{
"info": gatherInfo(), "info": gatherInfo(),
"txs": logTransactions(), "txs": logTransactions(),
}) })
wsMutex.Unlock()
} }
} }
}) })
......
...@@ -3,6 +3,7 @@ package ui ...@@ -3,6 +3,7 @@ package ui
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"sync"
"github.com/labstack/echo" "github.com/labstack/echo"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
...@@ -13,6 +14,7 @@ type socket struct { ...@@ -13,6 +14,7 @@ type socket struct {
} }
var ws socket var ws socket
var wsMutex sync.Mutex
func (sock socket) send(msg interface{}) { func (sock socket) send(msg interface{}) {
payload, err := json.Marshal(msg) payload, err := json.Marshal(msg)
...@@ -24,7 +26,9 @@ func (sock socket) send(msg interface{}) { ...@@ -24,7 +26,9 @@ func (sock socket) send(msg interface{}) {
func upgrader(c echo.Context) error { func upgrader(c echo.Context) error {
websocket.Handler(func(conn *websocket.Conn) { websocket.Handler(func(conn *websocket.Conn) {
wsMutex.Lock()
ws.conn = conn ws.conn = conn
wsMutex.Unlock()
defer conn.Close() defer conn.Close()
for { for {
msg := "" msg := ""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment