Skip to content

Commit

Permalink
Limit resolving concurrency (#4)
Browse files Browse the repository at this point in the history
* early return when ResolveNow is already resolving in another goroutine

* fix broken test
  • Loading branch information
glenn-kim authored Jul 13, 2023
1 parent 1067653 commit 01012f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
7 changes: 5 additions & 2 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ type resolver struct {
}

func (c *resolver) ResolveNow(grpcresolver.ResolveNowOptions) {
c.mu.RLock()
defer c.mu.RUnlock()
locked := c.mu.TryLock()
if !locked { // already resolving
return
}
defer c.mu.Unlock()

if c.isClosed {
return
Expand Down
39 changes: 26 additions & 13 deletions target_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cloudmap

import (
"net/url"
"reflect"
"testing"

Expand Down Expand Up @@ -65,7 +66,9 @@ func Test_parseTarget(t *testing.T) {
name: "unexpected scheme",
args: args{
t: grpcresolver.Target{
Scheme: "https",
URL: url.URL{
Scheme: "https",
},
},
},
wantErr: true,
Expand All @@ -74,7 +77,9 @@ func Test_parseTarget(t *testing.T) {
name: "empty namespace",
args: args{
t: grpcresolver.Target{
Scheme: "https",
URL: url.URL{
Scheme: Scheme,
},
},
},
wantErr: true,
Expand All @@ -83,8 +88,10 @@ func Test_parseTarget(t *testing.T) {
name: "empty service",
args: args{
t: grpcresolver.Target{
Scheme: Scheme,
Authority: "test-namespace",
URL: url.URL{
Scheme: Scheme,
Host: "test-namespace",
},
},
},
wantErr: true,
Expand All @@ -93,9 +100,11 @@ func Test_parseTarget(t *testing.T) {
name: "normal",
args: args{
t: grpcresolver.Target{
Scheme: Scheme,
Authority: "test-namespace",
Endpoint: "test-service",
URL: url.URL{
Scheme: Scheme,
Host: "test-namespace",
Path: "test-service",
},
},
},
want: &target{
Expand All @@ -107,9 +116,11 @@ func Test_parseTarget(t *testing.T) {
name: "with slash",
args: args{
t: grpcresolver.Target{
Scheme: Scheme,
Authority: "test-namespace",
Endpoint: "test/service",
URL: url.URL{
Scheme: Scheme,
Host: "test-namespace",
Path: "test/service",
},
},
},
want: &target{
Expand All @@ -121,9 +132,11 @@ func Test_parseTarget(t *testing.T) {
name: "with whitespace",
args: args{
t: grpcresolver.Target{
Scheme: Scheme,
Authority: "test-namespace",
Endpoint: "test%20service",
URL: url.URL{
Scheme: Scheme,
Host: "test-namespace",
Path: "test%20service",
},
},
},
want: &target{
Expand Down

0 comments on commit 01012f2

Please sign in to comment.