Skip to content

Commit

Permalink
Merge pull request #129 from colinzuo/fix-heartbeat
Browse files Browse the repository at this point in the history
Fix heartbeat
  • Loading branch information
worg authored Feb 6, 2024
2 parents 8ba92d1 + 319b37b commit b7da9a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
37 changes: 28 additions & 9 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func Connect(conn io.ReadWriteCloser, opts ...func(*Conn) error) (*Conn, error)
}
}

if readTimeout < options.ReadTimeout {
readTimeout = options.ReadTimeout
}

c.readTimeout = readTimeout
c.writeTimeout = writeTimeout

Expand Down Expand Up @@ -367,24 +371,32 @@ func processLoop(c *Conn, writer *frame.Writer) {

switch req.Frame.Command {
case frame.SUBSCRIBE:
id, _ := req.Frame.Header.Contains(frame.Id)
channels[id] = req.C

// if using a temp queue, map that destination as a known channel
// however, don't send the frame, it's most likely an invalid destination
// on the broker.
if replyTo, ok := req.Frame.Header.Contains(ReplyToHeader); ok {
channels[replyTo] = req.C
sendFrame = false
} else {
id, _ := req.Frame.Header.Contains(frame.Id)
channels[id] = req.C
}

case frame.UNSUBSCRIBE:
id, _ := req.Frame.Header.Contains(frame.Id)
// is this trying to be too clever -- add a receipt
// header so that when the server responds with a
// RECEIPT frame, the corresponding channel will be closed
req.Frame.Header.Set(frame.Receipt, id)

if replyTo, ok := req.Frame.Header.Contains(ReplyToHeader); ok {
ch, ok := channels[replyTo]
if ok {
delete(channels, replyTo)
close(ch)
}
sendFrame = false
} else {
id, _ := req.Frame.Header.Contains(frame.Id)
// is this trying to be too clever -- add a receipt
// header so that when the server responds with a
// RECEIPT frame, the corresponding channel will be closed
req.Frame.Header.Set(frame.Receipt, id)
}
}

// frame to send, if enabled
Expand Down Expand Up @@ -645,6 +657,12 @@ func (c *Conn) Subscribe(destination string, ack AckMode, opts ...func(*frame.Fr
}
}

replyTo, replyToSet := subscribeFrame.Header.Contains(ReplyToHeader)

if replyToSet {
subscribeFrame.Header.Set(frame.Id, replyTo)
}

// If the option functions have not specified the "id" header entry,
// create one.
id, ok := subscribeFrame.Header.Contains(frame.Id)
Expand All @@ -661,6 +679,7 @@ func (c *Conn) Subscribe(destination string, ack AckMode, opts ...func(*frame.Fr
closeMutex := &sync.Mutex{}
sub := &Subscription{
id: id,
replyToSet: replyToSet,
destination: destination,
conn: c,
ackMode: ack,
Expand Down
5 changes: 5 additions & 0 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
type Subscription struct {
C chan *Message
id string
replyToSet bool
destination string
conn *Conn
ackMode AckMode
Expand Down Expand Up @@ -75,6 +76,10 @@ func (s *Subscription) Unsubscribe(opts ...func(*frame.Frame) error) error {
}
}

if s.replyToSet {
f.Header.Set(ReplyToHeader, s.id)
}

s.conn.sendFrame(f)

// UNSUBSCRIBE is a bit weird in that it is tagged with a "receipt" header
Expand Down

0 comments on commit b7da9a7

Please sign in to comment.