Skip to content

Commit

Permalink
smpc: simple reconnect attempt after stream fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Loong committed Jun 18, 2018
1 parent 4422289 commit 81a4ec9
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions smpc/smpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (smpc *smpcer) Connect(networkID NetworkID, nodes identity.Addresses) {

// A background goroutine will handle the stream
logger.Network(logger.LevelDebug, fmt.Sprintf("connected to %v in network %v", addr, networkID))
go smpc.handleStream(addr, stream)
go smpc.handleStream(ctx, addr, stream)
})
}

Expand Down Expand Up @@ -204,7 +204,20 @@ func (smpc *smpcer) query(addr identity.Address) (identity.MultiAddress, error)
return multiAddr, nil
}

func (smpc *smpcer) handleStream(remoteAddr identity.Address, remoteStream stream.Stream) {
func (smpc *smpcer) handleStream(ctx context.Context, remoteAddr identity.Address, remoteStream stream.Stream) {
defer func() {
if multiAddr, ok := smpc.lookup[remoteAddr]; ok {
stream, err := smpc.streamer.Open(ctx, multiAddr)
if err != nil {
log.Println(fmt.Errorf("cannot reconnect stream to smpc node %v: %v", remoteAddr, err))
return
}

// A background goroutine will handle the stream
logger.Network(logger.LevelDebug, fmt.Sprintf("reconnected to %v", remoteAddr))
go smpc.handleStream(ctx, remoteAddr, stream)
}
}()
for {
message := Message{}
if err := remoteStream.Recv(&message); err != nil {
Expand Down

0 comments on commit 81a4ec9

Please sign in to comment.