minor renames

This commit is contained in:
Alex Grintsvayg 2019-01-16 11:45:28 -05:00
parent 6acb99c74d
commit 557988d2cb

View file

@ -5,7 +5,6 @@ package wallet
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt"
"math/rand" "math/rand"
"net" "net"
"sync" "sync"
@ -30,40 +29,21 @@ var (
ErrTimeout = errors.Base("timeout") ErrTimeout = errors.Base("timeout")
) )
type responseError struct { type response struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (r responseError) Error() string { return fmt.Sprintf("%d: %s", r.Code, r.Message) }
type resp struct {
data []byte data []byte
err error err error
} }
type response struct {
Id uint32 `json:"id"`
Method string `json:"method"`
Error responseError `json:"error"`
}
type request struct {
Id uint32 `json:"id"`
Method string `json:"method"`
Params []string `json:"params"`
}
type Node struct { type Node struct {
transport *TCPTransport transport *TCPTransport
nextId atomic.Uint32 nextId atomic.Uint32
grp *stop.Group grp *stop.Group
handlersMu *sync.RWMutex handlersMu *sync.RWMutex
handlers map[uint32]chan resp handlers map[uint32]chan response
pushHandlersMu *sync.RWMutex pushHandlersMu *sync.RWMutex
pushHandlers map[string][]chan resp pushHandlers map[string][]chan response
timeout time.Duration timeout time.Duration
} }
@ -71,8 +51,8 @@ type Node struct {
// NewNode creates a new node. // NewNode creates a new node.
func NewNode() *Node { func NewNode() *Node {
return &Node{ return &Node{
handlers: make(map[uint32]chan resp), handlers: make(map[uint32]chan response),
pushHandlers: make(map[string][]chan resp), pushHandlers: make(map[string][]chan response),
handlersMu: &sync.RWMutex{}, handlersMu: &sync.RWMutex{},
pushHandlersMu: &sync.RWMutex{}, pushHandlersMu: &sync.RWMutex{},
grp: stop.New(), grp: stop.New(),
@ -160,15 +140,22 @@ func (n *Node) listen() {
case <-n.grp.Ch(): case <-n.grp.Ch():
return return
case bytes := <-n.transport.Responses(): case bytes := <-n.transport.Responses():
msg := &response{} msg := &struct {
Id uint32 `json:"id"`
Method string `json:"method"`
Error struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error"`
}{}
if err := json.Unmarshal(bytes, msg); err != nil { if err := json.Unmarshal(bytes, msg); err != nil {
n.err(err) n.err(err)
continue continue
} }
r := resp{} r := response{}
if len(msg.Error.Message) > 0 { if len(msg.Error.Message) > 0 {
r.err = msg.Error r.err = errors.Base("%d: %s", msg.Error.Code, msg.Error.Message)
} else { } else {
r.data = bytes r.data = bytes
} }
@ -207,7 +194,11 @@ func (n *Node) listen() {
// request makes a request to the server and unmarshals the response into v. // request makes a request to the server and unmarshals the response into v.
func (n *Node) request(method string, params []string, v interface{}) error { func (n *Node) request(method string, params []string, v interface{}) error {
msg := request{ msg := struct {
Id uint32 `json:"id"`
Method string `json:"method"`
Params []string `json:"params"`
}{
Id: n.nextId.Load(), Id: n.nextId.Load(),
Method: method, Method: method,
Params: params, Params: params,
@ -220,7 +211,7 @@ func (n *Node) request(method string, params []string, v interface{}) error {
} }
bytes = append(bytes, delimiter) bytes = append(bytes, delimiter)
c := make(chan resp, 1) c := make(chan response, 1)
n.handlersMu.Lock() n.handlersMu.Lock()
n.handlers[msg.Id] = c n.handlers[msg.Id] = c
@ -231,11 +222,11 @@ func (n *Node) request(method string, params []string, v interface{}) error {
return err return err
} }
var r resp var r response
select { select {
case r = <-c: case r = <-c:
case <-time.After(n.timeout): case <-time.After(n.timeout):
r = resp{err: errors.Err(ErrTimeout)} r = response{err: errors.Err(ErrTimeout)}
} }
n.handlersMu.Lock() n.handlersMu.Lock()