Skip to content

Commit

Permalink
remove hardcoded references to localhost and port 6379
Browse files Browse the repository at this point in the history
Signed-off-by: jbrinkman <[email protected]>
  • Loading branch information
jbrinkman committed Feb 6, 2025
1 parent 5bb236e commit 0678965
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
20 changes: 10 additions & 10 deletions go/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ package api
import "github.com/valkey-io/valkey-glide/go/glide/protobuf"

const (
defaultHost = "localhost"
defaultPort = 6379
DefaultHost = "localhost"
DefaultPort = 6379
)

// NodeAddress represents the host address and port of a node in the cluster.
type NodeAddress struct {
Host string // If not supplied, "localhost" will be used.
Port int // If not supplied, 6379 will be used.
Host string // If not supplied, api.DefaultHost will be used.
Port int // If not supplied, api.DefaultPost will be used.
}

func (addr *NodeAddress) toProtobuf() *protobuf.NodeAddress {
if addr.Host == "" {
addr.Host = defaultHost
addr.Host = DefaultHost
}

if addr.Port == 0 {
addr.Port = defaultPort
addr.Port = DefaultPort
}

return &protobuf.NodeAddress{Host: addr.Host, Port: uint32(addr.Port)}
Expand Down Expand Up @@ -176,9 +176,9 @@ func (config *GlideClientConfiguration) toProtobuf() *protobuf.ConnectionRequest
//
// config := NewGlideClientConfiguration().
// WithAddress(&NodeAddress{
// Host: "sample-address-0001.use1.cache.amazonaws.com", Port: 6379}).
// Host: "sample-address-0001.use1.cache.amazonaws.com", Port: api.DefaultPost}).
// WithAddress(&NodeAddress{
// Host: "sample-address-0002.use1.cache.amazonaws.com", Port: 6379})
// Host: "sample-address-0002.use1.cache.amazonaws.com", Port: api.DefaultPost})
func (config *GlideClientConfiguration) WithAddress(address *NodeAddress) *GlideClientConfiguration {
config.addresses = append(config.addresses, *address)
return config
Expand Down Expand Up @@ -264,9 +264,9 @@ func (config *GlideClusterClientConfiguration) toProtobuf() *protobuf.Connection
//
// config := NewGlideClusterClientConfiguration().
// WithAddress(&NodeAddress{
// Host: "sample-address-0001.use1.cache.amazonaws.com", Port: 6379}).
// Host: "sample-address-0001.use1.cache.amazonaws.com", Port: api.DefaultPost}).
// WithAddress(&NodeAddress{
// Host: "sample-address-0002.use1.cache.amazonaws.com", Port: 6379})
// Host: "sample-address-0002.use1.cache.amazonaws.com", Port: api.DefaultPost})
func (config *GlideClusterClientConfiguration) WithAddress(address *NodeAddress) *GlideClusterClientConfiguration {
config.addresses = append(config.addresses, *address)
return config
Expand Down
6 changes: 3 additions & 3 deletions go/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func TestNodeAddress(t *testing.T) {
input NodeAddress
expected *protobuf.NodeAddress
}{
{NodeAddress{}, &protobuf.NodeAddress{Host: defaultHost, Port: defaultPort}},
{NodeAddress{Host: "host"}, &protobuf.NodeAddress{Host: "host", Port: defaultPort}},
{NodeAddress{Port: 1234}, &protobuf.NodeAddress{Host: defaultHost, Port: 1234}},
{NodeAddress{}, &protobuf.NodeAddress{Host: DefaultHost, Port: DefaultPort}},
{NodeAddress{Host: "host"}, &protobuf.NodeAddress{Host: "host", Port: DefaultPort}},
{NodeAddress{Port: 1234}, &protobuf.NodeAddress{Host: DefaultHost, Port: 1234}},
{NodeAddress{"host", 1234}, &protobuf.NodeAddress{Host: "host", Port: 1234}},
}

Expand Down
4 changes: 2 additions & 2 deletions go/benchmarks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func parseArguments() *options {
dataSize := flag.String("dataSize", "[100]", "Data block size")
concurrentTasks := flag.String("concurrentTasks", "[1 10 100 1000]", "Number of concurrent tasks")
clientNames := flag.String("clients", "all", "One of: all|go-redis|glide")
host := flag.String("host", "localhost", "Hostname")
port := flag.Int("port", 6379, "Port number")
host := flag.String("host", api.DefaultHost, "Hostname")
port := flag.Int("port", api.DefaultPost, "Port number")
clientCount := flag.String("clientCount", "[1]", "Number of clients to run")
tls := flag.Bool("tls", false, "Use TLS")
clusterModeEnabled := flag.Bool("clusterModeEnabled", false, "Is cluster mode enabled")
Expand Down
4 changes: 2 additions & 2 deletions go/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// TODO: Update the file based on the template used in other clients.
func main() {
host := "localhost"
port := 6379
host := api.DefaultHost
port := api.DefaultPost

config := api.NewGlideClientConfiguration().
WithAddress(&api.NodeAddress{Host: host, Port: port})
Expand Down
14 changes: 8 additions & 6 deletions go/integTest/request_routing_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package integTest

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/valkey-io/valkey-glide/go/glide/api"
"github.com/valkey-io/valkey-glide/go/glide/api/config"
"github.com/valkey-io/valkey-glide/go/glide/protobuf"
)
Expand Down Expand Up @@ -59,10 +61,10 @@ func TestSlotKeyRoute(t *testing.T) {
}

func TestByAddressRoute(t *testing.T) {
routeConfig := config.NewByAddressRoute("localhost", int32(6739))
routeConfig := config.NewByAddressRoute(api.DefaultHost, int32(api.DefaultPort))
expected := &protobuf.Routes{
Value: &protobuf.Routes_ByAddressRoute{
ByAddressRoute: &protobuf.ByAddressRoute{Host: "localhost", Port: 6739},
ByAddressRoute: &protobuf.ByAddressRoute{Host: api.DefaultHost, Port: api.DefaultPort},
},
}

Expand All @@ -73,10 +75,10 @@ func TestByAddressRoute(t *testing.T) {
}

func TestByAddressRouteWithHost(t *testing.T) {
routeConfig, _ := config.NewByAddressRouteWithHost("localhost:6739")
routeConfig, _ := config.NewByAddressRouteWithHost(fmt.Sprintf("%s:%d", api.DefaultHost, api.DefaultPort))
expected := &protobuf.Routes{
Value: &protobuf.Routes_ByAddressRoute{
ByAddressRoute: &protobuf.ByAddressRoute{Host: "localhost", Port: 6739},
ByAddressRoute: &protobuf.ByAddressRoute{Host: api.DefaultHost, Port: api.DefaultPort},
},
}

Expand All @@ -87,11 +89,11 @@ func TestByAddressRouteWithHost(t *testing.T) {
}

func TestByAddressRoute_MultiplePorts(t *testing.T) {
_, err := config.NewByAddressRouteWithHost("localhost:6739:6740")
_, err := config.NewByAddressRouteWithHost(fmt.Sprintf("%s:%d:%d", api.DefaultHost, api.DefaultPort, api.DefaultPort+1))
assert.NotNil(t, err)
}

func TestByAddressRoute_InvalidHost(t *testing.T) {
_, err := config.NewByAddressRouteWithHost("localhost")
_, err := config.NewByAddressRouteWithHost(api.DefaultHost)
assert.NotNil(t, err)
}

0 comments on commit 0678965

Please sign in to comment.