diff --git a/kv/kv.go b/kv/kv.go index f2468ba..ecd06c4 100644 --- a/kv/kv.go +++ b/kv/kv.go @@ -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. @@ -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 @@ -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 @@ -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 } diff --git a/store/store.go b/store/store.go index fc0967d..e9bfe2a 100644 --- a/store/store.go +++ b/store/store.go @@ -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 @@ -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 }