better error when connection fails

This commit is contained in:
Alex Grintsvayg 2019-06-05 11:03:55 -04:00
parent 2e81b1ab03
commit c649636eeb
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
5 changed files with 18 additions and 9 deletions

View file

@ -27,13 +27,13 @@ func sendBlobCmd(cmd *cobra.Command, args []string) {
c := reflector.Client{} c := reflector.Client{}
err := c.Connect(addr) err := c.Connect(addr)
if err != nil { if err != nil {
log.Fatal("error connecting client to server", err) log.Fatal("error connecting client to server: ", err)
} }
blob := make(stream.Blob, 1024) blob := make(stream.Blob, 1024)
_, err = rand.Read(blob) _, err = rand.Read(blob)
if err != nil { if err != nil {
log.Fatal("failed to make random blob", err) log.Fatal("failed to make random blob: ", err)
} }
err = c.SendBlob(blob) err = c.SendBlob(blob)

View file

@ -1,7 +1,13 @@
package main package main
import "github.com/lbryio/reflector.go/cmd" import (
"math/rand"
"time"
"github.com/lbryio/reflector.go/cmd"
)
func main() { func main() {
rand.Seed(time.Now().UnixNano())
cmd.Execute() cmd.Execute()
} }

View file

@ -69,7 +69,7 @@ func (c *Client) SendBlob(blob stream.Blob) error {
} }
if !sendResp.SendBlob { if !sendResp.SendBlob {
return ErrBlobExists return errors.Prefix(blobHash[:8], ErrBlobExists)
} }
log.Println("Sending blob " + blobHash[:8]) log.Println("Sending blob " + blobHash[:8])
@ -96,7 +96,7 @@ func (c *Client) doHandshake(version int) error {
return errors.Err("not connected") return errors.Err("not connected")
} }
handshake, err := json.Marshal(handshakeRequestResponse{Version: version}) handshake, err := json.Marshal(handshakeRequestResponse{Version: &version})
if err != nil { if err != nil {
return err return err
} }
@ -110,7 +110,9 @@ func (c *Client) doHandshake(version int) error {
err = json.NewDecoder(c.conn).Decode(&resp) err = json.NewDecoder(c.conn).Decode(&resp)
if err != nil { if err != nil {
return err return err
} else if resp.Version != version { } else if resp.Version == nil {
return errors.Err("invalid handshake")
} else if *resp.Version != version {
return errors.Err("handshake version mismatch") return errors.Err("handshake version mismatch")
} }

View file

@ -274,7 +274,9 @@ func (s *Server) doHandshake(conn net.Conn) error {
err := s.read(conn, &handshake) err := s.read(conn, &handshake)
if err != nil { if err != nil {
return err return err
} else if handshake.Version != protocolVersion1 && handshake.Version != protocolVersion2 { } else if handshake.Version == nil {
return errors.Err("handshake is missing protocol version")
} else if *handshake.Version != protocolVersion1 && *handshake.Version != protocolVersion2 {
return errors.Err("protocol version not supported") return errors.Err("protocol version not supported")
} }
@ -411,7 +413,7 @@ func BlobHash(blob []byte) string {
//} //}
type handshakeRequestResponse struct { type handshakeRequestResponse struct {
Version int `json:"version"` Version *int `json:"version"`
} }
type sendBlobRequest struct { type sendBlobRequest struct {

View file

@ -67,7 +67,6 @@ func (n *Node) Connect(addrs []string, config *tls.Config) error {
} }
// shuffle addresses for load balancing // shuffle addresses for load balancing
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(addrs), func(i, j int) { addrs[i], addrs[j] = addrs[j], addrs[i] }) rand.Shuffle(len(addrs), func(i, j int) { addrs[i], addrs[j] = addrs[j], addrs[i] })
var err error var err error