Skip to content
This repository has been archived by the owner on Dec 21, 2019. It is now read-only.

Commit

Permalink
socket: support network IO deadline (#339)
Browse files Browse the repository at this point in the history
This allows connection-oriented protocols to recover from bad
network conditions, by enabling to set a limit on the time for
network I/O.
As a special case, it allows client/servers to recover from a
bad connection during the initial TLS handshake.
  • Loading branch information
Gerrit Renker authored and Gerrit Renker committed Apr 11, 2019
1 parent 1f0e2e7 commit b6c5a34
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
23 changes: 23 additions & 0 deletions conn.go
Expand Up @@ -19,6 +19,7 @@ import (
"io"
"net"
"sync"
"time"
)

// conn implements the Pipe interface on top of net.Conn. The
Expand Down Expand Up @@ -49,6 +50,10 @@ func (p *conn) Recv() (*Message, error) {
var err error
var msg *Message

if err := p.setNetworkTimeout(); err != nil {
return nil, err
}

if err = binary.Read(p.c, binary.BigEndian, &sz); err != nil {
return nil, err
}
Expand Down Expand Up @@ -78,6 +83,10 @@ func (p *conn) Send(msg *Message) error {
return nil
}

if err := p.setNetworkTimeout(); err != nil {
return err
}

// send length header
if err := binary.Write(p.c, binary.BigEndian, l); err != nil {
return err
Expand Down Expand Up @@ -181,6 +190,10 @@ func (p *conn) handshake(props []interface{}) error {
p.maxrx = int64(v.(int))
}

if err := p.setNetworkTimeout(); err != nil {
return err
}

h := connHeader{S: 'S', P: 'P', Proto: p.proto.Number()}
if err = binary.Write(p.c, binary.BigEndian, &h); err != nil {
return err
Expand All @@ -207,3 +220,13 @@ func (p *conn) handshake(props []interface{}) error {
p.open = true
return nil
}

func (p *conn) setNetworkTimeout() error {
if v, err := p.sock.GetOption(OptionNetworkIoDeadline); err == nil {
// Socket implementation ensures that option type is time.Duration.
if iotimeout := v.(time.Duration); iotimeout > 0 {
return p.c.SetDeadline(time.Now().Add(iotimeout))
}
}
return nil
}
14 changes: 12 additions & 2 deletions core.go
Expand Up @@ -46,8 +46,9 @@ type socket struct {
recverr error // error to return on attempts to Recv()
senderr error // error to return on attempts to Send()

rdeadline time.Duration
wdeadline time.Duration
rdeadline time.Duration // mangos socket read deadline
wdeadline time.Duration // mangos socket write deadline
iodeadline time.Duration // IO timeout for connection used by pipe
reconntime time.Duration // reconnect time after error or disconnect
reconnmax time.Duration // max reconnect interval
linger time.Duration
Expand Down Expand Up @@ -437,6 +438,11 @@ func (sock *socket) SetOption(name string, value interface{}) error {
sock.wdeadline = value.(time.Duration)
sock.Unlock()
return nil
case OptionNetworkIoDeadline:
sock.Lock()
sock.iodeadline = value.(time.Duration)
sock.Unlock()
return nil
case OptionLinger:
sock.Lock()
sock.linger = value.(time.Duration)
Expand Down Expand Up @@ -523,6 +529,10 @@ func (sock *socket) GetOption(name string) (interface{}, error) {
sock.Lock()
defer sock.Unlock()
return sock.wdeadline, nil
case OptionNetworkIoDeadline:
sock.Lock()
defer sock.Unlock()
return sock.iodeadline, nil
case OptionLinger:
sock.Lock()
defer sock.Unlock()
Expand Down
7 changes: 7 additions & 0 deletions options.go
Expand Up @@ -39,6 +39,13 @@ const (
// non-blocking operation. By default there is no timeout.
OptionSendDeadline = "SEND-DEADLINE"

// OptionNetworkIoDeadline enables recovery from bad connections for
// connection-based transports; by limiting the time until a (read or
// write) network operation times out. The value is a time.Duration.
// Setting this option is recommended for connection-based protocols,
// it is disabled by default.
OptionNetworkIoDeadline = "NETWORK-IO-DEADLINE"

// OptionRetryTime is used by REQ. The argument is a time.Duration.
// When a request has not been replied to within the given duration,
// the request will automatically be resent to an available peer.
Expand Down
23 changes: 18 additions & 5 deletions transport/tlstcp/tlstcp.go
Expand Up @@ -85,13 +85,26 @@ func (o options) configTCP(conn *net.TCPConn) error {
return err
}
}
if v, ok := o[mangos.OptionNetworkIoDeadline]; ok {
if err := conn.SetDeadline(time.Now().Add(v.(time.Duration))); err != nil {
return err
}
}

return nil
}

func newOptions(t *tlsTran) options {
o := make(map[string]interface{})
o[mangos.OptionTLSConfig] = t.config
func newOptions(sock mangos.Socket, t *tlsTran) options {
var o = map[string]interface{}{
mangos.OptionTLSConfig: t.config,
}

// I/O timeout is needed to avoid blocking on the initial TLS handshake (#339).
if v, err := sock.GetOption(mangos.OptionNetworkIoDeadline); err == nil {
if iotimeout := v.(time.Duration); iotimeout > 0 {
o[mangos.OptionNetworkIoDeadline] = iotimeout
}
}
return options(o)
}

Expand Down Expand Up @@ -233,15 +246,15 @@ func (t *tlsTran) NewDialer(addr string, sock mangos.Socket) (mangos.PipeDialer,
return nil, err
}

d := &dialer{sock: sock, opts: newOptions(t), addr: addr}
d := &dialer{sock: sock, opts: newOptions(sock, t), addr: addr}

return d, nil
}

// NewAccepter implements the Transport NewAccepter method.
func (t *tlsTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListener, error) {
var err error
l := &listener{sock: sock, opts: newOptions(t)}
l := &listener{sock: sock, opts: newOptions(sock, t)}

if addr, err = mangos.StripScheme(t, addr); err != nil {
return nil, err
Expand Down

0 comments on commit b6c5a34

Please sign in to comment.