Skip to content

Commit

Permalink
Include num of retries in transaction result
Browse files Browse the repository at this point in the history
Resolves #74
  • Loading branch information
enobufs committed Jul 15, 2019
1 parent f95fbce commit 02ee07e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 6 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,11 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
tr.StopRtxTimer()
c.trMap.Delete(trKey)

if !tr.WriteResult(client.TransactionResult{Msg: msg, From: from}) {
if !tr.WriteResult(client.TransactionResult{
Msg: msg,
From: from,
Retries: tr.Retries(),
}) {
c.log.Debugf("no listener for %s", msg.String())
}

Expand Down Expand Up @@ -498,7 +502,7 @@ func (c *Client) handleChannelData(data []byte) error {
return nil
}

func (c *Client) onRtxTimeout(trKey string, nRtx int32) {
func (c *Client) onRtxTimeout(trKey string, nRtx int) {
tr, ok := c.trMap.Find(trKey)
if !ok {
return // already gone
Expand Down
19 changes: 14 additions & 5 deletions internal/client/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const (

// TransactionResult is a bag of result values of a transaction
type TransactionResult struct {
Msg *stun.Message
From net.Addr
Err error
Msg *stun.Message
From net.Addr
Retries int
Err error
}

// TransactionConfig is a set of confi params used by NewTransaction
Expand All @@ -32,7 +33,7 @@ type Transaction struct {
Key string // read-only
Raw []byte // read-only
To net.Addr // read-only
nRtx int32 // modified only by the timer thread
nRtx int // modified only by the timer thread
interval time.Duration // modified only by the timer thread
timer *time.Timer // therad-safe, set only by the creator, and stopper
resultCh chan TransactionResult // thread-safe
Expand All @@ -51,7 +52,7 @@ func NewTransaction(config *TransactionConfig) *Transaction {
}

// StartRtxTimer starts the transaction timer
func (t *Transaction) StartRtxTimer(onTimeout func(trKey string, nRtx int32)) {
func (t *Transaction) StartRtxTimer(onTimeout func(trKey string, nRtx int)) {
t.mutex.Lock()
defer t.mutex.Unlock()

Expand Down Expand Up @@ -95,6 +96,14 @@ func (t *Transaction) Close() {
close(t.resultCh)
}

// Retries returns the number of retransmission it has made
func (t *Transaction) Retries() int {
t.mutex.RLock()
defer t.mutex.RUnlock()

return t.nRtx
}

////////////////////////////////////////////////////////////////////////////////

// TransactionMap is a thread-safe transaction map
Expand Down

0 comments on commit 02ee07e

Please sign in to comment.