Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

feat: Add DialCache for key-based connection caching #391

Merged
merged 7 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fixup
  • Loading branch information
kylecarbs committed Jul 21, 2021
commit 844385e02b8636b0b5d359541189f02211a06dc7
22 changes: 15 additions & 7 deletions wsnet/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type DialerCache struct {
closed chan struct{}
mut sync.RWMutex

// Key is the "key" of a dialer.
// Key is the "key" of a dialer, which is usually the workspace ID.
dialers map[string]*Dialer
atime map[string]time.Time
}
Expand Down Expand Up @@ -81,13 +81,21 @@ func (d *DialerCache) evict() {
}
}

if evict {
_ = dialer.Close()
d.mut.Lock()
delete(d.atime, key)
delete(d.dialers, key)
d.mut.Unlock()
if !evict {
return
}

_ = dialer.Close()
// Ensure after Ping and potential delays that we're still testing against
// the proper dialer.
if dialer != d.dialers[key] {
return
}

d.mut.Lock()
defer d.mut.Unlock()
delete(d.atime, key)
delete(d.dialers, key)
}()
}
d.mut.RUnlock()
Expand Down
5 changes: 3 additions & 2 deletions wsnet/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ func TestCache(t *testing.T) {

cache := DialCache(0)

_, cached, err := cache.Dial(context.Background(), "example", dialFunc(connectAddr))
c1, cached, err := cache.Dial(context.Background(), "example", dialFunc(connectAddr))
require.NoError(t, err)
require.Equal(t, cached, false)
cache.evict()
_, cached, err = cache.Dial(context.Background(), "example", dialFunc(connectAddr))
c2, cached, err := cache.Dial(context.Background(), "example", dialFunc(connectAddr))
require.NoError(t, err)
require.Equal(t, cached, false)
assert.NotSame(t, c1, c2)
})
}