Merge pull request #6720 from invino4/slave

Make guestbook-go example use slaves
This commit is contained in:
Robert Bailey 2015-04-13 13:25:19 -07:00
commit 68c3f3a914

View File

@ -27,11 +27,14 @@ import (
"github.com/xyproto/simpleredis"
)
var pool *simpleredis.ConnectionPool
var (
masterPool *simpleredis.ConnectionPool
slavePool *simpleredis.ConnectionPool
)
func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["key"]
list := simpleredis.NewList(pool, key)
list := simpleredis.NewList(slavePool, key)
members := HandleError(list.GetAll()).([]string)
membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte)
rw.Write(membersJSON)
@ -40,13 +43,13 @@ func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
func ListPushHandler(rw http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["key"]
value := mux.Vars(req)["value"]
list := simpleredis.NewList(pool, key)
list := simpleredis.NewList(masterPool, key)
HandleError(nil, list.Add(value))
ListRangeHandler(rw, req)
}
func InfoHandler(rw http.ResponseWriter, req *http.Request) {
info := HandleError(pool.Get(0).Do("INFO")).([]byte)
info := HandleError(masterPool.Get(0).Do("INFO")).([]byte)
rw.Write(info)
}
@ -71,8 +74,10 @@ func HandleError(result interface{}, err error) (r interface{}) {
}
func main() {
pool = simpleredis.NewConnectionPoolHost("redis-master:6379")
defer pool.Close()
masterPool = simpleredis.NewConnectionPoolHost("redis-master:6379")
defer masterPool.Close()
slavePool = simpleredis.NewConnectionPoolHost("redis-slave:6379")
defer slavePool.Close()
r := mux.NewRouter()
r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)