diff --git a/plugins/ui/logger.go b/plugins/ui/logger.go index a4653aac52f189e818ce9f9157b68449e8dbf718..08658c431719479887e08ce781c14bd9bf094330 100644 --- a/plugins/ui/logger.go +++ b/plugins/ui/logger.go @@ -7,7 +7,7 @@ import ( "github.com/iotaledger/hive.go/logger" ) -var logMutex = sync.Mutex{} +var logMutex = sync.RWMutex{} var logHistory = make([]*statusMessage, 0) type statusMessage struct { diff --git a/plugins/ui/nodeInfo.go b/plugins/ui/nodeInfo.go index c23b25444e85b4d1e76b9dd574864f2ee47150a5..3c3f09c02f91387352f1cc742b1672a6897fc76d 100644 --- a/plugins/ui/nodeInfo.go +++ b/plugins/ui/nodeInfo.go @@ -1,6 +1,7 @@ package ui import ( + "sync" "sync/atomic" "time" @@ -14,6 +15,7 @@ var receivedTpsCounter uint64 var solidTpsCounter uint64 var tpsQueue []uint32 +var tpsQueueMutex sync.RWMutex const maxQueueSize int = 3600 @@ -31,10 +33,12 @@ type nodeInfo struct { func gatherInfo() nodeInfo { // update tps queue - tpsQueue = append(tpsQueue, uint32(receivedTpsCounter)) + tpsQueueMutex.Lock() + tpsQueue = append(tpsQueue, uint32(atomic.LoadUint64(&receivedTpsCounter))) if len(tpsQueue) > maxQueueSize { tpsQueue = tpsQueue[1:] } + tpsQueueMutex.Unlock() // update neighbors chosenNeighbors := []string{} diff --git a/plugins/ui/txLog.go b/plugins/ui/txLog.go index 3f64ac6198e10a319a3bd3ece9fc2fa7059c8cb9..d8d57932d17b8bd03735a83f305b063fd6b176d1 100644 --- a/plugins/ui/txLog.go +++ b/plugins/ui/txLog.go @@ -2,6 +2,7 @@ package ui import ( "strings" + "sync" "github.com/iotaledger/goshimmer/packages/model/value_transaction" "github.com/iotaledger/iota.go/transaction" @@ -9,16 +10,21 @@ import ( // cleared every second var transactions []transaction.Transaction +var tMutex sync.Mutex var emptyTag = strings.Repeat("9", 27) func logTransactions() interface{} { + tMutex.Lock() + defer tMutex.Unlock() a := transactions transactions = make([]transaction.Transaction, 0) return a } func saveTx(tx *value_transaction.ValueTransaction) { + tMutex.Lock() + defer tMutex.Unlock() transactions = append(transactions, transaction.Transaction{ Hash: tx.MetaTransaction.GetHash(), Address: tx.GetAddress(), diff --git a/plugins/ui/ui.go b/plugins/ui/ui.go index 3061a900e78d6eebdab333f46a255fae0e248959..86add4dfcecb91e1dc6345fd359316cc6a3f7274 100644 --- a/plugins/ui/ui.go +++ b/plugins/ui/ui.go @@ -27,9 +27,13 @@ func configure(plugin *node.Plugin) { webapi.AddEndpoint("ws", upgrader) webapi.AddEndpoint("loghistory", func(c echo.Context) error { + logMutex.RLock() + defer logMutex.RUnlock() return c.JSON(http.StatusOK, logHistory) }) webapi.AddEndpoint("tpsqueue", func(c echo.Context) error { + tpsQueueMutex.RLock() + defer tpsQueueMutex.RUnlock() return c.JSON(http.StatusOK, tpsQueue) }) @@ -74,10 +78,12 @@ func run(plugin *node.Plugin) { case <-daemon.ShutdownSignal: return case <-time.After(1 * time.Second): + wsMutex.Lock() ws.send(resp{ "info": gatherInfo(), "txs": logTransactions(), }) + wsMutex.Unlock() } } }) diff --git a/plugins/ui/websocket.go b/plugins/ui/websocket.go index 84cacf53969a34dfa2ef750141af299be20f6043..8507f59aea848056d48f6891a6ffd0d5f85be066 100644 --- a/plugins/ui/websocket.go +++ b/plugins/ui/websocket.go @@ -3,6 +3,7 @@ package ui import ( "encoding/json" "fmt" + "sync" "github.com/labstack/echo" "golang.org/x/net/websocket" @@ -13,6 +14,7 @@ type socket struct { } var ws socket +var wsMutex sync.Mutex func (sock socket) send(msg interface{}) { payload, err := json.Marshal(msg) @@ -24,7 +26,9 @@ func (sock socket) send(msg interface{}) { func upgrader(c echo.Context) error { websocket.Handler(func(conn *websocket.Conn) { + wsMutex.Lock() ws.conn = conn + wsMutex.Unlock() defer conn.Close() for { msg := ""