Browse Source

Merge internal package into the packages that use it

Gary Burd 7 years ago
parent
commit
b9037db4b8

+ 55 - 0
redis/commandinfo.go

@@ -0,0 +1,55 @@
+// Copyright 2014 Gary Burd
+//
+// Licensed under the Apache License, Version 2.0 (the "License"): you may
+// not use this file except in compliance with the License. You may obtain
+// a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
+// under the License.
+
+package redis
+
+import (
+	"strings"
+)
+
+const (
+	connectionWatchState = 1 << iota
+	connectionMultiState
+	connectionSubscribeState
+	connectionMonitorState
+)
+
+type commandInfo struct {
+	// Set or Clear these states on connection.
+	Set, Clear int
+}
+
+var commandInfos = map[string]commandInfo{
+	"WATCH":      {Set: connectionWatchState},
+	"UNWATCH":    {Clear: connectionWatchState},
+	"MULTI":      {Set: connectionMultiState},
+	"EXEC":       {Clear: connectionWatchState | connectionMultiState},
+	"DISCARD":    {Clear: connectionWatchState | connectionMultiState},
+	"PSUBSCRIBE": {Set: connectionSubscribeState},
+	"SUBSCRIBE":  {Set: connectionSubscribeState},
+	"MONITOR":    {Set: connectionMonitorState},
+}
+
+func init() {
+	for n, ci := range commandInfos {
+		commandInfos[strings.ToLower(n)] = ci
+	}
+}
+
+func lookupCommandInfo(commandName string) commandInfo {
+	if ci, ok := commandInfos[commandName]; ok {
+		return ci
+	}
+	return commandInfos[strings.ToUpper(commandName)]
+}

+ 3 - 3
internal/commandinfo_test.go → redis/commandinfo_test.go

@@ -1,10 +1,10 @@
-package internal
+package redis
 
 
 import "testing"
 import "testing"
 
 
 func TestLookupCommandInfo(t *testing.T) {
 func TestLookupCommandInfo(t *testing.T) {
 	for _, n := range []string{"watch", "WATCH", "wAtch"} {
 	for _, n := range []string{"watch", "WATCH", "wAtch"} {
-		if LookupCommandInfo(n) == (CommandInfo{}) {
+		if lookupCommandInfo(n) == (commandInfo{}) {
 			t.Errorf("LookupCommandInfo(%q) = CommandInfo{}, expected non-zero value", n)
 			t.Errorf("LookupCommandInfo(%q) = CommandInfo{}, expected non-zero value", n)
 		}
 		}
 	}
 	}
@@ -13,7 +13,7 @@ func TestLookupCommandInfo(t *testing.T) {
 func benchmarkLookupCommandInfo(b *testing.B, names ...string) {
 func benchmarkLookupCommandInfo(b *testing.B, names ...string) {
 	for i := 0; i < b.N; i++ {
 	for i := 0; i < b.N; i++ {
 		for _, c := range names {
 		for _, c := range names {
-			LookupCommandInfo(c)
+			lookupCommandInfo(c)
 		}
 		}
 	}
 	}
 }
 }

+ 1 - 1
redis/doc.go

@@ -174,4 +174,4 @@
 // non-recoverable error such as a network error or protocol parsing error. If
 // non-recoverable error such as a network error or protocol parsing error. If
 // Err() returns a non-nil value, then the connection is not usable and should
 // Err() returns a non-nil value, then the connection is not usable and should
 // be closed.
 // be closed.
-package redis // import "github.com/gomodule/redigo/redis"
+package redis

+ 9 - 11
redis/pool.go

@@ -24,8 +24,6 @@ import (
 	"sync"
 	"sync"
 	"sync/atomic"
 	"sync/atomic"
 	"time"
 	"time"
-
-	"github.com/gomodule/redigo/internal"
 )
 )
 
 
 var (
 var (
@@ -398,14 +396,14 @@ func (ac *activeConn) Close() error {
 	}
 	}
 	ac.pc = nil
 	ac.pc = nil
 
 
-	if ac.state&internal.MultiState != 0 {
+	if ac.state&connectionMultiState != 0 {
 		pc.c.Send("DISCARD")
 		pc.c.Send("DISCARD")
-		ac.state &^= (internal.MultiState | internal.WatchState)
-	} else if ac.state&internal.WatchState != 0 {
+		ac.state &^= (connectionMultiState | connectionWatchState)
+	} else if ac.state&connectionWatchState != 0 {
 		pc.c.Send("UNWATCH")
 		pc.c.Send("UNWATCH")
-		ac.state &^= internal.WatchState
+		ac.state &^= connectionWatchState
 	}
 	}
-	if ac.state&internal.SubscribeState != 0 {
+	if ac.state&connectionSubscribeState != 0 {
 		pc.c.Send("UNSUBSCRIBE")
 		pc.c.Send("UNSUBSCRIBE")
 		pc.c.Send("PUNSUBSCRIBE")
 		pc.c.Send("PUNSUBSCRIBE")
 		// To detect the end of the message stream, ask the server to echo
 		// To detect the end of the message stream, ask the server to echo
@@ -419,7 +417,7 @@ func (ac *activeConn) Close() error {
 				break
 				break
 			}
 			}
 			if p, ok := p.([]byte); ok && bytes.Equal(p, sentinel) {
 			if p, ok := p.([]byte); ok && bytes.Equal(p, sentinel) {
-				ac.state &^= internal.SubscribeState
+				ac.state &^= connectionSubscribeState
 				break
 				break
 			}
 			}
 		}
 		}
@@ -442,7 +440,7 @@ func (ac *activeConn) Do(commandName string, args ...interface{}) (reply interfa
 	if pc == nil {
 	if pc == nil {
 		return nil, errConnClosed
 		return nil, errConnClosed
 	}
 	}
-	ci := internal.LookupCommandInfo(commandName)
+	ci := lookupCommandInfo(commandName)
 	ac.state = (ac.state | ci.Set) &^ ci.Clear
 	ac.state = (ac.state | ci.Set) &^ ci.Clear
 	return pc.c.Do(commandName, args...)
 	return pc.c.Do(commandName, args...)
 }
 }
@@ -456,7 +454,7 @@ func (ac *activeConn) DoWithTimeout(timeout time.Duration, commandName string, a
 	if !ok {
 	if !ok {
 		return nil, errTimeoutNotSupported
 		return nil, errTimeoutNotSupported
 	}
 	}
-	ci := internal.LookupCommandInfo(commandName)
+	ci := lookupCommandInfo(commandName)
 	ac.state = (ac.state | ci.Set) &^ ci.Clear
 	ac.state = (ac.state | ci.Set) &^ ci.Clear
 	return cwt.DoWithTimeout(timeout, commandName, args...)
 	return cwt.DoWithTimeout(timeout, commandName, args...)
 }
 }
@@ -466,7 +464,7 @@ func (ac *activeConn) Send(commandName string, args ...interface{}) error {
 	if pc == nil {
 	if pc == nil {
 		return errConnClosed
 		return errConnClosed
 	}
 	}
-	ci := internal.LookupCommandInfo(commandName)
+	ci := lookupCommandInfo(commandName)
 	ac.state = (ac.state | ci.Set) &^ ci.Clear
 	ac.state = (ac.state | ci.Set) &^ ci.Clear
 	return pc.c.Send(commandName, args...)
 	return pc.c.Send(commandName, args...)
 }
 }

+ 17 - 17
internal/commandinfo.go → redisx/commandinfo.go

@@ -12,32 +12,32 @@
 // License for the specific language governing permissions and limitations
 // License for the specific language governing permissions and limitations
 // under the License.
 // under the License.
 
 
-package internal // import "github.com/gomodule/redigo/internal"
+package redisx
 
 
 import (
 import (
 	"strings"
 	"strings"
 )
 )
 
 
 const (
 const (
-	WatchState = 1 << iota
-	MultiState
-	SubscribeState
-	MonitorState
+	connectionWatchState = 1 << iota
+	connectionMultiState
+	connectionSubscribeState
+	connectionMonitorState
 )
 )
 
 
-type CommandInfo struct {
-	Set, Clear int
+type commandInfo struct {
+	notMuxable bool
 }
 }
 
 
-var commandInfos = map[string]CommandInfo{
-	"WATCH":      {Set: WatchState},
-	"UNWATCH":    {Clear: WatchState},
-	"MULTI":      {Set: MultiState},
-	"EXEC":       {Clear: WatchState | MultiState},
-	"DISCARD":    {Clear: WatchState | MultiState},
-	"PSUBSCRIBE": {Set: SubscribeState},
-	"SUBSCRIBE":  {Set: SubscribeState},
-	"MONITOR":    {Set: MonitorState},
+var commandInfos = map[string]commandInfo{
+	"WATCH":      {notMuxable: true},
+	"UNWATCH":    {notMuxable: true},
+	"MULTI":      {notMuxable: true},
+	"EXEC":       {notMuxable: true},
+	"DISCARD":    {notMuxable: true},
+	"PSUBSCRIBE": {notMuxable: true},
+	"SUBSCRIBE":  {notMuxable: true},
+	"MONITOR":    {notMuxable: true},
 }
 }
 
 
 func init() {
 func init() {
@@ -46,7 +46,7 @@ func init() {
 	}
 	}
 }
 }
 
 
-func LookupCommandInfo(commandName string) CommandInfo {
+func lookupCommandInfo(commandName string) commandInfo {
 	if ci, ok := commandInfos[commandName]; ok {
 	if ci, ok := commandInfos[commandName]; ok {
 		return ci
 		return ci
 	}
 	}

+ 11 - 0
redisx/commandinfo_test.go

@@ -0,0 +1,11 @@
+package redisx
+
+import "testing"
+
+func TestLookupCommandInfo(t *testing.T) {
+	for _, n := range []string{"watch", "WATCH", "wAtch"} {
+		if lookupCommandInfo(n) == (commandInfo{}) {
+			t.Errorf("LookupCommandInfo(%q) = CommandInfo{}, expected non-zero value", n)
+		}
+	}
+}

+ 1 - 2
redisx/connmux.go

@@ -18,7 +18,6 @@ import (
 	"errors"
 	"errors"
 	"sync"
 	"sync"
 
 
-	"github.com/gomodule/redigo/internal"
 	"github.com/gomodule/redigo/redis"
 	"github.com/gomodule/redigo/redis"
 )
 )
 
 
@@ -60,7 +59,7 @@ type muxConn struct {
 }
 }
 
 
 func (c *muxConn) send(flush bool, cmd string, args ...interface{}) error {
 func (c *muxConn) send(flush bool, cmd string, args ...interface{}) error {
-	if internal.LookupCommandInfo(cmd).Set != 0 {
+	if lookupCommandInfo(cmd).notMuxable {
 		return errors.New("command not supported by mux pool")
 		return errors.New("command not supported by mux pool")
 	}
 	}
 	p := c.p
 	p := c.p

+ 8 - 9
redisx/connmux_test.go

@@ -19,13 +19,12 @@ import (
 	"sync"
 	"sync"
 	"testing"
 	"testing"
 
 
-	"github.com/gomodule/redigo/internal/redistest"
 	"github.com/gomodule/redigo/redis"
 	"github.com/gomodule/redigo/redis"
 	"github.com/gomodule/redigo/redisx"
 	"github.com/gomodule/redigo/redisx"
 )
 )
 
 
 func TestConnMux(t *testing.T) {
 func TestConnMux(t *testing.T) {
-	c, err := redistest.Dial()
+	c, err := redisx.DialTest()
 	if err != nil {
 	if err != nil {
 		t.Fatalf("error connection to database, %v", err)
 		t.Fatalf("error connection to database, %v", err)
 	}
 	}
@@ -57,7 +56,7 @@ func TestConnMux(t *testing.T) {
 }
 }
 
 
 func TestConnMuxClose(t *testing.T) {
 func TestConnMuxClose(t *testing.T) {
-	c, err := redistest.Dial()
+	c, err := redisx.DialTest()
 	if err != nil {
 	if err != nil {
 		t.Fatalf("error connection to database, %v", err)
 		t.Fatalf("error connection to database, %v", err)
 	}
 	}
@@ -93,7 +92,7 @@ func TestConnMuxClose(t *testing.T) {
 
 
 func BenchmarkConn(b *testing.B) {
 func BenchmarkConn(b *testing.B) {
 	b.StopTimer()
 	b.StopTimer()
-	c, err := redistest.Dial()
+	c, err := redisx.DialTest()
 	if err != nil {
 	if err != nil {
 		b.Fatalf("error connection to database, %v", err)
 		b.Fatalf("error connection to database, %v", err)
 	}
 	}
@@ -109,7 +108,7 @@ func BenchmarkConn(b *testing.B) {
 
 
 func BenchmarkConnMux(b *testing.B) {
 func BenchmarkConnMux(b *testing.B) {
 	b.StopTimer()
 	b.StopTimer()
-	c, err := redistest.Dial()
+	c, err := redisx.DialTest()
 	if err != nil {
 	if err != nil {
 		b.Fatalf("error connection to database, %v", err)
 		b.Fatalf("error connection to database, %v", err)
 	}
 	}
@@ -130,7 +129,7 @@ func BenchmarkConnMux(b *testing.B) {
 func BenchmarkPool(b *testing.B) {
 func BenchmarkPool(b *testing.B) {
 	b.StopTimer()
 	b.StopTimer()
 
 
-	p := redis.Pool{Dial: redistest.Dial, MaxIdle: 1}
+	p := redis.Pool{Dial: redisx.DialTest, MaxIdle: 1}
 	defer p.Close()
 	defer p.Close()
 
 
 	// Fill the pool.
 	// Fill the pool.
@@ -155,7 +154,7 @@ const numConcurrent = 10
 
 
 func BenchmarkConnMuxConcurrent(b *testing.B) {
 func BenchmarkConnMuxConcurrent(b *testing.B) {
 	b.StopTimer()
 	b.StopTimer()
-	c, err := redistest.Dial()
+	c, err := redisx.DialTest()
 	if err != nil {
 	if err != nil {
 		b.Fatalf("error connection to database, %v", err)
 		b.Fatalf("error connection to database, %v", err)
 	}
 	}
@@ -186,7 +185,7 @@ func BenchmarkConnMuxConcurrent(b *testing.B) {
 func BenchmarkPoolConcurrent(b *testing.B) {
 func BenchmarkPoolConcurrent(b *testing.B) {
 	b.StopTimer()
 	b.StopTimer()
 
 
-	p := redis.Pool{Dial: redistest.Dial, MaxIdle: numConcurrent}
+	p := redis.Pool{Dial: redisx.DialTest, MaxIdle: numConcurrent}
 	defer p.Close()
 	defer p.Close()
 
 
 	// Fill the pool.
 	// Fill the pool.
@@ -224,7 +223,7 @@ func BenchmarkPoolConcurrent(b *testing.B) {
 
 
 func BenchmarkPipelineConcurrency(b *testing.B) {
 func BenchmarkPipelineConcurrency(b *testing.B) {
 	b.StopTimer()
 	b.StopTimer()
-	c, err := redistest.Dial()
+	c, err := redisx.DialTest()
 	if err != nil {
 	if err != nil {
 		b.Fatalf("error connection to database, %v", err)
 		b.Fatalf("error connection to database, %v", err)
 	}
 	}

+ 2 - 2
internal/redistest/testdb.go → redisx/db_test.go

@@ -13,7 +13,7 @@
 // under the License.
 // under the License.
 
 
 // Package redistest contains utilities for writing Redigo tests.
 // Package redistest contains utilities for writing Redigo tests.
-package redistest
+package redisx
 
 
 import (
 import (
 	"errors"
 	"errors"
@@ -41,7 +41,7 @@ func (t testConn) Close() error {
 // Dial dials the local Redis server and selects database 9. To prevent
 // Dial dials the local Redis server and selects database 9. To prevent
 // stomping on real data, DialTestDB fails if database 9 contains data. The
 // stomping on real data, DialTestDB fails if database 9 contains data. The
 // returned connection flushes database 9 on close.
 // returned connection flushes database 9 on close.
-func Dial() (redis.Conn, error) {
+func DialTest() (redis.Conn, error) {
 	c, err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second)
 	c, err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err

+ 1 - 1
redisx/doc.go

@@ -14,4 +14,4 @@
 
 
 // Package redisx contains experimental features for Redigo. Features in this
 // Package redisx contains experimental features for Redigo. Features in this
 // package may be modified or deleted at any time.
 // package may be modified or deleted at any time.
-package redisx // import "github.com/gomodule/redigo/redisx"
+package redisx