Skip to content
This repository has been archived by the owner on Nov 26, 2018. It is now read-only.

Features/spring cleanup #41

Open
wants to merge 83 commits into
base: features/flexible_config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
f7380fc
Endpoint cleanup.
bartTC Apr 14, 2014
aee0406
Adding some more logging to follow the restarting process
yml Apr 15, 2014
7a7bfec
Fixing a race condition between redis and postgres when we try to REF…
yml Apr 15, 2014
ba447d8
A more graceful channel update
yml Apr 16, 2014
72deadd
Move the sleep to avoid the race condition from botbot-bot to botbot-web
yml Apr 16, 2014
c31b353
golint
yml Apr 16, 2014
1085310
swap the posgresql driver to a better maintain repo. Set the applicat…
yml Apr 21, 2014
9cecd76
Explicitely setting a MaxOpenConns and MaxIdleConns for the postgesql…
yml Apr 22, 2014
e638673
Use redisDB number too.
bartTC Apr 22, 2014
ff4d3c0
Merge branch 'master' of https://github.com/BotBotMe/botbot-bot into …
yml Apr 23, 2014
c7e33dd
Build go against 1.2 and tip since we are now using some of the newer…
yml Apr 23, 2014
3cb59f5
Merge pull request #13 from BotBotMe/features/better-postgresql-conne…
yml Apr 23, 2014
1bb0ea1
Reduce the time between ping from 5 min to 10s. Try to reconnect afte…
yml May 16, 2014
eb93611
Merge branch 'features/better-postgresql-connection-handling'
yml May 16, 2014
1e752dd
ref #15. re architecture the ircBot.monitor() to reconnect if there i…
yml May 18, 2014
792c7f1
ref #15. Fix a potential race condition in the NetworkManager
yml May 18, 2014
cbdefe8
ref #15. Fix another race condition reported by the runtime.
yml May 18, 2014
988592a
ref #15. Reduce the verbosity of the log and improve its readability
yml May 18, 2014
b9f1f88
refs #15. Improved the logging before reconnecting
yml May 18, 2014
0bc1136
refs #15. give a grace period of 3 missed reply to Ping.
yml May 18, 2014
1e2b522
ref #15. Increase maxPongWithoutMessage from 15 to 150 to only rejoin…
yml May 18, 2014
13ca9c4
Improve the error loging to know which bot cannot write the the socke…
yml May 19, 2014
30cbab6
Reconnect a bit more aggresively when the server do not respond to ping
yml May 19, 2014
e4ba11a
Add expvar and export some stats for each bots
yml May 29, 2014
1ea5830
Change the naming of the stats for each bots
yml May 29, 2014
79376d0
Remove the nick from the stats name because it seems to upset graphite
yml May 29, 2014
c087d41
Taking advantage of the new server_identifier to push the stats to gr…
yml May 30, 2014
e191e8a
Added an expvar to count the received messages from IRC
yml Jun 1, 2014
ca09d03
Update README.md
yml Jun 6, 2014
aff63d3
Added a goroutine that collect the channels every bot is connected to…
yml Jun 6, 2014
021884e
Merge branch 'master' of https://github.com/BotBotMe/botbot-bot
yml Jun 6, 2014
aa66850
Added defer before unlock
vbabiy Jun 17, 2014
11f5442
Fix an issue on the method comparing the channels.
yml Jun 17, 2014
9a464e2
Oups this line should have been deleted in my previous commit
yml Jun 17, 2014
ce577dd
Fix a bunch of races in the test suite. Added a Field on the ircBot s…
yml Jun 19, 2014
ebbbda0
Use my protected method GotLength() instead of directly taking the le…
yml Jun 19, 2014
0af55d5
Continue to fix race condition in the test suite
yml Jun 19, 2014
b39692b
Increate the number of tries before belling out
yml Jun 19, 2014
79286be
An even larger number of tries to support slow server
yml Jun 19, 2014
59801dc
Fixing mock irc server to handle tls connect correctly.
vbabiy Jun 19, 2014
d52af7a
Merge branch 'master' into features/fix_races_in_test
vbabiy Jun 19, 2014
1e338c9
improve the ChatBot expvar stats
yml Jun 20, 2014
f3a15cb
Enable pprof
vbabiy Jun 21, 2014
f4c1d33
Add a .gitignore
vbabiy Jun 21, 2014
b5f8157
Move to a buffered channel till we refactor monitor()
vbabiy Jun 21, 2014
1f6c287
Merge branch 'master' into features/better_chatbotStats
yml Jun 23, 2014
1daf637
Itermediary changeset this is an improvement over the exisiting code …
yml Jun 23, 2014
85f4f7c
Break out read socket in to its goroutine
vbabiy Jun 23, 2014
f6f313c
Linting. Fix a panic when tring to write a closed bot.socket
yml Jun 23, 2014
18b94d3
Better connect
yml Jun 23, 2014
df6576a
Reinitialized the missedPing to 0
yml Jun 23, 2014
d2aa030
Fixing monitoring, and backoff.
vbabiy Jun 23, 2014
b94758a
Merge branch 'features/better_chatbotStats' of github.com:BotBotMe/bo…
vbabiy Jun 23, 2014
3c884e5
Fixing formatting
vbabiy Jun 23, 2014
0225b08
reverted the changes that aim to change the log library.
yml Jun 24, 2014
d67f8d7
move the goroutine to read socket in a more logical place where we ar…
yml Jun 24, 2014
6d3e35b
Move the goroutine whoiscollector and into monitor
yml Jun 25, 2014
51b6acb
Continue to clean up irc.go. Fix a panic when the bot try to reconnec…
yml Jun 25, 2014
5421ff1
Return from the monitor goroutine when we send the reconnect
yml Jun 25, 2014
52fb875
Fxing TestFlood and added a work around to force glog to log the test…
yml Jun 25, 2014
74512b5
Fix a race condition that is expose when running irc_test.
yml Jun 25, 2014
a171f31
Fix the test suite
yml Jun 25, 2014
0e6dd3a
Tells travis that it should also run the tests for the sub packages
yml Jun 25, 2014
a44b98e
Deactivate the pingTimeout case if we get a response.
yml Jun 28, 2014
e03945a
Fix another race in the MockIRCServer implementation.
yml Jun 28, 2014
f4aeb20
Cleanup the logging statement use Errorln for error and V(3) for the …
yml Jun 28, 2014
aa0aae5
Be stingy with the lock and take the smalest lock possible and releas…
yml Jun 28, 2014
3017443
Continue reduce time spent locking.
yml Jun 28, 2014
6d54ac9
Remove bot.closing from the ircBot struct. I explictely pass around a…
yml Jun 28, 2014
26b6994
Fix the last known race when the bot get disconnected for more than 4…
yml Jun 28, 2014
69ad07c
Merge sender and listen into a single goroutine ListenAndSend
yml Jun 30, 2014
6dd12e3
Fix a deadlock when several gorountines try to get the botstats.
yml Jun 30, 2014
9ab1f49
Double the number of Max open connections to postgres
yml Jun 30, 2014
1ba3ff6
Reduce the time where we are locking the bot in the bot.updateNick
yml Jun 30, 2014
abce29c
Temporary remove self.recordUserCounts() from the code base because I…
yml Jul 1, 2014
3205be5
nothing
yml Jul 1, 2014
c7909d6
nothing
yml Jul 1, 2014
21edbcb
move ircBot.init() to a not exported name.
yml Jul 2, 2014
fbbeb85
Move Connect and ListenAndSend to not exported name.
yml Jul 2, 2014
d4591bf
Spring cleanup. Reduce the surface of ircBot. Move ircBot.connect() o…
yml Jul 2, 2014
8fe417b
The reconnect is done at the networkmanager level. More cleanup
yml Jul 2, 2014
7e94c1c
I have commited the integration test because I can't figure out how t…
yml Jul 2, 2014
c89f71a
Fix the main integration test by using tls.Listen
yml Jul 3, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: go

go:
- 1.1
- 1.2
- tip

install:
- go get github.com/BotBotMe/botbot-bot

script: go test github.com/BotBotMe/botbot-bot
script: go test github.com/BotBotMe/botbot-bot/...
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/BotBotMe/botbot-bot.png)](https://travis-ci.org/BotBotMe/botbot-bot)

The bot used in botbot.me. To install:
The bot used in botbot.me is a Go (1.2) program. To install:

go get github.com/BotBotMe/botbot-bot

Expand Down
9 changes: 6 additions & 3 deletions botbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func (self *BotBot) listen(queueName string) {
}

func (self *BotBot) mainLoop() {

go self.recordUserCounts()
// TODO (yml) comment out self.recordUserCounts because I think it is
// leaking postgres connection.
//go self.recordUserCounts()

var busCommand string
var args string
for {
select {

case serverLine, ok := <-self.fromServer:
if !ok {
// Channel is closed, we're offline. Stop.
Expand Down Expand Up @@ -120,6 +120,9 @@ func (self *BotBot) mainLoop() {
// - WRITE <chatbotid> <channel> <msg>: Send message to server
// - REFRESH: Reload plugin configuration
func (self *BotBot) handleCommand(cmd string, args string) {
if glog.V(2) {
glog.Infoln("HandleCommand:", cmd)
}
switch cmd {
case "WRITE":
parts := strings.SplitN(args, " ", 3)
Expand Down
12 changes: 12 additions & 0 deletions certs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
The certs included in this repo has been with `generate_cert` from the GO
standard library.

```
generate_cert -ca=true -duration=8760h0m0s -host="127.0.0.1" -start-date="Jan 1 15:04:05 2014"
```

The generate_cert can be build like this

```
go build $GOROOT/src/pkg/crypto/tls/generate_cert.go
```
18 changes: 18 additions & 0 deletions certs/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIC8zCCAd2gAwIBAgIQLlTIDFFPizKZzJKC6j/k4jALBgkqhkiG9w0BAQswEjEQ
MA4GA1UEChMHQWNtZSBDbzAeFw0xNDAxMDExNTA0MDVaFw0xNTAxMDExNTA0MDVa
MBIxEDAOBgNVBAoTB0FjbWUgQ28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQDMVw4BGI1GsAdye8rqiSUS/gOI1oGpdbHO/B99xxXib9VvqFx3pT4WkQdd
OqAni7X4K4Td/3MBQ/K+Wy982wxR3pCHCWRxZ6iDQi/TPMRqP8hSufTyRd/QA+bj
UEXUh4Orq3VthIq0i4TjML9rC14pJy/ldb80clHLoVdx2uz3t52k0VZS1QMwLLpW
l3khpjO7ICUsdfFda+oMbroQPlgSEg69QcBLmwTuxJ7Y2PRVK6nWU/2+fGKQQXGT
PA9jU/zHt/7voUA9I1zXM1s7e1ExRbD4MFlaLwUo0qlbLL0dsIFURiJh3fBvgum/
T7NQRvEJQQFx25S6/zyxn1VwKcVbAgMBAAGjSTBHMA4GA1UdDwEB/wQEAwIApDAT
BgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MA8GA1UdEQQIMAaH
BH8AAAEwCwYJKoZIhvcNAQELA4IBAQA8YZJmhOwIYEhUzObwQQzcJY3ZrUN7AiEc
sQnFaqOmzrtP0SDjWPlMITXeHV84Zw5+taIv1OQ3IyG9z+BPgIAkHQYVuVILrYOU
nVGs9bxdcrhKLwKVXEUoRsa+FY3NG8r7HTO+jYveXoXBjqA3NT7CmlJ3QtJJiCD/
5S5u1KwfAZQ7xcrigBxTyRSpR/m0APGDGqrf5mKUcQVkGF9RIEDWOuCoqr1He9GZ
ba6HNYeEQrsdooazi9xbR2bk6F0Wa2Pt4YpS5Zy3KVuhxwbo43houwifhjF23MM/
4DmoVl1copKHf/dMSsiDbGDeF2g/Z6Q2X790ijDOnxkOEbpjyuHP
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions certs/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this was unintentional

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the author, but as noted above, it's a self-signed cert, probably used for testing. It isn't used for anything in production.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just seems overall that it would be better to just leave the readme explaining how to get here rather than distributing a default cert even if the intention is for testing.

MIIEpAIBAAKCAQEAzFcOARiNRrAHcnvK6oklEv4DiNaBqXWxzvwffccV4m/Vb6hc
d6U+FpEHXTqgJ4u1+CuE3f9zAUPyvlsvfNsMUd6QhwlkcWeog0Iv0zzEaj/IUrn0
8kXf0APm41BF1IeDq6t1bYSKtIuE4zC/awteKScv5XW/NHJRy6FXcdrs97edpNFW
UtUDMCy6Vpd5IaYzuyAlLHXxXWvqDG66ED5YEhIOvUHAS5sE7sSe2Nj0VSup1lP9
vnxikEFxkzwPY1P8x7f+76FAPSNc1zNbO3tRMUWw+DBZWi8FKNKpWyy9HbCBVEYi
Yd3wb4Lpv0+zUEbxCUEBcduUuv88sZ9VcCnFWwIDAQABAoIBAQDGY07JaZjgJE9W
qzGa+4PvKIct93Tznb0ABHBeoUdyDLngKkl8MAZTyoKn1R9gxZfqVfYtwrFZbSrj
/YvhYJpZcghH0qqeH9HPfhcIs1rO/RX0m25hOc3OG7uyvmDNsrP3nAPqTGCOYVan
bNCrkOHeeplZ4jO9K7CWTxKjC7Y92V1EL1IW1H3NU7z+6gA/r5JRUhrSljR37sja
RIc3kBPqfo+qlwTSQ8m9A8dKVQro6Lj8xzSUxwZbDRV83GJDQS0UWDRZEbxO/6Oq
xNp6LEeFjjgI4CpKtPogGkOrcrOTRlZt4Y0bFaEYmKjsMepUIBmf7rbT/C9dfJKc
76YzTnnBAoGBANCBB43/jJuOyvwVM+n7mn0/dRBhnioe0ffONu0EXKx42JFUZ2hf
nnFbFQDjQU5EJDyWmz7gRSbcWXOBdrLwhLfqgOs4NBO9IYfw8tCjFfvLieo1cC1g
k++qfxQWSGlLOqLHmH89a2Zo+n6E2bVHZeEPLtx9BaKsDu/JjtFcn8bpAoGBAPrj
M9RvY0tpPcfmoBch3LYseGLPonbVJLOea2xOyQRgUuzSD8rfcoKL9HrmLGV1jP7b
dUamOle+1gOIjMN/NDTULnbU3of9Jpg7sM4QN58Ae2VVheJiP7vQ3mQMVGQFNjZn
PqkHsX8SrSufjJPXXZpyEB8eoncEOHrEXCSY98ejAoGAUKTXSzDALZp5IC8DOUi7
ZB4bJQ7coeXxyCrWS64YOg0zlmJS7KevHKDh6sn7BE6OxXBq81LC7FjlsCsuwd8N
NiVZX6EJY2kPgwjKoa+yXQVIfnHUxXhJP7VuGVLVBwS11Sfl77DNzXplCHJR0i+f
VAEPWvBwMY7i6BPJTWjJ06ECgYB+PuoL0ekzP0f5WVHhkgaQHRyPDFCCX4rSX7tK
ivKYIyhXRvTvbzUd//MMDNr2SwqiDCidXnQpRzxGFuZOUaohk0u2PDOmVnqfTBoR
9xMmDYYKXdi7LBlKFzZxPcsRcZXwFk/vmpF0kh9VvjltA4h0eUUng7DZFuSc0kZT
nC3C5QKBgQCfg1NASP4WsANzaLABzfDKbQ23pxb9eqBWGVESEPhzUoqiTZ0rSDl4
5oY7Dx9RGIJxPQWoG3nxjAK3GnlArUEJf4S/YMFmOX0naXp4HW4RxODPNoxnaQG0
NfDb7xAuQNgY2F5T9dK02EPJ0tamsJbvHxVZLm1eiU2HPcK1ZurRfw==
-----END RSA PRIVATE KEY-----
25 changes: 19 additions & 6 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"io"
"strings"
)

// Interface all bots must implement
Expand All @@ -15,11 +16,23 @@ type ChatBot interface {

// Configuration for a 'chatbot', which is what clients pay for
type BotConfig struct {
Id int
Config map[string]string
Id int
Config map[string]string
Channels []*Channel
}

// Configuration for a channel
type Channel struct {
Id int
Name string
Pwd string
Fingerprint string
}

func (cc *Channel) Credential() string {
return strings.TrimSpace(cc.Name + " " + cc.Pwd)
}

// Normally this array is names of the channels to join
// but if the channel has a password, the string is "<channel> <pw>"
// That means we can pass the string straight to JOIN and it works
Channels []string
func (cc *Channel) String() string {
return cc.Name
}
127 changes: 98 additions & 29 deletions common/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,65 @@ package common

import (
"bufio"
"net"
"crypto/tls"
"flag"
"log"
"sync"
"testing"
"time"

"github.com/golang/glog"
)

// SetGlogFlags walk around a glog issue and force it to log to stderr.
// It need to be called at the beginning of each test.
func SetGlogFlags() {
flag.Set("alsologtostderr", "true")
flag.Set("V", "3")
}

// MockSocket is a dummy implementation of ReadWriteCloser
type MockSocket struct {
sync.RWMutex
Counter chan bool
Receiver chan string
}

func (sock MockSocket) Write(data []byte) (int, error) {
glog.V(3).Infoln("[Debug]: Starting MockSocket.Write of:", string(data))
if sock.Counter != nil {
sock.Counter <- true
}
if sock.Receiver != nil {
sock.Receiver <- string(data)
}

return len(data), nil
}

func (sock MockSocket) Read(into []byte) (int, error) {
sock.RLock()
defer sock.RUnlock()
time.Sleep(time.Second) // Prevent busy loop
return 0, nil
}

func (sock MockSocket) Close() error {
if sock.Receiver != nil {
close(sock.Receiver)
}
if sock.Counter != nil {
close(sock.Counter)
}
return nil
}

/*
* Mock IRC server
*/

type MockIRCServer struct {
sync.RWMutex
Port string
Message string
Got []string
Expand All @@ -24,44 +74,63 @@ func NewMockIRCServer(msg, port string) *MockIRCServer {
}
}

func (self *MockIRCServer) Run(t *testing.T) {
func (srv *MockIRCServer) GotLength() int {
srv.RLock()
defer srv.RUnlock()
return len(srv.Got)
}

listener, err := net.Listen("tcp", ":"+self.Port)
func (srv *MockIRCServer) Run(t *testing.T) {
// Use the certs generated with generate_certs
cert, err := tls.LoadX509KeyPair("certs/cert.pem", "certs/key.pem")
if err != nil {
t.Error("Error starting mock server on "+self.Port, err)
return
log.Fatalf("server: loadkeys: %s", err)
}

// Accept a single connection
conn, lerr := listener.Accept()
if lerr != nil {
t.Error("Error on IRC server on Accept. ", err)
config := tls.Config{Certificates: []tls.Certificate{cert}}
listener, err := tls.Listen("tcp", "127.0.0.1:"+srv.Port, &config)
if err != nil {
t.Error("Error starting mock server on "+srv.Port, err)
return
}

// First message triggers BotBot to send USER and NICK messages
conn.Write([]byte(":hybrid7.debian.local NOTICE AUTH :*** Looking up your hostname...\n"))

// Ask for NickServ auth, and pretend we got it
conn.Write([]byte(":NickServ!NickServ@services. NOTICE graham_king :This nickname is registered. Please choose a different nickname, or identify via /msg NickServ identify <password>\n"))
conn.Write([]byte(":NickServ!NickServ@services. NOTICE graham_king :You are now identified for graham_king.\n"))
for {
conn, lerr := listener.Accept()
// If create a new connection throw the old data away
// This can happen if a client trys to connect with tls
// Got will store the handshake data. The cient will try
// connect with a plaintext connect after the tls fails.
srv.Lock()
srv.Got = make([]string, 0)
srv.Unlock()

conn.Write([]byte(":wolfe.freenode.net 001 graham_king :Welcome to the freenode Internet Relay Chat Network graham_king\n"))
if lerr != nil {
t.Error("Error on IRC server on Accept. ", err)
}

// This should get sent to plugins
conn.Write([]byte(":[email protected] PRIVMSG #unit :" + self.Message + "\n"))
//conn.Write([]byte("test: " + self.Message + "\n"))
// First message triggers BotBot to send USER and NICK messages
conn.Write([]byte(":hybrid7.debian.local NOTICE AUTH :*** Looking up your hostname...\n"))
// Ask for NickServ auth, and pretend we got it
conn.Write([]byte(":NickServ!NickServ@services. NOTICE graham_king :This nickname is registered. Please choose a different nickname, or identify via /msg NickServ identify <password>\n"))
conn.Write([]byte(":NickServ!NickServ@services. NOTICE graham_king :You are now identified for graham_king.\n"))
conn.Write([]byte(":wolfe.freenode.net 001 graham_king :Welcome to the freenode Internet Relay Chat Network graham_king\n"))
// This should get sent to plugins
conn.Write([]byte(":[email protected] PRIVMSG #unit :" + srv.Message + "\n"))
conn.Write([]byte("test: " + srv.Message + "\n"))

var derr error
var data []byte
var derr error
var data []byte

bufRead := bufio.NewReader(conn)
for {
data, derr = bufRead.ReadBytes('\n')
if derr != nil {
// Client closed connection
break
bufRead := bufio.NewReader(conn)
for {
data, derr = bufRead.ReadBytes('\n')
if derr != nil {
// Client closed connection
break
}
srv.Lock()
srv.Got = append(srv.Got, string(data))
srv.Unlock()
}
self.Got = append(self.Got, string(data))
}

}
38 changes: 27 additions & 11 deletions common/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"net"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"

"github.com/golang/glog"
Expand Down Expand Up @@ -38,6 +41,7 @@ type Queue interface {

// Simplistic Queue implementation used by the test suite
type MockQueue struct {
sync.RWMutex
Got map[string][]string
ReadChannel chan string
}
Expand All @@ -49,23 +53,29 @@ func NewMockQueue() *MockQueue {
}
}

func (self *MockQueue) Publish(queue string, message []byte) error {
self.Got[queue] = append(self.Got[queue], string(message))
func (mq *MockQueue) Publish(queue string, message []byte) error {
mq.Lock()
defer mq.Unlock()
mq.Got[queue] = append(mq.Got[queue], string(message))
return nil
}

func (self *MockQueue) Rpush(key string, val []byte) error {
self.Got[key] = append(self.Got[key], string(val))
func (mq *MockQueue) Rpush(key string, val []byte) error {
mq.Lock()
defer mq.Unlock()
mq.Got[key] = append(mq.Got[key], string(val))
return nil
}

func (self *MockQueue) Blpop(keys []string, timeoutsecs uint) (*string, []byte, error) {
val := <-self.ReadChannel
func (mq *MockQueue) Blpop(keys []string, timeoutsecs uint) (*string, []byte, error) {
val := <-mq.ReadChannel
return &keys[0], []byte(val), nil
}

func (self *MockQueue) Llen(key string) (int, error) {
return len(self.Got), nil
func (mq *MockQueue) Llen(key string) (int, error) {
mq.RLock()
defer mq.RUnlock()
return len(mq.Got), nil
}

func (self *MockQueue) Ltrim(key string, start int, end int) error {
Expand All @@ -86,15 +96,21 @@ type RedisQueue struct {
}

func NewRedisQueue() Queue {
redisUrlString := os.Getenv("QUEUE_URL")
redisUrlString := os.Getenv("REDIS_PLUGIN_QUEUE_URL")
if redisUrlString == "" {
glog.Fatal("QUEUE_URL cannot be empty.\nexport QUEUE_URL=redis://host:port/db_number")
glog.Fatal("REDIS_PLUGIN_QUEUE_URL cannot be empty.\nexport REDIS_PLUGIN_QUEUE_URL=redis://host:port/db_number")
}
redisUrl, err := url.Parse(redisUrlString)
if err != nil {
glog.Fatal("Could not read Redis string", err)
}
redisQueue := goredis.Client{Addr: redisUrl.Host}

redisDb, err := strconv.Atoi(strings.TrimLeft(redisUrl.Path, "/"))
if err != nil {
glog.Fatal("Could not read Redis path", err)
}

redisQueue := goredis.Client{Addr: redisUrl.Host, Db: redisDb}
s := RedisQueue{queue: &redisQueue}
s.waitForRedis()
return &s
Expand Down
Loading