Skip to content

Commit

Permalink
os.ErrDeadlineExceeded not in Go 1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Aug 26, 2024
1 parent 242d60f commit 37edc0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions internal/protocol/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net"
"os"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -264,9 +263,11 @@ func Handshake(ctx context.Context, conn net.Conn, version uint64) (*Protocol, e
defer stop()

// Perform the protocol handshake.
// TODO: this should be a message and use same code for writing and reading
// to the network.
n, err := conn.Write(protocol)
if err != nil {
if canceled && errors.Is(err, os.ErrDeadlineExceeded) {
if canceled && errors.Cause(err).(net.Error).Timeout() {
return nil, errors.Wrap(err, "write handshake")
}
return nil, errors.Wrap(err, "write handshake")
Expand Down
11 changes: 6 additions & 5 deletions internal/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"io"
"net"
"os"
"sync"
"time"

Expand Down Expand Up @@ -76,14 +75,14 @@ func (p *Protocol) Call(ctx context.Context, request, response *Message) (err er
desc := requestDesc(request.mtype)

if err = p.send(request); err != nil {
if canceled && errors.Is(err, os.ErrDeadlineExceeded) {
if canceled && errors.Cause(err).(net.Error).Timeout() {
return errors.Wrapf(err, "call %s (canceled): send", desc)
}
return errors.Wrapf(err, "call %s (budget %s): send", desc, budget)
}

if err = p.recv(response); err != nil {
if canceled && errors.Is(err, os.ErrDeadlineExceeded) {
if canceled && errors.Cause(err).(net.Error).Timeout() {
return errors.Wrapf(err, "call %s (canceled): receive", desc)
}
return errors.Wrapf(err, "call %s (budget %s): receive", desc, budget)
Expand Down Expand Up @@ -125,16 +124,18 @@ func (p *Protocol) Interrupt(ctx context.Context, request *Message, response *Me

EncodeInterrupt(request, 0)

// TODO: the context cancelation and honoring should happen in the protocol
// primitives which can be tested better.
if err := p.send(request); err != nil {
if canceled && errors.Is(err, os.ErrDeadlineExceeded) {
if canceled && errors.Cause(err).(net.Error).Timeout() {
return errors.Wrapf(err, "interrupt request (canceled): send")
}
return errors.Wrapf(err, "interrupt request (budget %s): send", budget)
}

for {
if err := p.recv(response); err != nil {
if canceled && errors.Is(err, os.ErrDeadlineExceeded) {
if canceled && errors.Cause(err).(net.Error).Timeout() {
return errors.Wrapf(err, "interrupt request (canceled): receive")
}
return errors.Wrapf(err, "interrupt request (budget %s): receive", budget)
Expand Down

0 comments on commit 37edc0b

Please sign in to comment.