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
Next Next commit
Remove DialOptions
  • Loading branch information
kylecarbs committed Jul 20, 2021
commit 857a743c14f57ef75d9705daa60473397cdfb5fe
6 changes: 3 additions & 3 deletions wsnet/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// dialerFunc is used to reference a dialer returned for caching.
type dialerFunc func(ctx context.Context, key string, options *DialOptions) (*Dialer, error)
type dialerFunc func(ctx context.Context, key string) (*Dialer, error)

// DialCache constructs a new DialerCache.
// The cache clears connections that:
Expand Down Expand Up @@ -98,7 +98,7 @@ func (d *DialerCache) evict() {

// Dial returns a Dialer from the cache if one exists with the key provided,
// or dials a new connection using the dialerFunc.
func (d *DialerCache) Dial(ctx context.Context, key string, options *DialOptions) (*Dialer, bool, error) {
func (d *DialerCache) Dial(ctx context.Context, key string) (*Dialer, bool, error) {
d.mut.RLock()
if dialer, ok := d.dialers[key]; ok {
closed := false
Expand All @@ -119,7 +119,7 @@ func (d *DialerCache) Dial(ctx context.Context, key string, options *DialOptions
d.mut.RUnlock()

dialer, err, _ := d.flightGroup.Do(key, func() (interface{}, error) {
dialer, err := d.dialerFunc(ctx, key, options)
dialer, err := d.dialerFunc(ctx, key)
if err != nil {
return nil, err
}
Expand Down
24 changes: 12 additions & 12 deletions wsnet/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ func TestCache(t *testing.T) {
require.NoError(t, err)
defer l.Close()

cache := DialCache(time.Hour, func(ctx context.Context, key string, options *DialOptions) (*Dialer, error) {
return DialWebsocket(ctx, connectAddr, options)
cache := DialCache(time.Hour, func(ctx context.Context, key string) (*Dialer, error) {
return DialWebsocket(ctx, connectAddr, nil)
})
_, cached, err := cache.Dial(context.Background(), "example", nil)
_, cached, err := cache.Dial(context.Background(), "example")
require.NoError(t, err)
require.Equal(t, cached, false)
_, cached, err = cache.Dial(context.Background(), "example", nil)
_, cached, err = cache.Dial(context.Background(), "example")
require.NoError(t, err)
require.Equal(t, cached, true)
})
Expand All @@ -33,15 +33,15 @@ func TestCache(t *testing.T) {
require.NoError(t, err)
defer l.Close()

cache := DialCache(time.Hour, func(ctx context.Context, key string, options *DialOptions) (*Dialer, error) {
return DialWebsocket(ctx, connectAddr, options)
cache := DialCache(time.Hour, func(ctx context.Context, key string) (*Dialer, error) {
return DialWebsocket(ctx, connectAddr, nil)
})

conn, cached, err := cache.Dial(context.Background(), "example", nil)
conn, cached, err := cache.Dial(context.Background(), "example")
require.NoError(t, err)
require.Equal(t, cached, false)
require.NoError(t, conn.Close())
_, cached, err = cache.Dial(context.Background(), "example", nil)
_, cached, err = cache.Dial(context.Background(), "example")
require.NoError(t, err)
require.Equal(t, cached, false)
})
Expand All @@ -52,15 +52,15 @@ func TestCache(t *testing.T) {
require.NoError(t, err)
defer l.Close()

cache := DialCache(0, func(ctx context.Context, key string, options *DialOptions) (*Dialer, error) {
return DialWebsocket(ctx, connectAddr, options)
cache := DialCache(0, func(ctx context.Context, key string) (*Dialer, error) {
return DialWebsocket(ctx, connectAddr, nil)
})

_, cached, err := cache.Dial(context.Background(), "example", nil)
_, cached, err := cache.Dial(context.Background(), "example")
require.NoError(t, err)
require.Equal(t, cached, false)
cache.evict()
_, cached, err = cache.Dial(context.Background(), "example", nil)
_, cached, err = cache.Dial(context.Background(), "example")
require.NoError(t, err)
require.Equal(t, cached, false)
})
Expand Down