From cdbd0d8ec92e37b781d6cef2900889f194317b2a Mon Sep 17 00:00:00 2001 From: Marcin Kaszynski Date: Fri, 22 Dec 2017 19:35:12 +0100 Subject: [PATCH 1/4] Show TODO markers during build. --- go/Makefile | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/go/Makefile b/go/Makefile index 943bb6a..9ad2bee 100644 --- a/go/Makefile +++ b/go/Makefile @@ -2,7 +2,7 @@ BINARIES=exsrv all: $(BINARIES) -exsrv: redrpc/*.go commands/exsrv/*.go +exsrv: show-todos redrpc/*.go commands/exsrv/*.go ./scripts/go build -o "$@" github.com/Codility/redis-rpc/go/commands/exsrv .PHONY: clean @@ -10,7 +10,7 @@ clean: rm -rf $(BINARIES) .gopath .PHONY: test -test: goimports govet +test: show-todos goimports govet ./scripts/go test -v github.com/Codility/redis-rpc/go/redrpc .PHONY: govet @@ -29,3 +29,10 @@ goimports: exit 1; \ fi @echo "goimports passed" + +.PHONY: show-todos +show-todos: + @echo "---------- TODOs ----------" + @grep --line-number '// TODO' `find . -name \*.go | grep -v '\(/vendor/\|/.gopath/\)'` \ + | perl -ple 's|^([^:]+:[^:]+:)[^/]*|sprintf("%-40s", $$1)|e' + @echo From 81f809c2e6f0e36bb24a966ecf328c51423c2133 Mon Sep 17 00:00:00 2001 From: Marcin Kaszynski Date: Fri, 22 Dec 2017 19:56:30 +0100 Subject: [PATCH 2/4] Hide perl warnings about misconfigured locale. --- go/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/Makefile b/go/Makefile index 9ad2bee..73fb481 100644 --- a/go/Makefile +++ b/go/Makefile @@ -34,5 +34,5 @@ goimports: show-todos: @echo "---------- TODOs ----------" @grep --line-number '// TODO' `find . -name \*.go | grep -v '\(/vendor/\|/.gopath/\)'` \ - | perl -ple 's|^([^:]+:[^:]+:)[^/]*|sprintf("%-40s", $$1)|e' + | LC_ALL=C perl -ple 's|^([^:]+:[^:]+:)[^/]*|sprintf("%-40s", $$1)|e' @echo From eb73d0525258b27aed82b404dd750f5ee4d67252 Mon Sep 17 00:00:00 2001 From: Marcin Kaszynski Date: Fri, 22 Dec 2017 19:56:59 +0100 Subject: [PATCH 3/4] Rotate keys in calls to BLPOP to avoid starving queues. --- go/redrpc/server.go | 15 +++++++++++++-- go/redrpc/server_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/go/redrpc/server.go b/go/redrpc/server.go index e12aff3..06cf44a 100644 --- a/go/redrpc/server.go +++ b/go/redrpc/server.go @@ -41,7 +41,8 @@ type Server struct { queues []string queueMap map[string]string - closing int64 + closing int64 + iterations int } type RequestImpl struct { @@ -98,7 +99,9 @@ func (s *Server) Run() { } func (s *Server) RunOnce() bool { - res, err := s.red.BLPop(time.Second, s.queues...) + res, err := s.red.BLPop(time.Second, rotated(s.queues, s.iterations)...) + s.iterations += 1 + if err != nil && err != redis.Nil { log.Print("Error in BLPOP: ", err) return false @@ -210,3 +213,11 @@ func (s *Server) sendResponse(func_name string, req *RequestImpl, res interface{ func (s *Server) timestamp() string { return s.opts.TimeSource().Format(time.RFC3339) } + +func rotated(s []string, i int) []string { + if len(s) == 0 { + return s + } + i = i % len(s) + return append(s[i:], s[:i]...) +} diff --git a/go/redrpc/server_test.go b/go/redrpc/server_test.go index d760ee6..6a3d892 100644 --- a/go/redrpc/server_test.go +++ b/go/redrpc/server_test.go @@ -130,3 +130,28 @@ func TestPropagatePanic(t *testing.T) { assert.Equal(t, result, []string{"redis_rpc:test:result:call-id", `{"ts":"2018-01-01T00:00:00Z","err":"oh no"}`}) assert.Equal(t, resultTTL, 10*time.Second) } + +func TestServerRotatesQueues(t *testing.T) { + blpopCalls := [][]string{} + + red := &TestDbAdapter{ + blpop: func(timeout time.Duration, keys ...string) ([]string, error) { + blpopCalls = append(blpopCalls, keys) + return nil, nil + }, + } + srv := NewServerWithAdapter(red, nil, map[string]Handler{ + "a": HandlerFunc(func(req Request) (interface{}, error) { return nil, nil }), + "b": HandlerFunc(func(req Request) (interface{}, error) { return nil, nil }), + "c": HandlerFunc(func(req Request) (interface{}, error) { return nil, nil }), + }) + assert.True(t, srv.RunOnce()) + assert.True(t, srv.RunOnce()) + assert.True(t, srv.RunOnce()) + + assert.Equal(t, blpopCalls, [][]string{ + []string{"redis_rpc:a:calls", "redis_rpc:b:calls", "redis_rpc:c:calls"}, + []string{"redis_rpc:b:calls", "redis_rpc:c:calls", "redis_rpc:a:calls"}, + []string{"redis_rpc:c:calls", "redis_rpc:a:calls", "redis_rpc:b:calls"}, + }) +} From 0cfb0b9c4a1afe31a65b18fb4921ac4b8f44e361 Mon Sep 17 00:00:00 2001 From: Marcin Kaszynski Date: Fri, 12 Jan 2018 18:39:04 +0100 Subject: [PATCH 4/4] Post-merge fix. --- go/redrpc/server_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/redrpc/server_test.go b/go/redrpc/server_test.go index 174263a..6c43f16 100644 --- a/go/redrpc/server_test.go +++ b/go/redrpc/server_test.go @@ -135,7 +135,7 @@ func TestServerRotatesQueues(t *testing.T) { blpopCalls := [][]string{} red := &TestDbAdapter{ - blpop: func(timeout time.Duration, keys ...string) ([]string, error) { + blpopMock: func(timeout time.Duration, keys ...string) ([]string, error) { blpopCalls = append(blpopCalls, keys) return nil, nil },