Skip to content

Commit

Permalink
Merge internal package into the packages that use it
Browse files Browse the repository at this point in the history
  • Loading branch information
garyburd committed Oct 12, 2018
1 parent 2cd21d9 commit b9037db
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 46 deletions.
55 changes: 55 additions & 0 deletions 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)]
}
6 changes: 3 additions & 3 deletions internal/commandinfo_test.go → redis/commandinfo_test.go
@@ -1,10 +1,10 @@
package internal
package redis

import "testing"

func TestLookupCommandInfo(t *testing.T) {
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)
}
}
Expand All @@ -13,7 +13,7 @@ func TestLookupCommandInfo(t *testing.T) {
func benchmarkLookupCommandInfo(b *testing.B, names ...string) {
for i := 0; i < b.N; i++ {
for _, c := range names {
LookupCommandInfo(c)
lookupCommandInfo(c)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion redis/doc.go
Expand Up @@ -174,4 +174,4 @@
// 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
// be closed.
package redis // import "github.com/gomodule/redigo/redis"
package redis
20 changes: 9 additions & 11 deletions redis/pool.go
Expand Up @@ -24,8 +24,6 @@ import (
"sync"
"sync/atomic"
"time"

"github.com/gomodule/redigo/internal"
)

var (
Expand Down Expand Up @@ -398,14 +396,14 @@ func (ac *activeConn) Close() error {
}
ac.pc = nil

if ac.state&internal.MultiState != 0 {
if ac.state&connectionMultiState != 0 {
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")
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("PUNSUBSCRIBE")
// To detect the end of the message stream, ask the server to echo
Expand All @@ -419,7 +417,7 @@ func (ac *activeConn) Close() error {
break
}
if p, ok := p.([]byte); ok && bytes.Equal(p, sentinel) {
ac.state &^= internal.SubscribeState
ac.state &^= connectionSubscribeState
break
}
}
Expand All @@ -442,7 +440,7 @@ func (ac *activeConn) Do(commandName string, args ...interface{}) (reply interfa
if pc == nil {
return nil, errConnClosed
}
ci := internal.LookupCommandInfo(commandName)
ci := lookupCommandInfo(commandName)
ac.state = (ac.state | ci.Set) &^ ci.Clear
return pc.c.Do(commandName, args...)
}
Expand All @@ -456,7 +454,7 @@ func (ac *activeConn) DoWithTimeout(timeout time.Duration, commandName string, a
if !ok {
return nil, errTimeoutNotSupported
}
ci := internal.LookupCommandInfo(commandName)
ci := lookupCommandInfo(commandName)
ac.state = (ac.state | ci.Set) &^ ci.Clear
return cwt.DoWithTimeout(timeout, commandName, args...)
}
Expand All @@ -466,7 +464,7 @@ func (ac *activeConn) Send(commandName string, args ...interface{}) error {
if pc == nil {
return errConnClosed
}
ci := internal.LookupCommandInfo(commandName)
ci := lookupCommandInfo(commandName)
ac.state = (ac.state | ci.Set) &^ ci.Clear
return pc.c.Send(commandName, args...)
}
Expand Down
34 changes: 17 additions & 17 deletions internal/commandinfo.go → redisx/commandinfo.go
Expand Up @@ -12,32 +12,32 @@
// License for the specific language governing permissions and limitations
// under the License.

package internal // import "github.com/gomodule/redigo/internal"
package redisx

import (
"strings"
)

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() {
Expand All @@ -46,7 +46,7 @@ func init() {
}
}

func LookupCommandInfo(commandName string) CommandInfo {
func lookupCommandInfo(commandName string) commandInfo {
if ci, ok := commandInfos[commandName]; ok {
return ci
}
Expand Down
11 changes: 11 additions & 0 deletions 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)
}
}
}
3 changes: 1 addition & 2 deletions redisx/connmux.go
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"sync"

"github.com/gomodule/redigo/internal"
"github.com/gomodule/redigo/redis"
)

Expand Down Expand Up @@ -60,7 +59,7 @@ type muxConn struct {
}

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")
}
p := c.p
Expand Down
17 changes: 8 additions & 9 deletions redisx/connmux_test.go
Expand Up @@ -19,13 +19,12 @@ import (
"sync"
"testing"

"github.com/gomodule/redigo/internal/redistest"
"github.com/gomodule/redigo/redis"
"github.com/gomodule/redigo/redisx"
)

func TestConnMux(t *testing.T) {
c, err := redistest.Dial()
c, err := redisx.DialTest()
if err != nil {
t.Fatalf("error connection to database, %v", err)
}
Expand Down Expand Up @@ -57,7 +56,7 @@ func TestConnMux(t *testing.T) {
}

func TestConnMuxClose(t *testing.T) {
c, err := redistest.Dial()
c, err := redisx.DialTest()
if err != nil {
t.Fatalf("error connection to database, %v", err)
}
Expand Down Expand Up @@ -93,7 +92,7 @@ func TestConnMuxClose(t *testing.T) {

func BenchmarkConn(b *testing.B) {
b.StopTimer()
c, err := redistest.Dial()
c, err := redisx.DialTest()
if err != nil {
b.Fatalf("error connection to database, %v", err)
}
Expand All @@ -109,7 +108,7 @@ func BenchmarkConn(b *testing.B) {

func BenchmarkConnMux(b *testing.B) {
b.StopTimer()
c, err := redistest.Dial()
c, err := redisx.DialTest()
if err != nil {
b.Fatalf("error connection to database, %v", err)
}
Expand All @@ -130,7 +129,7 @@ func BenchmarkConnMux(b *testing.B) {
func BenchmarkPool(b *testing.B) {
b.StopTimer()

p := redis.Pool{Dial: redistest.Dial, MaxIdle: 1}
p := redis.Pool{Dial: redisx.DialTest, MaxIdle: 1}
defer p.Close()

// Fill the pool.
Expand All @@ -155,7 +154,7 @@ const numConcurrent = 10

func BenchmarkConnMuxConcurrent(b *testing.B) {
b.StopTimer()
c, err := redistest.Dial()
c, err := redisx.DialTest()
if err != nil {
b.Fatalf("error connection to database, %v", err)
}
Expand Down Expand Up @@ -186,7 +185,7 @@ func BenchmarkConnMuxConcurrent(b *testing.B) {
func BenchmarkPoolConcurrent(b *testing.B) {
b.StopTimer()

p := redis.Pool{Dial: redistest.Dial, MaxIdle: numConcurrent}
p := redis.Pool{Dial: redisx.DialTest, MaxIdle: numConcurrent}
defer p.Close()

// Fill the pool.
Expand Down Expand Up @@ -224,7 +223,7 @@ func BenchmarkPoolConcurrent(b *testing.B) {

func BenchmarkPipelineConcurrency(b *testing.B) {
b.StopTimer()
c, err := redistest.Dial()
c, err := redisx.DialTest()
if err != nil {
b.Fatalf("error connection to database, %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/redistest/testdb.go → redisx/db_test.go
Expand Up @@ -13,7 +13,7 @@
// under the License.

// Package redistest contains utilities for writing Redigo tests.
package redistest
package redisx

import (
"errors"
Expand Down Expand Up @@ -41,7 +41,7 @@ func (t testConn) Close() error {
// Dial dials the local Redis server and selects database 9. To prevent
// stomping on real data, DialTestDB fails if database 9 contains data. The
// 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)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion redisx/doc.go
Expand Up @@ -14,4 +14,4 @@

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

0 comments on commit b9037db

Please sign in to comment.