diff --git a/common/turbotunnel/clientmap.go b/common/turbotunnel/clientmap.go index 53d03024216953312e150bce6c82ede572838182..2d039476394a39c5a2ff0b822275472dd61641e3 100644 --- a/common/turbotunnel/clientmap.go +++ b/common/turbotunnel/clientmap.go @@ -57,8 +57,9 @@ func NewClientMap(timeout time.Duration) *ClientMap { // necessary. func (m *ClientMap) SendQueue(addr net.Addr) chan []byte { m.lock.Lock() - defer m.lock.Unlock() - return m.inner.SendQueue(addr, time.Now()) + queue := m.inner.SendQueue(addr, time.Now()) + m.lock.Unlock() + return queue } // clientMapInner is the inner type of ClientMap, implementing heap.Interface. diff --git a/common/turbotunnel/clientmap_test.go b/common/turbotunnel/clientmap_test.go new file mode 100644 index 0000000000000000000000000000000000000000..57d794aefe00d749eb80097a55836fe135451237 --- /dev/null +++ b/common/turbotunnel/clientmap_test.go @@ -0,0 +1,18 @@ +package turbotunnel + +import ( + "testing" + "time" +) + +// Benchmark the ClientMap.SendQueue function. This is mainly measuring the cost +// of the mutex operations around the call to clientMapInner.SendQueue. +func BenchmarkSendQueue(b *testing.B) { + m := NewClientMap(1 * time.Hour) + id := NewClientID() + m.SendQueue(id) // populate the entry for id + b.ResetTimer() + for i := 0; i < b.N; i++ { + m.SendQueue(id) + } +}