mirror of
https://github.com/talent-plan/tinykv.git
synced 2025-01-13 13:50:43 +08:00
5e089a2cd1
Signed-off-by: Connor <zbk602423539@gmail.com> Co-authored-by: Nick Cameron <nrc@ncameron.org> Co-authored-by: linning <linningde25@gmail.com> Co-authored-by: YangKeao <keao.yang@yahoo.com> Co-authored-by: andylokandy <andylokandy@hotmail.com> Co-authored-by: Iosmanthus Teng <myosmanthustree@gmail.com>
47 lines
806 B
Go
47 lines
806 B
Go
package message
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/Connor1996/badger"
|
|
"github.com/pingcap-incubator/tinykv/proto/pkg/raft_cmdpb"
|
|
)
|
|
|
|
type Callback struct {
|
|
Resp *raft_cmdpb.RaftCmdResponse
|
|
Txn *badger.Txn // used for GetSnap
|
|
done chan struct{}
|
|
}
|
|
|
|
func (cb *Callback) Done(resp *raft_cmdpb.RaftCmdResponse) {
|
|
if cb == nil {
|
|
return
|
|
}
|
|
if resp != nil {
|
|
cb.Resp = resp
|
|
}
|
|
cb.done <- struct{}{}
|
|
}
|
|
|
|
func (cb *Callback) WaitResp() *raft_cmdpb.RaftCmdResponse {
|
|
select {
|
|
case <-cb.done:
|
|
return cb.Resp
|
|
}
|
|
}
|
|
|
|
func (cb *Callback) WaitRespWithTimeout(timeout time.Duration) *raft_cmdpb.RaftCmdResponse {
|
|
select {
|
|
case <-cb.done:
|
|
return cb.Resp
|
|
case <-time.After(timeout):
|
|
return cb.Resp
|
|
}
|
|
}
|
|
|
|
func NewCallback() *Callback {
|
|
done := make(chan struct{}, 1)
|
|
cb := &Callback{done: done}
|
|
return cb
|
|
}
|