2017-10-31 09:25:44 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var zeroTime time.Time
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
type RequestState struct {
|
2017-11-01 08:43:20 +00:00
|
|
|
ID uint64
|
2017-10-31 09:25:44 +00:00
|
|
|
Method string
|
|
|
|
Args interface{}
|
|
|
|
Result interface{}
|
|
|
|
Error error
|
2017-11-26 10:15:51 +00:00
|
|
|
DoneChan chan *RequestState
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-02 06:39:30 +00:00
|
|
|
hasResponse bool
|
|
|
|
canceled uint32
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
func (rs *RequestState) Done() {
|
2017-10-31 09:25:44 +00:00
|
|
|
select {
|
2017-11-26 10:15:51 +00:00
|
|
|
case rs.DoneChan <- rs:
|
2017-10-31 09:25:44 +00:00
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
// We don't want to block here. It is the caller's responsibility to make
|
|
|
|
// sure the channel has enough buffer space. See comment in Go().
|
|
|
|
// if debugLog {
|
|
|
|
// log.Println("rpc: discarding Call reply due to insufficient Done chan capacity")
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel cancels async call.
|
|
|
|
//
|
|
|
|
// Canceled call isn't sent to the server unless it is already sent there.
|
|
|
|
// Canceled call may successfully complete if it has been already sent
|
|
|
|
// to the server before Cancel call.
|
|
|
|
//
|
|
|
|
// It is safe calling this function multiple times from concurrently
|
|
|
|
// running goroutines.
|
2017-11-26 10:15:51 +00:00
|
|
|
func (rs *RequestState) Cancel() {
|
|
|
|
atomic.StoreUint32(&rs.canceled, 1)
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
func (rs *RequestState) IsCanceled() bool {
|
|
|
|
return atomic.LoadUint32(&rs.canceled) != 0
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
var requestStatePool sync.Pool
|
|
|
|
|
|
|
|
func retainRequestState() *RequestState {
|
|
|
|
v := requestStatePool.Get()
|
2017-10-31 09:25:44 +00:00
|
|
|
if v == nil {
|
2017-11-26 10:15:51 +00:00
|
|
|
return &RequestState{}
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
2017-11-26 10:15:51 +00:00
|
|
|
return v.(*RequestState)
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
func releaseCallState(rs *RequestState) {
|
|
|
|
rs.Method = ""
|
|
|
|
rs.Args = nil
|
|
|
|
rs.Result = nil
|
|
|
|
rs.Error = nil
|
|
|
|
rs.DoneChan = nil
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
requestStatePool.Put(rs)
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|