Merge pull request #394 from go-redis/fix/rm-errors-pack

Remove internal errors package that clashes with std lib.
This commit is contained in:
Vladimir Mihailenco 2016-10-09 14:50:36 +03:00 committed by GitHub
commit cb3f27133f
9 changed files with 34 additions and 41 deletions

View File

@ -7,12 +7,11 @@ import (
"time" "time"
"gopkg.in/redis.v5/internal" "gopkg.in/redis.v5/internal"
"gopkg.in/redis.v5/internal/errors"
"gopkg.in/redis.v5/internal/hashtag" "gopkg.in/redis.v5/internal/hashtag"
"gopkg.in/redis.v5/internal/pool" "gopkg.in/redis.v5/internal/pool"
) )
var errClusterNoNodes = errors.RedisError("redis: cluster has no nodes") var errClusterNoNodes = internal.RedisError("redis: cluster has no nodes")
// ClusterOptions are used to configure a cluster client and should be // ClusterOptions are used to configure a cluster client and should be
// passed to NewClusterClient. // passed to NewClusterClient.
@ -382,20 +381,20 @@ func (c *ClusterClient) Process(cmd Cmder) error {
} }
// If slave is loading - read from master. // If slave is loading - read from master.
if c.opt.ReadOnly && errors.IsLoading(err) { if c.opt.ReadOnly && internal.IsLoadingError(err) {
node.loading = time.Now() node.loading = time.Now()
continue continue
} }
// On network errors try random node. // On network errors try random node.
if errors.IsRetryable(err) { if internal.IsRetryableError(err) {
node, err = c.randomNode() node, err = c.randomNode()
continue continue
} }
var moved bool var moved bool
var addr string var addr string
moved, ask, addr = errors.IsMoved(err) moved, ask, addr = internal.IsMovedError(err)
if moved || ask { if moved || ask {
if slot >= 0 { if slot >= 0 {
master, _ := c.slotMasterNode(slot) master, _ := c.slotMasterNode(slot)
@ -650,11 +649,11 @@ func (c *ClusterClient) execClusterCmds(
if err == nil { if err == nil {
continue continue
} }
if errors.IsNetwork(err) { if internal.IsNetworkError(err) {
cmd.reset() cmd.reset()
failedCmds[nil] = append(failedCmds[nil], cmds[i:]...) failedCmds[nil] = append(failedCmds[nil], cmds[i:]...)
break break
} else if moved, ask, addr := errors.IsMoved(err); moved { } else if moved, ask, addr := internal.IsMovedError(err); moved {
c.lazyReloadSlots() c.lazyReloadSlots()
cmd.reset() cmd.reset()
node, err := c.nodeByAddr(addr) node, err := c.nodeByAddr(addr)

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"gopkg.in/redis.v5/internal" "gopkg.in/redis.v5/internal"
"gopkg.in/redis.v5/internal/errors"
) )
func readTimeout(timeout time.Duration) time.Duration { func readTimeout(timeout time.Duration) time.Duration {
@ -1710,7 +1709,7 @@ func (c *cmdable) shutdown(modifier string) *StatusCmd {
} }
} else { } else {
// Server did not quit. String reply contains the reason. // Server did not quit. String reply contains the reason.
cmd.err = errors.RedisError(cmd.val) cmd.err = internal.RedisError(cmd.val)
cmd.val = "" cmd.val = ""
} }
return cmd return cmd

View File

@ -1,4 +1,4 @@
package errors package internal
import ( import (
"io" "io"
@ -12,16 +12,16 @@ type RedisError string
func (e RedisError) Error() string { return string(e) } func (e RedisError) Error() string { return string(e) }
func IsRetryable(err error) bool { func IsRetryableError(err error) bool {
return IsNetwork(err) return IsNetworkError(err)
} }
func IsInternal(err error) bool { func IsInternalError(err error) bool {
_, ok := err.(RedisError) _, ok := err.(RedisError)
return ok return ok
} }
func IsNetwork(err error) bool { func IsNetworkError(err error) bool {
if err == io.EOF { if err == io.EOF {
return true return true
} }
@ -33,7 +33,7 @@ func IsBadConn(err error, allowTimeout bool) bool {
if err == nil { if err == nil {
return false return false
} }
if IsInternal(err) { if IsInternalError(err) {
return false return false
} }
if allowTimeout { if allowTimeout {
@ -44,8 +44,8 @@ func IsBadConn(err error, allowTimeout bool) bool {
return true return true
} }
func IsMoved(err error) (moved bool, ask bool, addr string) { func IsMovedError(err error) (moved bool, ask bool, addr string) {
if !IsInternal(err) { if !IsInternalError(err) {
return return
} }
@ -66,6 +66,6 @@ func IsMoved(err error) (moved bool, ask bool, addr string) {
return return
} }
func IsLoading(err error) bool { func IsLoadingError(err error) bool {
return strings.HasPrefix(err.Error(), "LOADING") return strings.HasPrefix(err.Error(), "LOADING")
} }

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"gopkg.in/redis.v5/internal/errors" "gopkg.in/redis.v5/internal"
) )
const ( const (
@ -18,7 +18,7 @@ const (
const defaultBufSize = 4096 const defaultBufSize = 4096
var errScanNil = errors.RedisError("redis: Scan(nil)") const errScanNil = internal.RedisError("redis: Scan(nil)")
func Scan(b []byte, val interface{}) error { func Scan(b []byte, val interface{}) error {
switch v := val.(type) { switch v := val.(type) {

View File

@ -2,17 +2,16 @@ package proto
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
ierrors "gopkg.in/redis.v5/internal/errors" "gopkg.in/redis.v5/internal"
) )
type MultiBulkParse func(*Reader, int64) (interface{}, error) const errEmptyReply = internal.RedisError("redis: reply is empty")
var errEmptyReply = errors.New("redis: reply is empty") type MultiBulkParse func(*Reader, int64) (interface{}, error)
type Reader struct { type Reader struct {
src *bufio.Reader src *bufio.Reader
@ -59,7 +58,7 @@ func (p *Reader) ReadLine() ([]byte, error) {
return nil, errEmptyReply return nil, errEmptyReply
} }
if isNilReply(line) { if isNilReply(line) {
return nil, ierrors.Nil return nil, internal.Nil
} }
return line, nil return line, nil
} }
@ -209,7 +208,7 @@ func (p *Reader) ReadScanReply() ([]string, uint64, error) {
func (p *Reader) parseBytesValue(line []byte) ([]byte, error) { func (p *Reader) parseBytesValue(line []byte) ([]byte, error) {
if isNilReply(line) { if isNilReply(line) {
return nil, ierrors.Nil return nil, internal.Nil
} }
replyLen, err := strconv.Atoi(string(line[1:])) replyLen, err := strconv.Atoi(string(line[1:]))
@ -245,7 +244,7 @@ func isNilReply(b []byte) bool {
} }
func parseErrorValue(line []byte) error { func parseErrorValue(line []byte) error {
return ierrors.RedisError(string(line[1:])) return internal.RedisError(string(line[1:]))
} }
func parseStatusValue(line []byte) ([]byte, error) { func parseStatusValue(line []byte) ([]byte, error) {
@ -258,7 +257,7 @@ func parseIntValue(line []byte) (int64, error) {
func parseArrayLen(line []byte) (int64, error) { func parseArrayLen(line []byte) (int64, error) {
if isNilReply(line) { if isNilReply(line) {
return 0, ierrors.Nil return 0, internal.Nil
} }
return parseIntValue(line) return parseIntValue(line)
} }

View File

@ -4,7 +4,7 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"gopkg.in/redis.v5/internal/errors" "gopkg.in/redis.v5/internal"
"gopkg.in/redis.v5/internal/pool" "gopkg.in/redis.v5/internal/pool"
) )
@ -100,7 +100,7 @@ func execCmds(cn *pool.Conn, cmds []Cmder) ([]Cmder, error) {
if firstCmdErr == nil { if firstCmdErr == nil {
firstCmdErr = err firstCmdErr = err
} }
if errors.IsRetryable(err) { if internal.IsRetryableError(err) {
failedCmds = append(failedCmds, cmd) failedCmds = append(failedCmds, cmd)
} }
} }

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"gopkg.in/redis.v5/internal" "gopkg.in/redis.v5/internal"
"gopkg.in/redis.v5/internal/errors"
"gopkg.in/redis.v5/internal/pool" "gopkg.in/redis.v5/internal/pool"
) )
@ -212,7 +211,7 @@ func (c *PubSub) receiveMessage(timeout time.Duration) (*Message, error) {
for { for {
msgi, err := c.ReceiveTimeout(timeout) msgi, err := c.ReceiveTimeout(timeout)
if err != nil { if err != nil {
if !errors.IsNetwork(err) { if !internal.IsNetworkError(err) {
return nil, err return nil, err
} }

View File

@ -5,12 +5,11 @@ import (
"log" "log"
"gopkg.in/redis.v5/internal" "gopkg.in/redis.v5/internal"
"gopkg.in/redis.v5/internal/errors"
"gopkg.in/redis.v5/internal/pool" "gopkg.in/redis.v5/internal/pool"
) )
// Redis nil reply, .e.g. when key does not exist. // Redis nil reply, .e.g. when key does not exist.
const Nil = errors.Nil const Nil = internal.Nil
func SetLogger(logger *log.Logger) { func SetLogger(logger *log.Logger) {
internal.Logger = logger internal.Logger = logger
@ -42,7 +41,7 @@ func (c *baseClient) conn() (*pool.Conn, bool, error) {
} }
func (c *baseClient) putConn(cn *pool.Conn, err error, allowTimeout bool) bool { func (c *baseClient) putConn(cn *pool.Conn, err error, allowTimeout bool) bool {
if errors.IsBadConn(err, allowTimeout) { if internal.IsBadConn(err, allowTimeout) {
_ = c.connPool.Remove(cn, err) _ = c.connPool.Remove(cn, err)
return false return false
} }
@ -101,7 +100,7 @@ func (c *baseClient) Process(cmd Cmder) error {
if err := writeCmd(cn, cmd); err != nil { if err := writeCmd(cn, cmd); err != nil {
c.putConn(cn, err, false) c.putConn(cn, err, false)
cmd.setErr(err) cmd.setErr(err)
if err != nil && errors.IsRetryable(err) { if err != nil && internal.IsRetryableError(err) {
continue continue
} }
return err return err
@ -109,7 +108,7 @@ func (c *baseClient) Process(cmd Cmder) error {
err = cmd.readReply(cn) err = cmd.readReply(cn)
c.putConn(cn, err, readTimeout != nil) c.putConn(cn, err, readTimeout != nil)
if err != nil && errors.IsRetryable(err) { if err != nil && internal.IsRetryableError(err) {
continue continue
} }

6
tx.go
View File

@ -1,19 +1,17 @@
package redis package redis
import ( import (
"errors"
"fmt" "fmt"
"gopkg.in/redis.v5/internal" "gopkg.in/redis.v5/internal"
ierrors "gopkg.in/redis.v5/internal/errors"
"gopkg.in/redis.v5/internal/pool" "gopkg.in/redis.v5/internal/pool"
"gopkg.in/redis.v5/internal/proto" "gopkg.in/redis.v5/internal/proto"
) )
// Redis transaction failed. // Redis transaction failed.
const TxFailedErr = ierrors.RedisError("redis: transaction failed") const TxFailedErr = internal.RedisError("redis: transaction failed")
var errDiscard = errors.New("redis: Discard can be used only inside Exec") var errDiscard = internal.RedisError("redis: Discard can be used only inside Exec")
// Tx implements Redis transactions as described in // Tx implements Redis transactions as described in
// http://redis.io/topics/transactions. It's NOT safe for concurrent use // http://redis.io/topics/transactions. It's NOT safe for concurrent use