Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ability to Test and Reconnect to Data Store #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion redis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package shield

import (
"github.com/garyburd/redigo/redis"
"github.com/gomodule/redigo/redis"
"log"
"strconv"
)
Expand Down Expand Up @@ -50,6 +50,19 @@ func (rs *RedisStore) conn() (conn redis.Conn, err error) {
return rs.redis, nil
}

func (rs *RedisStore) TestConnection() {
if c, err := rs.conn(); err == nil {
if _, err := c.Do("PING"); err == nil {
return
}
}

log.Println("Resetting redis connection...")

rs.redis = nil
rs.conn()
}

func (rs *RedisStore) Classes() (a []string, err error) {
c, err := rs.conn()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ func (s *shield) Classify(text string) (class string, err error) {
func (sh *shield) Reset() error {
return sh.store.Reset()
}

func (sh *shield) TestConnection() {
sh.store.TestConnection()
}
4 changes: 4 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Shield interface {

// Reset clears the storage
Reset() error

// Checks and reestablishes connection to the data store
TestConnection()
}

type Store interface {
Expand All @@ -36,4 +39,5 @@ type Store interface {
IncrementClassWordCounts(m map[string]map[string]int64) error
TotalClassWordCounts() (map[string]int64, error)
Reset() error
TestConnection()
}