From bdf7e37ddc8bcd461b5bbf08dddc43e370a59eac Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Fri, 14 Sep 2018 15:25:23 -0700 Subject: [PATCH] Make send() retry with exponential backoff --- p2p/peer.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/p2p/peer.go b/p2p/peer.go index 7b1d2a19c..b02eca667 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -129,12 +129,21 @@ func sendWithSocketClient(ip, port string, message []byte) (err error) { } // Send a message to another node with given port. -func send(ip, port string, message []byte) (returnMessage string) { +func send(ip, port string, message []byte) { // Add attack code here. attack.GetInstance().Run() - sendWithSocketClient(ip, port, message) - return + backoff := NewExpBackoff(250*time.Millisecond, 10*time.Second, 2) + + for { + err := sendWithSocketClient(ip, port, message) + if err == nil { + break + } + log.Printf("sleeping %s before trying to send to %s again", + backoff.Cur, net.JoinHostPort(ip, port)) + backoff.Sleep() + } } func DialWithSocketClient(ip, port string) (conn net.Conn, err error) {