Commit bbf9f440 authored by Levin Zimmermann's avatar Levin Zimmermann

Dial: Catch NotPrimaryMaster & return custom error

After initial handshake a NEO node checks the identification of its peer
by sending the 'RequestIdentification' packet. In case the peer is a
secondary master it responds with 'NotPrimaryMaster'. Before this patch
'Dial' ignored the 'NotPrimaryMaster' packet and simply returned a
general error. Now - after this patch - 'Dial' returns an instance of
'proto.NotPrimaryMaster' (which implements 'Error').
This helps a caller to correctly handle the secondary-master-case, which
otherwise is impossible to differentiate from any other error
possibility.
parent 8811f8b4
...@@ -135,17 +135,37 @@ func Dial(ctx context.Context, typ proto.NodeType, net xnet.Networker, addr stri ...@@ -135,17 +135,37 @@ func Dial(ctx context.Context, typ proto.NodeType, net xnet.Networker, addr stri
// start, and if peer sends new connection in that window it will be rejected. // start, and if peer sends new connection in that window it will be rejected.
// //
// TODO thinking. // TODO thinking.
err = link.Ask1(reqID, accept)
conn, err := link.NewConn()
if err != nil {
return err
}
defer conn.Close()
err = conn.Send(reqID)
if err != nil { if err != nil {
return err return err
} }
if accept.NodeType != typ { notPrimary := &proto.NotPrimaryMaster{}
// TODO send Error to peer? nerr := &proto.Error{}
return fmt.Errorf("accepted, but peer is not %s (identifies as %s)", typ, accept.NodeType)
which, err := conn.Expect(nerr, notPrimary, accept)
switch which {
case 0:
return nerr
case 1:
return notPrimary
case 2:
if accept.NodeType != typ {
// TODO send Error to peer?
return fmt.Errorf("accepted, but peer is not %s (identifies as %s)", typ, accept.NodeType)
}
return nil
} }
return err
return nil
}) })
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment