Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"google.golang.org/protobuf/proto"
)



const (
// RaftRPC is the first byte sent on a connection to identify it as a Raft command
// for the connection multiplexer.
Expand All @@ -35,6 +33,11 @@ const (
lenWidth = 8
)

var (
ErrLeaderTimeout = errors.New("timed out waiting for leader")
ErrNotRaftRPC = errors.New("not a raft rpc connection")
)

// RequestType identifies the type of operation being applied to the Raft log.
type RequestType uint8

Expand Down Expand Up @@ -219,7 +222,7 @@ func (kv *KV) WaitForLeader(timeout time.Duration) error {
for {
select {
case <-timeoutc:
return errors.New("timed out")
return ErrLeaderTimeout
case <-ticker.C:
if l := kv.raft.Leader(); l != "" {
return nil
Expand Down Expand Up @@ -498,7 +501,7 @@ func (s *StreamLayer) Accept() (net.Conn, error) {
}
if !bytes.Equal([]byte{byte(RaftRPC)}, b) {
conn.Close()
return nil, errors.New("not a raft rpc connection")
return nil, ErrNotRaftRPC
}
return conn, nil
}
Expand Down
9 changes: 7 additions & 2 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"sync"
)

var (
ErrNotFound = errors.New("item not found")
ErrNotBytes = errors.New("value is not of type []byte")
)

// Store is a concurrent, in-memory implementation of Storer using a sync.Map.
type Store struct {
m sync.Map
Expand All @@ -20,12 +25,12 @@ func New() *Store {
func (s *Store) Get(key string) ([]byte, error) {
value, ok := s.m.Load(key)
if !ok {
return nil, errors.New("not found")
return nil, ErrNotFound
}

bytes, ok := value.([]byte)
if !ok {
return nil, errors.New("value is not of type []byte")
return nil, ErrNotBytes
}
return bytes, nil
}
Expand Down