mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-19 15:44:28 +01:00
protect map write with mutex
* in order to make the r.receivedResponsesById map goroutine-safe, a mutex was added. This is not particular nice, but rewriting the jsonrpc connection handling would be quite a lot of work and has a big regression potential. So, if it is not absolutely necessary, I'd like to avoid that. see #555
This commit is contained in:
@@ -61,6 +61,7 @@ type JsonRpc2Client struct {
|
|||||||
signalCliApiConfig *utils.SignalCliApiConfig
|
signalCliApiConfig *utils.SignalCliApiConfig
|
||||||
number string
|
number string
|
||||||
receivedMessagesMutex sync.Mutex
|
receivedMessagesMutex sync.Mutex
|
||||||
|
receivedResponsesMutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client {
|
func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client {
|
||||||
@@ -137,11 +138,16 @@ func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{
|
|||||||
}
|
}
|
||||||
|
|
||||||
responseChan := make(chan JsonRpc2MessageResponse)
|
responseChan := make(chan JsonRpc2MessageResponse)
|
||||||
|
r.receivedResponsesMutex.Lock()
|
||||||
r.receivedResponsesById[u.String()] = responseChan
|
r.receivedResponsesById[u.String()] = responseChan
|
||||||
|
r.receivedResponsesMutex.Unlock()
|
||||||
|
|
||||||
var resp JsonRpc2MessageResponse
|
var resp JsonRpc2MessageResponse
|
||||||
resp = <-responseChan
|
resp = <-responseChan
|
||||||
|
|
||||||
|
r.receivedResponsesMutex.Lock()
|
||||||
delete(r.receivedResponsesById, u.String())
|
delete(r.receivedResponsesById, u.String())
|
||||||
|
r.receivedResponsesMutex.Unlock()
|
||||||
|
|
||||||
log.Debug("json-rpc command response message: ", string(resp.Result))
|
log.Debug("json-rpc command response message: ", string(resp.Result))
|
||||||
log.Debug("json-rpc response error: ", string(resp.Err.Message))
|
log.Debug("json-rpc response error: ", string(resp.Err.Message))
|
||||||
|
|||||||
Reference in New Issue
Block a user