It fixes race conditions in `geth/signal`.
This commit is contained in:
b00ris 2017-11-28 16:17:15 +03:00 committed by Adam Babik
parent 6bf980a1a7
commit c530611642
1 changed files with 10 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import (
"encoding/json"
"github.com/status-im/status-go/geth/log"
"sync"
)
const (
@ -47,14 +48,21 @@ type NodeNotificationHandler func(jsonEvent string)
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
// notificationHandlerMutex guards notificationHandler for concurrent calls
var notificationHandlerMutex sync.RWMutex
// SetDefaultNodeNotificationHandler sets notification handler to invoke on Send
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
notificationHandlerMutex.Lock()
notificationHandler = fn
notificationHandlerMutex.Unlock()
}
// ResetDefaultNodeNotificationHandler sets notification handler to default one
func ResetDefaultNodeNotificationHandler() {
notificationHandlerMutex.Lock()
notificationHandler = TriggerDefaultNodeNotificationHandler
notificationHandlerMutex.Unlock()
}
// TriggerDefaultNodeNotificationHandler triggers default notification handler (helpful in tests)
@ -71,6 +79,8 @@ func Send(signal Envelope) {
//export NotifyNode
//nolint: golint
func NotifyNode(jsonEvent *C.char) {
notificationHandlerMutex.RLock()
defer notificationHandlerMutex.RUnlock()
notificationHandler(C.GoString(jsonEvent))
}