Skip to content

Commit

Permalink
Merge pull request #462 from blinklabs-io/feat/dial-timeout
Browse files Browse the repository at this point in the history
feat: connect timeout on dial
  • Loading branch information
agaffney authored Dec 18, 2023
2 parents e519033 + 50b37c7 commit 860c8d3
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"io"
"net"
"sync"
"time"

"github.com/blinklabs-io/gouroboros/muxer"
"github.com/blinklabs-io/gouroboros/protocol"
Expand All @@ -43,6 +44,11 @@ import (
"github.com/blinklabs-io/gouroboros/protocol/txsubmission"
)

const (
// Default connection timeout
DefaultConnectTimeout = 30 * time.Second
)

// The Connection type is a wrapper around a net.Conn object that handles communication using the Ouroboros network protocol over that connection
type Connection struct {
conn net.Conn
Expand Down Expand Up @@ -118,15 +124,21 @@ func (c *Connection) ErrorChan() chan error {
return c.errorChan
}

// Dial will establish a connection using the specified protocol and address. These parameters are
// passed to the [net.Dial] func. The handshake will be started when a connection is established.
// Dial will establish a connection using the specified protocol and address. It works the same as [DialTimeout],
// except that it provides a default connect timeout
func (c *Connection) Dial(proto string, address string) error {
return c.DialTimeout(proto, address, DefaultConnectTimeout)
}

// DialTimeout will establish a connection using the specified protocol, address, and timeout. These parameters are
// passed to the [net.DialTimeout] func. The handshake will be started when a connection is established.
// An error will be returned if the connection fails, a connection was already established, or the
// handshake fails
func (c *Connection) Dial(proto string, address string) error {
func (c *Connection) DialTimeout(proto string, address string, timeout time.Duration) error {
if c.conn != nil {
return fmt.Errorf("a connection was already established")
}
conn, err := net.Dial(proto, address)
conn, err := net.DialTimeout(proto, address, timeout)
if err != nil {
return err
}
Expand Down

0 comments on commit 860c8d3

Please sign in to comment.