Skip to content

Commit

Permalink
Problem: socket interactions randomly fail due to EINTR (zeromq#285)
Browse files Browse the repository at this point in the history
Solution: retry functions that are likely to fail with EINTR
  • Loading branch information
tarmo authored Oct 13, 2020
1 parent 224a2d1 commit fbfd1a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
9 changes: 9 additions & 0 deletions goczmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import "C"

import (
"errors"
"syscall"
)

const (
Expand Down Expand Up @@ -202,3 +203,11 @@ func getStringType(k int) string {
return ""
}
}

func isRetryableError(err error) bool {
if err == nil {
return false
}
eno, ok := err.(syscall.Errno)
return ok && eno == syscall.EINTR
}
24 changes: 19 additions & 5 deletions sock.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ func (s *Sock) Connect(endpoint string) error {
cEndpoint := C.CString(endpoint)
defer C.free(unsafe.Pointer(cEndpoint))

rc := C.Sock_connect(s.zsockT, cEndpoint)
Connect:
rc, err := C.Sock_connect(s.zsockT, cEndpoint)
if rc != C.int(0) {
if isRetryableError(err) {
goto Connect
}
return ErrConnect
}
return nil
Expand Down Expand Up @@ -285,13 +289,19 @@ func (s *Sock) Pollout() bool {
// a multi-part message
func (s *Sock) SendFrame(data []byte, flags int) error {
var rc C.int
var err error

SendFrame:
if len(data) == 0 {
rc = C.Sock_sendframe(s.zsockT, nil, C.size_t(0), C.int(flags))
rc, err = C.Sock_sendframe(s.zsockT, nil, C.size_t(0), C.int(flags))
} else {
rc = C.Sock_sendframe(s.zsockT, unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(flags))
rc, err = C.Sock_sendframe(s.zsockT, unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(flags))
}
if rc == C.int(-1) {
return ErrSendFrame
if isRetryableError(err) {
goto SendFrame
}
return err
}
return nil
}
Expand All @@ -304,8 +314,12 @@ func (s *Sock) RecvFrame() ([]byte, int, error) {
return nil, -1, ErrRecvFrameAfterDestroy
}

frame := C.zframe_recv(unsafe.Pointer(s.zsockT))
RecvFrame:
frame, err := C.zframe_recv(unsafe.Pointer(s.zsockT))
if frame == nil {
if isRetryableError(err) {
goto RecvFrame
}
return []byte{0}, 0, ErrRecvFrame
}
dataSize := C.zframe_size(frame)
Expand Down

0 comments on commit fbfd1a3

Please sign in to comment.