From 02ee07e2e951af131809eb397a6a2ffe8ee4370d Mon Sep 17 00:00:00 2001 From: Yutaka Takeda Date: Fri, 12 Jul 2019 21:25:32 -0700 Subject: [PATCH] Include num of retries in transaction result Resolves #74 --- client.go | 8 ++++++-- internal/client/transaction.go | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 741a8d27..5681c940 100644 --- a/client.go +++ b/client.go @@ -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()) } @@ -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 diff --git a/internal/client/transaction.go b/internal/client/transaction.go index 18271850..36cc6cbd 100644 --- a/internal/client/transaction.go +++ b/internal/client/transaction.go @@ -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 @@ -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 @@ -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() @@ -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