简体   繁体   中英

Memcached Ping() doesn't return an error on an invalid server

I use memcache for caching and the client I use is https://github.com/bradfitz/gomemcache . When I tried initiate new client with dummy/invalid server address and then pinging to it, it return no error.

package main

import (
    "fmt"
    m "github.com/bradfitz/gomemcache"
)

func main() {
    o := m.New("dummy_adress")
    fmt.Println(o.Ping()) // return no error
}

I think it suppose to return error as the server is invalid. What do I miss?

It looks like the New() call ignores the return value for SetServers :

func New(server ...string) *Client {
    ss := new(ServerList)
    ss.SetServers(server...)
    return NewFromSelector(ss)
}

The SetServers() function will only set the server list to valid servers (in your case: no servers) and the Ping() funtion will only ping servers that are set, and since there are no servers set it doesn't really do anything.

This is arguably a feature; if you have 4 servers and one is down then that's not really an issue. Even with just 1 server memcache is generally optional.

You can duplicate the New() logic with an error check:

ss := new(memcache.ServerList)
err := ss.SetServers("example.localhost:11211")
if err != nil {
    panic(err)
}
c := memcache.NewFromSelector(ss)

err = c.Ping()
if err != nil {
    panic(err)
}

Which gives:

panic: dial tcp 127.0.0.1:11211: connect: connection refused

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM