Skip to content

Commit

Permalink
minor bug fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Aug 2, 2018
1 parent 88c9186 commit a7b2fa9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
21 changes: 11 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ func (c *Config) Update(o *Config) error {

// GetName returns the name of the local host defined by the configuration or
// using the hostname by default.
func (c *Config) GetName() (string, error) {
func (c *Config) GetName() (name string, err error) {
if c.Name == "" {
hostname, err := os.Hostname()
if err != nil {
return "", errors.New("could not look up hostname to find peer")
if name, err = os.Hostname(); err != nil {
return "", errors.New("could not find unique name of localhost")
}
return hostname, nil
return name, nil
}

return c.Name, nil
Expand All @@ -131,22 +130,24 @@ func (c *Config) GetPeer() (peers.Peer, error) {
}
}

return peers.Peer{}, fmt.Errorf("could not find peer for '%s'", c.Name)
return peers.Peer{}, fmt.Errorf("could not find peer for '%s'", local)
}

// GetRemotes returns all peer configurations for remote hosts on the network,
// e.g. by excluding the local peer configuration.
func (c *Config) GetRemotes() ([]peers.Peer, error) {
local, err := c.GetName()
if err != nil {
func (c *Config) GetRemotes() (remotes []peers.Peer, err error) {
var local string
if local, err = c.GetName(); err != nil {
return nil, err
}
remotes := make([]peers.Peer, 0, len(c.Peers)-1)

remotes = make([]peers.Peer, 0, len(c.Peers)-1)

for _, peer := range c.Peers {
if local == peer.Name {
continue
}
remotes = append(remotes, peer)
}

return remotes, nil
Expand Down
9 changes: 6 additions & 3 deletions example/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
{
"pid": 1,
"name": "alpha",
"addr": "localhost",
"ip_address": "127.0.0.1",
"domain": "localhost",
"port": 3264
},
{
"pid": 2,
"name": "bravo",
"addr": "localhost",
"ip_address": "127.0.0.1",
"domain": "localhost",
"port": 3265
},
{
"pid": 3,
"name": "charlie",
"addr": "localhost",
"ip_address": "127.0.0.1",
"domain": "localhost",
"port": 3266
}
]
Expand Down
2 changes: 1 addition & 1 deletion example/raft.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
export RAFT="../cmd/raft/main.go"
export SERVE="go run $RAFT serve -c config.json"
export COMMIT="go run $RAFT commit -c config.json -k foo -v $(ts)"
export BENCH="go run $RAFT bench -c config.json -n 1 -r 5000"
export BENCH="go run $RAFT bench -c config.json -n 5 -r 5000"
9 changes: 6 additions & 3 deletions fixtures/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
{
"pid": 1,
"name": "alpha",
"addr": "localhost",
"ip_address": "127.0.0.1",
"domain": "localhost",
"port": 3264
},
{
"pid": 2,
"name": "bravo",
"addr": "localhost",
"ip_address": "127.0.0.1",
"domain": "localhost",
"port": 3265
},
{
"pid": 3,
"name": "charlie",
"addr": "localhost",
"ip_address": "127.0.0.1",
"domain": "localhost",
"port": 3266
}
]
Expand Down
2 changes: 1 addition & 1 deletion remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *Remote) AppendEntries(leader string, term uint64, log *Log) error {
// Connect to the remote using the specified timeout. Connect is usually not
// explicitly called, but is instead connected when a message is sent.
func (c *Remote) Connect() (err error) {
addr := c.Endpoint(false)
addr := c.Endpoint(true)

if c.conn, err = grpc.Dial(addr, grpc.WithInsecure(), grpc.WithTimeout(c.timeout)); err != nil {
return fmt.Errorf("could not connect to '%s': %s", addr, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (r *Replica) AppendEntries(stream pb.Raft_AppendEntriesServer) (err error)
source: source,
value: in,
}
if err := r.Dispatch(event); err != nil {
if err = r.Dispatch(event); err != nil {
return err
}

Expand Down

0 comments on commit a7b2fa9

Please sign in to comment.