Skip to content

Commit

Permalink
Return error in New() if server is not reachable
Browse files Browse the repository at this point in the history
Return ss.SetServers()'s error to make debugging easier.

Updates bradfitz#83

Closes bradfitz#63

Signed-off-by: Mikel Olasagasti Uranga <[email protected]>
  • Loading branch information
mikelolasagasti committed Mar 14, 2024
1 parent 24af94b commit 400c7dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func main() {
mc := memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")
mc, err := memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")
mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value")})

it, err := mc.Get("foo")
Expand Down
9 changes: 6 additions & 3 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ var (
// New returns a memcache client using the provided server(s)
// with equal weight. If a server is listed multiple times,
// it gets a proportional amount of weight.
func New(server ...string) *Client {
// Error is returned if any of the server names fail to resolve.
// No attempt is made to connect to the server. If any error
// is returned, no changes are made to the ServerList.
func New(server ...string) (*Client, error) {
ss := new(ServerList)
ss.SetServers(server...)
return NewFromSelector(ss)
err := ss.SetServers(server...)
return NewFromSelector(ss), err
}

// NewFromSelector returns a new Client using the provided ServerSelector.
Expand Down
24 changes: 17 additions & 7 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ func TestLocalhost(t *testing.T) {
}
io.WriteString(c, "flush_all\r\n")
c.Close()

testWithClient(t, New(localhostTCPAddr))
client, err := New(localhostTCPAddr)
if err != nil {
t.Fatalf("error creating client: %v", err)
}
testWithClient(t, client)
}

// Run the memcached binary as a child process and connect to its unix socket.
Expand All @@ -71,8 +74,11 @@ func TestUnixSocket(t *testing.T) {
}
time.Sleep(time.Duration(25*i) * time.Millisecond)
}

testWithClient(t, New(sock))
client, err := New(sock)
if err != nil {
t.Fatalf("error creating client: %v", err)
}
testWithClient(t, client)
}

func TestFakeServer(t *testing.T) {
Expand All @@ -86,7 +92,11 @@ func TestFakeServer(t *testing.T) {
srv := &testServer{}
go srv.Serve(ln)

testWithClient(t, New(ln.Addr().String()))
client, err := New(ln.Addr().String())
if err != nil {
t.Fatalf("error creating client: %v", err)
}
testWithClient(t, client)
}

func TestTLS(t *testing.T) {
Expand Down Expand Up @@ -148,7 +158,7 @@ func TestTLS(t *testing.T) {
time.Sleep(time.Duration(25*i) * time.Millisecond)
}

c := New(net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
c, err := New(net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
c.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
var td tls.Dialer
td.Config = &tls.Config{
Expand Down Expand Up @@ -412,7 +422,7 @@ func BenchmarkOnItem(b *testing.B) {
}()

addr := fakeServer.Addr()
c := New(addr.String())
c, err := New(addr.String())
if _, err := c.getConn(addr); err != nil {
b.Fatal("failed to initialize connection to fake server")
}
Expand Down

0 comments on commit 400c7dc

Please sign in to comment.