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 (
"github.com/iotaledger/hive.go/logger"
)
var logMutex = sync.Mutex{}
var logMutex = sync.RWMutex{}
var logHistory = make([]*statusMessage, 0)
type statusMessage struct {
......
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{}
......
......@@ -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(),
......
......@@ -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()
}
}
})
......
......@@ -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 := ""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment