-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from athos/feature/retry-connection
Retry on connection
- Loading branch information
Showing
10 changed files
with
138 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
type ConnBuilder interface { | ||
Connect() (net.Conn, error) | ||
} | ||
|
||
type TCPConnBuilder struct { | ||
Host string | ||
Port int | ||
} | ||
|
||
func (conn *TCPConnBuilder) Connect() (net.Conn, error) { | ||
return net.Dial("tcp", fmt.Sprintf("%s:%d", conn.Host, conn.Port)) | ||
} | ||
|
||
type ConnBuilderFunc func() (net.Conn, error) | ||
|
||
func (f ConnBuilderFunc) Connect() (net.Conn, error) { | ||
return f() | ||
} | ||
|
||
func NewRetryConnBuilder(connBuilder ConnBuilder, timeout time.Duration, interval time.Duration) ConnBuilder { | ||
if interval > timeout { | ||
interval = timeout | ||
} | ||
return ConnBuilderFunc(func() (conn net.Conn, err error) { | ||
end := time.Now().Add(timeout) | ||
for { | ||
conn, err = connBuilder.Connect() | ||
if err == nil { | ||
return conn, nil | ||
} | ||
if time.Now().After(end) { | ||
return | ||
} | ||
time.Sleep(interval) | ||
if time.Now().After(end) { | ||
return | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.