mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-31 04:01:55 +00:00
Replace ResultsCh with ResponsesCh
This commit is contained in:
@@ -40,9 +40,8 @@ type WSClient struct {
|
||||
// https://godoc.org/github.com/rcrowley/go-metrics#Timer.
|
||||
PingPongLatencyTimer metrics.Timer
|
||||
|
||||
// user facing channels, closed only when the client is being stopped.
|
||||
ResultsCh chan json.RawMessage
|
||||
ErrorsCh chan error
|
||||
// Single user facing channel to read RPCResponses from, closed only when the client is being stopped.
|
||||
ResponsesCh chan types.RPCResponse
|
||||
|
||||
// Callback, which will be called each time after successful reconnect.
|
||||
onReconnect func()
|
||||
@@ -149,8 +148,7 @@ func (c *WSClient) OnStart() error {
|
||||
return err
|
||||
}
|
||||
|
||||
c.ResultsCh = make(chan json.RawMessage)
|
||||
c.ErrorsCh = make(chan error)
|
||||
c.ResponsesCh = make(chan types.RPCResponse)
|
||||
|
||||
c.send = make(chan types.RPCRequest)
|
||||
// 1 additional error may come from the read/write
|
||||
@@ -175,8 +173,7 @@ func (c *WSClient) Stop() bool {
|
||||
success := c.BaseService.Stop()
|
||||
// only close user-facing channels when we can't write to them
|
||||
c.wg.Wait()
|
||||
close(c.ResultsCh)
|
||||
close(c.ErrorsCh)
|
||||
close(c.ResponsesCh)
|
||||
return success
|
||||
}
|
||||
|
||||
@@ -193,7 +190,7 @@ func (c *WSClient) IsActive() bool {
|
||||
}
|
||||
|
||||
// Send the given RPC request to the server. Results will be available on
|
||||
// ResultsCh, errors, if any, on ErrorsCh. Will block until send succeeds or
|
||||
// ResponsesCh, errors, if any, on ErrorsCh. Will block until send succeeds or
|
||||
// ctx.Done is closed.
|
||||
func (c *WSClient) Send(ctx context.Context, request types.RPCRequest) error {
|
||||
select {
|
||||
@@ -438,19 +435,14 @@ func (c *WSClient) readRoutine() {
|
||||
err = json.Unmarshal(data, &response)
|
||||
if err != nil {
|
||||
c.Logger.Error("failed to parse response", "err", err, "data", string(data))
|
||||
c.ErrorsCh <- err
|
||||
continue
|
||||
}
|
||||
if response.Error != nil {
|
||||
c.ErrorsCh <- response.Error
|
||||
continue
|
||||
}
|
||||
c.Logger.Info("got response", "resp", response.Result)
|
||||
// Combine a non-blocking read on writeRoutineQuit with a non-blocking write on ResultsCh to avoid blocking
|
||||
// Combine a non-blocking read on writeRoutineQuit with a non-blocking write on ResponsesCh to avoid blocking
|
||||
// c.wg.Wait() in c.Stop()
|
||||
select {
|
||||
case <-c.writeRoutineQuit:
|
||||
case c.ResultsCh <- *response.Result:
|
||||
case c.ResponsesCh <- response:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -125,8 +125,7 @@ func TestWSClientReconnectFailure(t *testing.T) {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-c.ResultsCh:
|
||||
case <-c.ErrorsCh:
|
||||
case <-c.ResponsesCh:
|
||||
case <-c.Quit:
|
||||
return
|
||||
}
|
||||
@@ -163,7 +162,7 @@ func TestWSClientReconnectFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotBlockingOnStop(t *testing.T) {
|
||||
timeout := 2 *time.Second
|
||||
timeout := 2 * time.Second
|
||||
s := httptest.NewServer(&myHandler{})
|
||||
c := startClient(t, s.Listener.Addr())
|
||||
c.Call(context.Background(), "a", make(map[string]interface{}))
|
||||
@@ -171,10 +170,10 @@ func TestNotBlockingOnStop(t *testing.T) {
|
||||
time.Sleep(time.Second)
|
||||
passCh := make(chan struct{})
|
||||
go func() {
|
||||
// Unless we have a non-blocking write to ResultsCh from readRoutine
|
||||
// Unless we have a non-blocking write to ResponsesCh from readRoutine
|
||||
// this blocks forever ont the waitgroup
|
||||
c.Stop()
|
||||
passCh <- struct{}{}
|
||||
c.Stop()
|
||||
passCh <- struct{}{}
|
||||
}()
|
||||
select {
|
||||
case <-passCh:
|
||||
@@ -201,13 +200,12 @@ func call(t *testing.T, method string, c *WSClient) {
|
||||
func callWgDoneOnResult(t *testing.T, c *WSClient, wg *sync.WaitGroup) {
|
||||
for {
|
||||
select {
|
||||
case res := <-c.ResultsCh:
|
||||
if res != nil {
|
||||
wg.Done()
|
||||
case resp := <-c.ResponsesCh:
|
||||
if resp.Error != nil {
|
||||
t.Fatalf("unexpected error: %v", resp.Error)
|
||||
}
|
||||
case err := <-c.ErrorsCh:
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
if *resp.Result != nil {
|
||||
wg.Done()
|
||||
}
|
||||
case <-c.Quit:
|
||||
return
|
||||
|
Reference in New Issue
Block a user