Merge pull request #1 from Detectify/develop

Add lock on job handler assignment
This commit is contained in:
Christoffer Fjellström 2017-09-27 12:00:00 +02:00 committed by GitHub
commit d9ad23413d

View File

@ -31,7 +31,7 @@ type Client struct {
}
type responseHandlerMap struct {
sync.RWMutex
sync.Mutex
holder map[string]ResponseHandler
}
@ -46,17 +46,22 @@ func (r *responseHandlerMap) remove(key string) {
}
func (r *responseHandlerMap) get(key string) (ResponseHandler, bool) {
r.RLock()
r.Lock()
rh, b := r.holder[key]
r.RUnlock()
r.Unlock()
return rh, b
}
func (r *responseHandlerMap) put(key string, rh ResponseHandler) {
r.Lock()
r.holder[key] = rh
r.Unlock()
}
func (r *responseHandlerMap) putNoLock(key string, rh ResponseHandler) {
r.holder[key] = rh
}
// Return a client.
func New(network, addr string) (client *Client, err error) {
client = &Client{
@ -266,9 +271,12 @@ func (client *Client) Do(funcname string, data []byte,
default:
datatype = dtSubmitJob
}
client.respHandler.Lock()
defer client.respHandler.Unlock()
handle, err = client.do(funcname, data, datatype)
if err == nil && h != nil {
client.respHandler.put(handle, h)
client.respHandler.putNoLock(handle, h)
}
return
}