1
0
Fork 0

Fill our API peers lookups (#110)

master
Philip O'Toole 9 years ago
parent 434116a704
commit c04ff92d7a

@ -298,8 +298,9 @@ func (s *Service) handleStatus(w http.ResponseWriter, r *http.Request) {
}
httpStatus := map[string]interface{}{
"addr": s.Addr().String(),
"auth": prettyEnabled(s.credentialStore != nil),
"addr": s.Addr().String(),
"auth": prettyEnabled(s.credentialStore != nil),
"redirect": s.store.Peer(s.store.Leader()),
}
nodeStatus := map[string]interface{}{

@ -95,13 +95,32 @@ type clusterMeta struct {
APIPeers map[string]string // Map from Raft address to API address
}
// newClusterMeta returns an initialized cluster meta store.
func newClusterMeta() *clusterMeta {
// NewClusterMeta returns an initialized cluster meta store.
func NewClusterMeta() *clusterMeta {
return &clusterMeta{
APIPeers: make(map[string]string),
}
}
func (c *clusterMeta) AddrForPeer(addr string) string {
if api, ok := c.APIPeers[addr]; ok && api != "" {
return api
}
// Go through each entry, and see if any key resolves to addr.
for k, v := range c.APIPeers {
resv, err := net.ResolveTCPAddr("tcp", k)
if err != nil {
continue
}
if resv.String() == addr {
return v
}
}
return ""
}
// DBConfig represents the configuration of the underlying SQLite database.
type DBConfig struct {
DSN string // Any custom DSN
@ -139,7 +158,7 @@ func New(dbConf *DBConfig, dir string, tn Transport) *Store {
raftTransport: tn,
dbConf: dbConf,
dbPath: filepath.Join(dir, sqliteFile),
meta: newClusterMeta(),
meta: NewClusterMeta(),
logger: log.New(os.Stderr, "[store] ", log.LstdFlags),
}
}
@ -256,7 +275,7 @@ func (s *Store) Leader() string {
// Peer returns the API address for the given addr. If there is no peer
// for the address, it returns the empty string.
func (s *Store) Peer(addr string) string {
return s.meta.APIPeers[addr]
return s.meta.AddrForPeer(addr)
}
// APIPeers return the map of Raft addresses to API addresses.

@ -23,6 +23,23 @@ func (m *mockSnapshotSink) Cancel() error {
return nil
}
func Test_ClusterMeta(t *testing.T) {
c := NewClusterMeta()
c.APIPeers["localhost:4002"] = "localhost:4001"
if c.AddrForPeer("localhost:4002") != "localhost:4001" {
t.Fatalf("wrong address returned for localhost:4002")
}
if c.AddrForPeer("127.0.0.1:4002") != "localhost:4001" {
t.Fatalf("wrong address returned for 127.0.0.1:4002")
}
if c.AddrForPeer("127.0.0.1:4004") != "" {
t.Fatalf("wrong address returned for 127.0.0.1:4003")
}
}
func Test_OpenStoreSingleNode(t *testing.T) {
s := mustNewStore(true)
defer os.RemoveAll(s.Path())

Loading…
Cancel
Save