Skip to content

Commit

Permalink
m.redigo(), to avoid a TCP connection in Lua
Browse files Browse the repository at this point in the history
Not public to keep the redigo stuff internal
  • Loading branch information
alicebob committed Jan 15, 2018
1 parent a082243 commit 7333f0a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
9 changes: 1 addition & 8 deletions cmd_scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package miniredis
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"strconv"
"strings"

"github.com/garyburd/redigo/redis"
lua "github.com/yuin/gopher-lua"
"github.com/yuin/gopher-lua/parse"

Expand All @@ -25,12 +23,7 @@ func (m *Miniredis) runLuaScript(c *server.Peer, script string, args []string) {
l := lua.NewState()
defer l.Close()

// create a redis client for redis.call
conn, err := redis.Dial("tcp", m.srv.Addr().String())
if err != nil {
c.WriteError(fmt.Sprintf("ERR Redis error: %v", err.Error()))
return
}
conn := m.redigo()
defer conn.Close()

// set global variable KEYS
Expand Down
10 changes: 10 additions & 0 deletions miniredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ package miniredis

import (
"fmt"
"net"
"strconv"
"sync"
"time"

redigo "github.com/garyburd/redigo/redis"

"github.com/alicebob/miniredis/server"
)

Expand Down Expand Up @@ -237,6 +240,13 @@ func (m *Miniredis) FastForward(duration time.Duration) {
}
}

// redigo returns a redigo.Conn, connected using net.Pipe
func (m *Miniredis) redigo() redigo.Conn {
c1, c2 := net.Pipe()
m.srv.ServeConn(c1)
return redigo.NewConn(c2, 0, 0)
}

// Dump returns a text version of the selected DB, usable for debugging.
func (m *Miniredis) Dump() string {
m.Lock()
Expand Down
18 changes: 18 additions & 0 deletions miniredis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,21 @@ func TestExpireWithFastForward(t *testing.T) {
s.FastForward(5 * time.Second)
equals(t, 1, len(s.Keys()))
}

func TestRedigo(t *testing.T) {
s, err := Run()
ok(t, err)

r := s.redigo()
defer r.Close()

_, err = r.Do("SELECT", 2)
ok(t, err)

_, err = r.Do("SET", "foo", "bar")
ok(t, err)

v, err := redis.String(r.Do("GET", "foo"))
ok(t, err)
equals(t, "bar", v)
}
35 changes: 20 additions & 15 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,29 @@ func (s *Server) serve(l net.Listener) {
if err != nil {
return
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer conn.Close()
s.mu.Lock()
s.peers[conn] = struct{}{}
s.infoConns++
s.mu.Unlock()

s.servePeer(conn)

s.mu.Lock()
delete(s.peers, conn)
s.mu.Unlock()
}()
s.ServeConn(conn)
}
}

// ServeConn handles a net.Conn. Nice with net.Pipe()
func (s *Server) ServeConn(conn net.Conn) {
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer conn.Close()
s.mu.Lock()
s.peers[conn] = struct{}{}
s.infoConns++
s.mu.Unlock()

s.servePeer(conn)

s.mu.Lock()
delete(s.peers, conn)
s.mu.Unlock()
}()
}

// Addr has the net.Addr struct
func (s *Server) Addr() *net.TCPAddr {
s.mu.Lock()
Expand Down

0 comments on commit 7333f0a

Please sign in to comment.