Skip to content

Add environment vairable to randomize resolver addresses #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,4 @@ endef
.PHONY: gettrivy
gettrivy:
@mkdir -p $(TOOLS_BIN)
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b $(TOOLS_BIN) v0.38.3
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b $(TOOLS_BIN) v0.51.2
3 changes: 3 additions & 0 deletions coherence/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const (
// envResolverDebug enables resolver debug messages to be displayed.
envResolverDebug = "COHERENCE_RESOLVER_DEBUG"

// envResolverDebug enables randomization of addresses returned by resolver
envResolverRandomize = "COHERENCE_RESOLVER_RANDOMIZE"

// Integer.MAX_VALUE on Java
integerMaxValue = 2147483647

Expand Down
4 changes: 4 additions & 0 deletions coherence/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ If you have multiple clusters on the same port, you can also append the cluster

coherence.WithAddress("coherence:///localhost:7574/cluster2")

When using the 'coherence' gRPC resolver, the addresses are always tried in the same order as they are returned from the
Name Service lookup. The Go Client randomizes any addresses, but you can turn this off by setting the
environment variable COHERENCE_RESOLVER_RANDOMIZER=false.

To Configure SSL, you must first enable SSL on the gRPC Proxy, see [gRPC Proxy documentation] for details.
Refer to the section on [NewSession] for more information on setting up a SSL connection on the client.

Expand Down
14 changes: 13 additions & 1 deletion coherence/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/oracle/coherence-go-client/coherence/discovery"
"google.golang.org/grpc/resolver"
"log"
"math/rand"
"strings"
"sync"
"time"
)

const (
Expand All @@ -25,6 +27,7 @@ var (
resolverDebug = func(v ...any) {
// noop as default debug mode
}
randomizeAddresses bool
)

type nsLookupResolverBuilder struct {
Expand Down Expand Up @@ -68,7 +71,16 @@ func (r *nsLookupResolver) resolve() {
for i, s := range grpcEndpoints {
addresses[i] = resolver.Address{Addr: s}
}
resolverDebug(fmt.Sprintf("resolver produced the following addresses: %v", addresses))

if randomizeAddresses {
// randomize the address list
rand.NewSource(time.Now().UnixNano())
rand.Shuffle(len(addresses), func(i, j int) {
addresses[i], addresses[j] = addresses[j], addresses[i]
})
}

resolverDebug(fmt.Sprintf("resolver produced the following addresses: %v, randomize=%v", addresses, randomizeAddresses))
_ = r.cc.UpdateState(resolver.State{Addresses: addresses})
}

Expand Down
4 changes: 4 additions & 0 deletions coherence/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func NewSession(ctx context.Context, options ...func(session *SessionOptions)) (
DisconnectTimeout: time.Duration(0) * time.Second},
}

if getBoolValueFromEnvVarOrDefault(envResolverRandomize, true) {
randomizeAddresses = true
}

// ensure name resolver has been registered
resolver.Register(&nsLookupResolverBuilder{})

Expand Down
Loading