run grpc finishAsyncCall in go routine

This commit is contained in:
Ethan Buchman 2016-07-06 16:57:02 -04:00
parent 88f8141ab8
commit 732634112b
1 changed files with 17 additions and 9 deletions

View File

@ -90,7 +90,10 @@ func (cli *grpcClient) Error() error {
} }
//---------------------------------------- //----------------------------------------
// async calls are really sync. // GRPC calls are synchronous, but some callbacks expect to be called asynchronously
// (eg. the mempool expects to be able to lock to remove bad txs from cache).
// To accomodate, we finish each call in its own go-routine,
// which is expensive, but easy - if you want something better, use the socket protocol!
// maybe one day, if people really want it, we use grpc streams, // maybe one day, if people really want it, we use grpc streams,
// but hopefully not :D // but hopefully not :D
@ -199,15 +202,20 @@ func (cli *grpcClient) finishAsyncCall(req *types.Request, res *types.Response)
reqres.Done() // Release waiters reqres.Done() // Release waiters
reqres.SetDone() // so reqRes.SetCallback will run the callback reqres.SetDone() // so reqRes.SetCallback will run the callback
// Notify reqRes listener if set // go routine for callbacks
if cb := reqres.GetCallback(); cb != nil { go func() {
cb(res) // Notify reqRes listener if set
} if cb := reqres.GetCallback(); cb != nil {
fmt.Println("CALLING reqres CB")
cb(res)
}
// Notify client listener if set // Notify client listener if set
if cli.resCb != nil { if cli.resCb != nil {
cli.resCb(reqres.Request, res) fmt.Println("CALLING client CB")
} cli.resCb(reqres.Request, res)
}
}()
return reqres return reqres
} }