Skip to content

Commit 860c8d3

Browse files
authored
Merge pull request #462 from blinklabs-io/feat/dial-timeout
feat: connect timeout on dial
2 parents e519033 + 50b37c7 commit 860c8d3

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

connection.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"io"
3030
"net"
3131
"sync"
32+
"time"
3233

3334
"github.com/blinklabs-io/gouroboros/muxer"
3435
"github.com/blinklabs-io/gouroboros/protocol"
@@ -43,6 +44,11 @@ import (
4344
"github.com/blinklabs-io/gouroboros/protocol/txsubmission"
4445
)
4546

47+
const (
48+
// Default connection timeout
49+
DefaultConnectTimeout = 30 * time.Second
50+
)
51+
4652
// The Connection type is a wrapper around a net.Conn object that handles communication using the Ouroboros network protocol over that connection
4753
type Connection struct {
4854
conn net.Conn
@@ -118,15 +124,21 @@ func (c *Connection) ErrorChan() chan error {
118124
return c.errorChan
119125
}
120126

121-
// Dial will establish a connection using the specified protocol and address. These parameters are
122-
// passed to the [net.Dial] func. The handshake will be started when a connection is established.
127+
// Dial will establish a connection using the specified protocol and address. It works the same as [DialTimeout],
128+
// except that it provides a default connect timeout
129+
func (c *Connection) Dial(proto string, address string) error {
130+
return c.DialTimeout(proto, address, DefaultConnectTimeout)
131+
}
132+
133+
// DialTimeout will establish a connection using the specified protocol, address, and timeout. These parameters are
134+
// passed to the [net.DialTimeout] func. The handshake will be started when a connection is established.
123135
// An error will be returned if the connection fails, a connection was already established, or the
124136
// handshake fails
125-
func (c *Connection) Dial(proto string, address string) error {
137+
func (c *Connection) DialTimeout(proto string, address string, timeout time.Duration) error {
126138
if c.conn != nil {
127139
return fmt.Errorf("a connection was already established")
128140
}
129-
conn, err := net.Dial(proto, address)
141+
conn, err := net.DialTimeout(proto, address, timeout)
130142
if err != nil {
131143
return err
132144
}

0 commit comments

Comments
 (0)