|
@@ -34,21 +34,22 @@ type (
|
|
}
|
|
}
|
|
|
|
|
|
transaction struct {
|
|
transaction struct {
|
|
- sequence uint16
|
|
|
|
- response *Response
|
|
|
|
- isCanceled bool
|
|
|
|
- ch chan *transaction
|
|
|
|
|
|
+ sequence uint16
|
|
|
|
+ response *Response
|
|
|
|
+ canceledFlag int32
|
|
|
|
+ ch chan *transaction
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
func (t *transaction) Cancel() {
|
|
func (t *transaction) Cancel() {
|
|
- t.isCanceled = true
|
|
|
|
- close(t.ch)
|
|
|
|
|
|
+ if atomic.CompareAndSwapInt32(&t.canceledFlag, 0, 1) {
|
|
|
|
+ close(t.ch)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
func (t *transaction) Done(r *Response) {
|
|
func (t *transaction) Done(r *Response) {
|
|
t.response = r
|
|
t.response = r
|
|
- if t.ch != nil && !t.isCanceled {
|
|
|
|
|
|
+ if t.ch != nil && atomic.LoadInt32(&t.canceledFlag) == 0 {
|
|
select {
|
|
select {
|
|
case t.ch <- t:
|
|
case t.ch <- t:
|
|
default:
|
|
default:
|
|
@@ -59,9 +60,8 @@ func (t *transaction) Done(r *Response) {
|
|
func (c *Client) commit(seq uint16) *transaction {
|
|
func (c *Client) commit(seq uint16) *transaction {
|
|
c.transactionLocker.Lock()
|
|
c.transactionLocker.Lock()
|
|
trans := &transaction{
|
|
trans := &transaction{
|
|
- sequence: seq,
|
|
|
|
- isCanceled: false,
|
|
|
|
- ch: make(chan *transaction),
|
|
|
|
|
|
+ sequence: seq,
|
|
|
|
+ ch: make(chan *transaction),
|
|
}
|
|
}
|
|
c.transaction[seq] = trans
|
|
c.transaction[seq] = trans
|
|
c.transactionLocker.Unlock()
|
|
c.transactionLocker.Unlock()
|