Add lock on job handler assignment
Fixes race condition on jobs being done before handler is set.
This commit is contained in:
parent
27942f55cd
commit
2dbf199260
@ -31,7 +31,7 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type responseHandlerMap struct {
|
type responseHandlerMap struct {
|
||||||
sync.RWMutex
|
sync.Mutex
|
||||||
holder map[string]ResponseHandler
|
holder map[string]ResponseHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,17 +46,22 @@ func (r *responseHandlerMap) remove(key string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *responseHandlerMap) get(key string) (ResponseHandler, bool) {
|
func (r *responseHandlerMap) get(key string) (ResponseHandler, bool) {
|
||||||
r.RLock()
|
r.Lock()
|
||||||
rh, b := r.holder[key]
|
rh, b := r.holder[key]
|
||||||
r.RUnlock()
|
r.Unlock()
|
||||||
return rh, b
|
return rh, b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *responseHandlerMap) put(key string, rh ResponseHandler) {
|
func (r *responseHandlerMap) put(key string, rh ResponseHandler) {
|
||||||
r.Lock()
|
r.Lock()
|
||||||
r.holder[key] = rh
|
r.holder[key] = rh
|
||||||
r.Unlock()
|
r.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *responseHandlerMap) putNoLock(key string, rh ResponseHandler) {
|
||||||
|
r.holder[key] = rh
|
||||||
|
}
|
||||||
|
|
||||||
// Return a client.
|
// Return a client.
|
||||||
func New(network, addr string) (client *Client, err error) {
|
func New(network, addr string) (client *Client, err error) {
|
||||||
client = &Client{
|
client = &Client{
|
||||||
@ -266,9 +271,12 @@ func (client *Client) Do(funcname string, data []byte,
|
|||||||
default:
|
default:
|
||||||
datatype = dtSubmitJob
|
datatype = dtSubmitJob
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.respHandler.Lock()
|
||||||
|
defer client.respHandler.Unlock()
|
||||||
handle, err = client.do(funcname, data, datatype)
|
handle, err = client.do(funcname, data, datatype)
|
||||||
if err == nil && h != nil {
|
if err == nil && h != nil {
|
||||||
client.respHandler.put(handle, h)
|
client.respHandler.putNoLock(handle, h)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user