Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions scamp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scamp
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -254,13 +255,17 @@ func (conn *Connection) routePacket(pkt *Packet) (err error) {

const RetryLimit = 50

// ErrConnectionClosed is returned by Send when a packet write fails because the
// underlying connection is closed or broken; callers should reconnect/retry.
var ErrConnectionClosed = errors.New("scamp: connection closed or broken during send")

// Send sends a scamp message using the current *Connection
func (conn *Connection) Send(msg *Message) (err error) {
if conn == nil {
return fmt.Errorf("cannot send on nil connection")
}
if conn.isClosed {
err = fmt.Errorf("connection already closed")
return fmt.Errorf("connection already closed")
}

conn.readWriterLock.Lock()
Expand Down Expand Up @@ -292,16 +297,12 @@ func (conn *Connection) Send(msg *Message) (err error) {
_, err := pkt.Write(conn.readWriter)
// TODO: should we actually blacklist this error?
if err != nil {
// temprarily
if strings.Contains(err.Error(), "use of closed connection") {
// NOTE: this error is intentionally swallowed (matches
// pre-existing behavior); the outer Send returns nil here.
break
}
// TODO: attempt to reconnect
if strings.Contains(err.Error(), "broken pipe") {
// NOTE: error intentionally swallowed (pre-existing behavior).
break
// A closed or broken connection won't recover by retrying;
// propagate so callers can detect it and reconnect/retry
// (previously these were swallowed and Send returned nil).
if strings.Contains(err.Error(), "use of closed connection") ||
strings.Contains(err.Error(), "broken pipe") {
return fmt.Errorf("%w: %v", ErrConnectionClosed, err)
}

if retries > RetryLimit {
Expand Down
7 changes: 7 additions & 0 deletions scamp/serviceproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func newServiceProxy(classRecordsRaw []byte, certRaw []byte, sigRaw []byte) (sp
if err != nil {
return
}
if len(classRecords) < 8 {
return nil, fmt.Errorf("malformed announce: expected at least 8 class records, got %d", len(classRecords))
}
// OMG, position-based, heterogenously typed values in an array suck to deal with.
err = json.Unmarshal(classRecords[0], &sp.version)
if err != nil {
Expand Down Expand Up @@ -270,6 +273,10 @@ func newServiceProxy(classRecordsRaw []byte, certRaw []byte, sigRaw []byte) (sp
return nil, err
}

if len(actionsRawMessages) < 2 {
return nil, fmt.Errorf("malformed announce: action spec needs at least 2 entries, got %d", len(actionsRawMessages))
}

err = json.Unmarshal(actionsRawMessages[0], &classes[i].actions[j].actionName)
if err != nil {
return nil, err
Expand Down
Loading