2016-01-22 15:50:11 -08:00
|
|
|
package tmspcli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"container/list"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2016-03-23 21:07:03 -07:00
|
|
|
"time"
|
2016-01-22 15:50:11 -08:00
|
|
|
|
|
|
|
. "github.com/tendermint/go-common"
|
2016-01-30 19:36:33 -08:00
|
|
|
"github.com/tendermint/tmsp/types"
|
2016-01-22 15:50:11 -08:00
|
|
|
)
|
|
|
|
|
2016-02-08 00:50:00 -08:00
|
|
|
const reqQueueSize = 256 // TODO make configurable
|
2016-01-22 15:50:11 -08:00
|
|
|
const maxResponseSize = 1048576 // 1MB TODO make configurable
|
|
|
|
const flushThrottleMS = 20 // Don't wait longer than...
|
|
|
|
|
2016-01-30 19:36:33 -08:00
|
|
|
type Callback func(*types.Request, *types.Response)
|
2016-01-22 15:50:11 -08:00
|
|
|
|
|
|
|
// This is goroutine-safe, but users should beware that
|
|
|
|
// the application in general is not meant to be interfaced
|
|
|
|
// with concurrent callers.
|
2016-02-21 23:55:45 -08:00
|
|
|
type Client struct {
|
2016-01-22 15:50:11 -08:00
|
|
|
QuitService
|
|
|
|
sync.Mutex // [EB]: is this even used?
|
|
|
|
|
2016-03-23 21:07:03 -07:00
|
|
|
reqQueue chan *ReqRes
|
|
|
|
flushTimer *ThrottleTimer
|
|
|
|
mustConnect bool
|
2016-01-22 15:50:11 -08:00
|
|
|
|
2016-03-23 21:07:03 -07:00
|
|
|
mtx sync.Mutex
|
|
|
|
addr string
|
|
|
|
conn net.Conn
|
|
|
|
err error
|
|
|
|
reqSent *list.List
|
|
|
|
resCb func(*types.Request, *types.Response) // listens to all callbacks
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 21:07:03 -07:00
|
|
|
func NewClient(addr string, mustConnect bool) (*Client, error) {
|
2016-02-21 23:55:45 -08:00
|
|
|
cli := &Client{
|
2016-03-23 21:07:03 -07:00
|
|
|
reqQueue: make(chan *ReqRes, reqQueueSize),
|
|
|
|
flushTimer: NewThrottleTimer("Client", flushThrottleMS),
|
|
|
|
mustConnect: mustConnect,
|
2016-01-22 15:50:11 -08:00
|
|
|
|
2016-03-23 21:07:03 -07:00
|
|
|
addr: addr,
|
|
|
|
reqSent: list.New(),
|
|
|
|
resCb: nil,
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
2016-02-21 23:55:45 -08:00
|
|
|
cli.QuitService = *NewQuitService(nil, "Client", cli)
|
2016-03-23 21:07:03 -07:00
|
|
|
_, err := cli.Start() // Just start it, it's confusing for callers to remember to start.
|
|
|
|
if mustConnect {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return cli, nil
|
|
|
|
}
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 21:07:03 -07:00
|
|
|
func (cli *Client) OnStart() (err error) {
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.QuitService.OnStart()
|
2016-03-23 21:07:03 -07:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
RETRY_LOOP:
|
|
|
|
for {
|
|
|
|
conn, err_ := Connect(cli.addr)
|
|
|
|
if err_ != nil {
|
|
|
|
if cli.mustConnect {
|
|
|
|
err = err_ // OnStart() will return this.
|
|
|
|
close(doneCh)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Printf("tmsp.Client failed to connect to %v. Retrying...\n", cli.addr)
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
continue RETRY_LOOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go cli.sendRequestsRoutine(conn)
|
|
|
|
go cli.recvResponseRoutine(conn)
|
|
|
|
close(doneCh) // OnStart() will return no error.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-doneCh
|
|
|
|
return // err
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) OnStop() {
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.QuitService.OnStop()
|
2016-03-23 21:07:03 -07:00
|
|
|
if cli.conn != nil {
|
|
|
|
cli.conn.Close()
|
|
|
|
}
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-08 00:50:00 -08:00
|
|
|
// Set listener for all responses
|
2016-01-22 15:50:11 -08:00
|
|
|
// NOTE: callback may get internally generated flush responses.
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) SetResponseCallback(resCb Callback) {
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.mtx.Lock()
|
|
|
|
defer cli.mtx.Unlock()
|
|
|
|
cli.resCb = resCb
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) StopForError(err error) {
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.mtx.Lock()
|
2016-03-23 21:07:03 -07:00
|
|
|
fmt.Printf("Stopping tmsp.Client for error: %v\n", err.Error())
|
2016-01-22 15:50:11 -08:00
|
|
|
if cli.err == nil {
|
|
|
|
cli.err = err
|
|
|
|
}
|
|
|
|
cli.mtx.Unlock()
|
|
|
|
cli.Stop()
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) Error() error {
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.mtx.Lock()
|
|
|
|
defer cli.mtx.Unlock()
|
|
|
|
return cli.err
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
2016-03-23 21:07:03 -07:00
|
|
|
func (cli *Client) sendRequestsRoutine(conn net.Conn) {
|
|
|
|
w := bufio.NewWriter(conn)
|
2016-01-22 15:50:11 -08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-cli.flushTimer.Ch:
|
|
|
|
select {
|
2016-02-27 07:02:34 +00:00
|
|
|
case cli.reqQueue <- NewReqRes(types.RequestFlush()):
|
2016-01-22 15:50:11 -08:00
|
|
|
default:
|
|
|
|
// Probably will fill the buffer, or retry later.
|
|
|
|
}
|
|
|
|
case <-cli.QuitService.Quit:
|
|
|
|
return
|
|
|
|
case reqres := <-cli.reqQueue:
|
|
|
|
cli.willSendReq(reqres)
|
2016-03-23 21:07:03 -07:00
|
|
|
err := types.WriteMessage(reqres.Request, w)
|
2016-01-22 15:50:11 -08:00
|
|
|
if err != nil {
|
|
|
|
cli.StopForError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// log.Debug("Sent request", "requestType", reflect.TypeOf(reqres.Request), "request", reqres.Request)
|
2016-01-31 19:56:02 -08:00
|
|
|
if reqres.Request.Type == types.MessageType_Flush {
|
2016-03-23 21:07:03 -07:00
|
|
|
err = w.Flush()
|
2016-01-22 15:50:11 -08:00
|
|
|
if err != nil {
|
|
|
|
cli.StopForError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:07:03 -07:00
|
|
|
func (cli *Client) recvResponseRoutine(conn net.Conn) {
|
|
|
|
r := bufio.NewReader(conn) // Buffer reads
|
2016-01-22 15:50:11 -08:00
|
|
|
for {
|
2016-01-30 19:36:33 -08:00
|
|
|
var res = &types.Response{}
|
|
|
|
err := types.ReadMessage(r, res)
|
2016-01-22 15:50:11 -08:00
|
|
|
if err != nil {
|
|
|
|
cli.StopForError(err)
|
|
|
|
return
|
|
|
|
}
|
2016-01-30 19:36:33 -08:00
|
|
|
switch res.Type {
|
2016-01-31 19:56:02 -08:00
|
|
|
case types.MessageType_Exception:
|
2016-01-25 08:01:18 -08:00
|
|
|
// XXX After setting cli.err, release waiters (e.g. reqres.Done())
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.StopForError(errors.New(res.Error))
|
|
|
|
default:
|
|
|
|
// log.Debug("Received response", "responseType", reflect.TypeOf(res), "response", res)
|
|
|
|
err := cli.didRecvResponse(res)
|
|
|
|
if err != nil {
|
|
|
|
cli.StopForError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) willSendReq(reqres *ReqRes) {
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.mtx.Lock()
|
|
|
|
defer cli.mtx.Unlock()
|
|
|
|
cli.reqSent.PushBack(reqres)
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) didRecvResponse(res *types.Response) error {
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.mtx.Lock()
|
|
|
|
defer cli.mtx.Unlock()
|
|
|
|
|
2016-02-08 00:50:00 -08:00
|
|
|
// Get the first ReqRes
|
2016-01-22 15:50:11 -08:00
|
|
|
next := cli.reqSent.Front()
|
|
|
|
if next == nil {
|
2016-01-30 19:36:33 -08:00
|
|
|
return fmt.Errorf("Unexpected result type %v when nothing expected", res.Type)
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
2016-02-08 00:50:00 -08:00
|
|
|
reqres := next.Value.(*ReqRes)
|
2016-01-22 15:50:11 -08:00
|
|
|
if !resMatchesReq(reqres.Request, res) {
|
|
|
|
return fmt.Errorf("Unexpected result type %v when response to %v expected",
|
2016-01-30 19:36:33 -08:00
|
|
|
res.Type, reqres.Request.Type)
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
reqres.Response = res // Set response
|
|
|
|
reqres.Done() // Release waiters
|
|
|
|
cli.reqSent.Remove(next) // Pop first item from linked list
|
|
|
|
|
2016-02-08 00:50:00 -08:00
|
|
|
// Notify reqRes listener if set
|
|
|
|
if cb := reqres.GetCallback(); cb != nil {
|
|
|
|
cb(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify client listener if set
|
2016-01-22 15:50:11 -08:00
|
|
|
if cli.resCb != nil {
|
|
|
|
cli.resCb(reqres.Request, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) EchoAsync(msg string) *ReqRes {
|
2016-02-08 00:50:00 -08:00
|
|
|
return cli.queueRequest(types.RequestEcho(msg))
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) FlushAsync() *ReqRes {
|
2016-02-08 00:50:00 -08:00
|
|
|
return cli.queueRequest(types.RequestFlush())
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) SetOptionAsync(key string, value string) *ReqRes {
|
2016-02-08 00:50:00 -08:00
|
|
|
return cli.queueRequest(types.RequestSetOption(key, value))
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) AppendTxAsync(tx []byte) *ReqRes {
|
2016-02-08 00:50:00 -08:00
|
|
|
return cli.queueRequest(types.RequestAppendTx(tx))
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) CheckTxAsync(tx []byte) *ReqRes {
|
2016-02-08 00:50:00 -08:00
|
|
|
return cli.queueRequest(types.RequestCheckTx(tx))
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) CommitAsync() *ReqRes {
|
2016-02-14 13:11:06 -08:00
|
|
|
return cli.queueRequest(types.RequestCommit())
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) QueryAsync(query []byte) *ReqRes {
|
2016-02-08 00:50:00 -08:00
|
|
|
return cli.queueRequest(types.RequestQuery(query))
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-05 19:18:34 -08:00
|
|
|
func (cli *Client) InitChainAsync(validators []*types.Validator) *ReqRes {
|
|
|
|
return cli.queueRequest(types.RequestInitChain(validators))
|
2016-02-28 19:19:29 -08:00
|
|
|
}
|
|
|
|
|
2016-03-06 17:57:47 -08:00
|
|
|
func (cli *Client) EndBlockAsync(height uint64) *ReqRes {
|
|
|
|
return cli.queueRequest(types.RequestEndBlock(height))
|
2016-02-28 19:19:29 -08:00
|
|
|
}
|
|
|
|
|
2016-01-22 15:50:11 -08:00
|
|
|
//----------------------------------------
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) FlushSync() error {
|
2016-02-14 14:59:53 -08:00
|
|
|
cli.queueRequest(types.RequestFlush()).Wait()
|
|
|
|
return cli.err
|
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) InfoSync() (info string, err error) {
|
2016-01-30 19:36:33 -08:00
|
|
|
reqres := cli.queueRequest(types.RequestInfo())
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
2016-01-25 08:01:18 -08:00
|
|
|
return "", cli.err
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
2016-01-30 19:36:33 -08:00
|
|
|
return string(reqres.Response.Data), nil
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) SetOptionSync(key string, value string) (log string, err error) {
|
2016-02-14 14:59:53 -08:00
|
|
|
reqres := cli.queueRequest(types.RequestSetOption(key, value))
|
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
|
|
|
return "", cli.err
|
|
|
|
}
|
|
|
|
return reqres.Response.Log, nil
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:50:29 -07:00
|
|
|
func (cli *Client) AppendTxSync(tx []byte) (res types.Result) {
|
2016-01-30 19:36:33 -08:00
|
|
|
reqres := cli.queueRequest(types.RequestAppendTx(tx))
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error())
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
2016-03-23 02:50:29 -07:00
|
|
|
resp := reqres.Response
|
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
|
2016-01-25 08:01:18 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:50:29 -07:00
|
|
|
func (cli *Client) CheckTxSync(tx []byte) (res types.Result) {
|
2016-01-30 19:36:33 -08:00
|
|
|
reqres := cli.queueRequest(types.RequestCheckTx(tx))
|
2016-01-25 08:01:18 -08:00
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error())
|
2016-01-25 08:01:18 -08:00
|
|
|
}
|
2016-03-23 02:50:29 -07:00
|
|
|
resp := reqres.Response
|
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:50:29 -07:00
|
|
|
func (cli *Client) CommitSync() (res types.Result) {
|
2016-02-14 13:11:06 -08:00
|
|
|
reqres := cli.queueRequest(types.RequestCommit())
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error())
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
2016-03-23 02:50:29 -07:00
|
|
|
resp := reqres.Response
|
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:50:29 -07:00
|
|
|
func (cli *Client) QuerySync(query []byte) (res types.Result) {
|
2016-01-30 19:36:33 -08:00
|
|
|
reqres := cli.queueRequest(types.RequestQuery(query))
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.ErrInternalError.SetLog(cli.err.Error())
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
2016-03-23 02:50:29 -07:00
|
|
|
resp := reqres.Response
|
|
|
|
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-05 19:18:34 -08:00
|
|
|
func (cli *Client) InitChainSync(validators []*types.Validator) (err error) {
|
|
|
|
cli.queueRequest(types.RequestInitChain(validators))
|
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
|
|
|
return cli.err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-06 17:57:47 -08:00
|
|
|
func (cli *Client) EndBlockSync(height uint64) (validators []*types.Validator, err error) {
|
|
|
|
reqres := cli.queueRequest(types.RequestEndBlock(height))
|
2016-02-28 19:19:29 -08:00
|
|
|
cli.FlushSync()
|
|
|
|
if cli.err != nil {
|
|
|
|
return nil, cli.err
|
|
|
|
}
|
|
|
|
return reqres.Response.Validators, nil
|
|
|
|
}
|
|
|
|
|
2016-01-22 15:50:11 -08:00
|
|
|
//----------------------------------------
|
|
|
|
|
2016-02-21 23:55:45 -08:00
|
|
|
func (cli *Client) queueRequest(req *types.Request) *ReqRes {
|
2016-02-27 07:02:34 +00:00
|
|
|
reqres := NewReqRes(req)
|
2016-01-22 15:50:11 -08:00
|
|
|
// TODO: set cli.err if reqQueue times out
|
|
|
|
cli.reqQueue <- reqres
|
|
|
|
|
|
|
|
// Maybe auto-flush, or unset auto-flush
|
2016-01-30 19:36:33 -08:00
|
|
|
switch req.Type {
|
2016-01-31 19:56:02 -08:00
|
|
|
case types.MessageType_Flush:
|
2016-01-22 15:50:11 -08:00
|
|
|
cli.flushTimer.Unset()
|
|
|
|
default:
|
|
|
|
cli.flushTimer.Set()
|
|
|
|
}
|
|
|
|
|
|
|
|
return reqres
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
2016-01-30 19:36:33 -08:00
|
|
|
func resMatchesReq(req *types.Request, res *types.Response) (ok bool) {
|
2016-01-31 19:56:02 -08:00
|
|
|
return req.Type == res.Type
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-08 00:50:00 -08:00
|
|
|
type ReqRes struct {
|
2016-01-30 19:36:33 -08:00
|
|
|
*types.Request
|
2016-01-22 15:50:11 -08:00
|
|
|
*sync.WaitGroup
|
2016-01-30 19:36:33 -08:00
|
|
|
*types.Response // Not set atomically, so be sure to use WaitGroup.
|
2016-02-08 00:50:00 -08:00
|
|
|
|
|
|
|
mtx sync.Mutex
|
|
|
|
done bool // Gets set to true once *after* WaitGroup.Done().
|
|
|
|
cb func(*types.Response) // A single callback that may be set.
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
|
2016-02-27 07:02:34 +00:00
|
|
|
func NewReqRes(req *types.Request) *ReqRes {
|
2016-02-08 00:50:00 -08:00
|
|
|
return &ReqRes{
|
2016-01-22 15:50:11 -08:00
|
|
|
Request: req,
|
|
|
|
WaitGroup: waitGroup1(),
|
|
|
|
Response: nil,
|
2016-02-08 00:50:00 -08:00
|
|
|
|
|
|
|
done: false,
|
|
|
|
cb: nil,
|
2016-01-22 15:50:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:50:00 -08:00
|
|
|
// Sets the callback for this ReqRes atomically.
|
|
|
|
// If reqRes is already done, calls cb immediately.
|
|
|
|
// NOTE: reqRes.cb should not change if reqRes.done.
|
|
|
|
// NOTE: only one callback is supported.
|
|
|
|
func (reqRes *ReqRes) SetCallback(cb func(res *types.Response)) {
|
|
|
|
reqRes.mtx.Lock()
|
|
|
|
|
|
|
|
if reqRes.done {
|
|
|
|
reqRes.mtx.Unlock()
|
|
|
|
cb(reqRes.Response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer reqRes.mtx.Unlock()
|
|
|
|
reqRes.cb = cb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reqRes *ReqRes) GetCallback() func(*types.Response) {
|
|
|
|
reqRes.mtx.Lock()
|
|
|
|
defer reqRes.mtx.Unlock()
|
|
|
|
return reqRes.cb
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: it should be safe to read reqRes.cb without locks after this.
|
|
|
|
func (reqRes *ReqRes) SetDone() {
|
|
|
|
reqRes.mtx.Lock()
|
|
|
|
reqRes.done = true
|
|
|
|
reqRes.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-01-22 15:50:11 -08:00
|
|
|
func waitGroup1() (wg *sync.WaitGroup) {
|
|
|
|
wg = &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
return
|
|
|
|
}
|