test wallet server connection

This commit is contained in:
Alex Grintsvayg 2020-07-01 13:23:44 -04:00 committed by Niko Storni
parent 264390a2b2
commit 41d758ef5c
2 changed files with 49 additions and 11 deletions

View file

@ -76,6 +76,9 @@ func (n *Node) Connect(addrs []string, config *tls.Config) error {
if err == nil { if err == nil {
break break
} }
if errors.Is(err, ErrTimeout) {
continue
}
if e, ok := err.(*net.OpError); ok && e.Err.Error() == "no such host" { if e, ok := err.(*net.OpError); ok && e.Err.Error() == "no such host" {
// net.errNoSuchHost is not exported, so we have to string-match // net.errNoSuchHost is not exported, so we have to string-match
continue continue

View file

@ -5,9 +5,12 @@ package wallet
import ( import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"encoding/json"
"fmt"
"net" "net"
"time" "time"
"github.com/lbryio/lbry.go/extras/errors"
"github.com/lbryio/lbry.go/v2/extras/stop" "github.com/lbryio/lbry.go/v2/extras/stop"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -50,15 +53,31 @@ func NewTransport(addr string, config *tls.Config) (*TCPTransport, error) {
t.grp.Add(1) t.grp.Add(1)
go func() { go func() {
t.grp.Done() defer t.grp.Done()
t.listen() t.listen()
}() }()
err = t.test()
if err != nil {
t.grp.StopAndWait()
return nil, errors.Prefix(addr, err)
}
return t, nil return t, nil
} }
const delimiter = byte('\n') const delimiter = byte('\n')
func (t *TCPTransport) Send(body []byte) error {
log.Debugf("%s <- %s", t.conn.RemoteAddr(), body)
_, err := t.conn.Write(body)
return err
}
func (t *TCPTransport) Responses() <-chan []byte { return t.responses }
func (t *TCPTransport) Errors() <-chan error { return t.errors }
func (t *TCPTransport) Shutdown() { t.grp.StopAndWait() }
func (t *TCPTransport) listen() { func (t *TCPTransport) listen() {
reader := bufio.NewReader(t.conn) reader := bufio.NewReader(t.conn)
for { for {
@ -74,12 +93,6 @@ func (t *TCPTransport) listen() {
} }
} }
func (t *TCPTransport) Send(body []byte) error {
log.Debugf("%s <- %s", t.conn.RemoteAddr(), body)
_, err := t.conn.Write(body)
return err
}
func (t *TCPTransport) error(err error) { func (t *TCPTransport) error(err error) {
select { select {
case t.errors <- err: case t.errors <- err:
@ -87,11 +100,33 @@ func (t *TCPTransport) error(err error) {
} }
} }
func (t *TCPTransport) Responses() <-chan []byte { return t.responses } func (t *TCPTransport) test() error {
func (t *TCPTransport) Errors() <-chan error { return t.errors } err := t.Send([]byte(`{"id":1,"method":"server.version"}` + "\n"))
if err != nil {
return errors.Err(err)
}
func (t *TCPTransport) Shutdown() { var data []byte
t.grp.StopAndWait() select {
case data = <-t.Responses():
case <-time.Tick(1 * time.Second):
return errors.Err(ErrTimeout)
}
var response struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}
err = json.Unmarshal(data, &response)
if err != nil {
return errors.Err(err)
}
if response.Error.Message != "" {
return fmt.Errorf(response.Error.Message)
}
return nil
} }
func (t *TCPTransport) close() { func (t *TCPTransport) close() {