Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active January 18, 2019 01:58
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save peterhellberg/9450839 to your computer and use it in GitHub Desktop.
Save peterhellberg/9450839 to your computer and use it in GitHub Desktop.
A tiny example API written in Go using Martini and Redigo
package main
import (
"flag"
"fmt"
"net/http"
"github.com/codegangsta/martini"
"github.com/garyburd/redigo/redis"
"github.com/martini-contrib/render"
)
var (
redisAddress = flag.String("redis-address", ":6379", "Address to the Redis server")
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis")
)
func main() {
martini.Env = martini.Prod
flag.Parse()
redisPool := redis.NewPool(func() (redis.Conn, error) {
c, err := redis.Dial("tcp", *redisAddress)
if err != nil {
return nil, err
}
return c, err
}, *maxConnections)
defer redisPool.Close()
m := martini.Classic()
m.Map(redisPool)
m.Use(render.Renderer())
m.Get("/", func() string {
return "Hello from Martini!"
})
m.Get("/set/:key", func(r render.Render, pool *redis.Pool, params martini.Params, req *http.Request) {
key := params["key"]
value := req.URL.Query().Get("value")
c := pool.Get()
defer c.Close()
status, err := c.Do("SET", key, value)
if err != nil {
message := fmt.Sprintf("Could not SET %s:%s", key, value)
r.JSON(400, map[string]interface{}{
"status": "ERR",
"message": message})
} else {
r.JSON(200, map[string]interface{}{
"status": status})
}
})
m.Get("/get/:key", func(r render.Render, pool *redis.Pool, params martini.Params) {
key := params["key"]
c := pool.Get()
defer c.Close()
value, err := redis.String(c.Do("GET", key))
if err != nil {
message := fmt.Sprintf("Could not GET %s", key)
r.JSON(400, map[string]interface{}{
"status": "ERR",
"message": message})
} else {
r.JSON(200, map[string]interface{}{
"status": "OK",
"value": value})
}
})
m.Run()
}
@peterhellberg
Copy link
Author

Now I just need to figure out how to inject the Redis connection pool into a Martini middleware :)

Copy link

ghost commented Mar 9, 2014

I think you need

   defer c.Close() 

after the pool Get() calls.

@peterhellberg
Copy link
Author

@monamousa Yes, thank you :)

@rastasheep
Copy link

@peterhellberg You can inject stuff into Martini with m.Map() https://github.com/codegangsta/martini#services

@peterhellberg
Copy link
Author

@rastasheep Thank you. I probably want to inject the pool and then retrieve the connection like I currently do, especially since:

Go's reflection cannot tell that you are trying to map redis.Conn as an interface, that is where you have to use the MapTo method. – Jeremy Saenz

@peterhellberg
Copy link
Author

Example on how to create your own little Martini middleware:
Go Advent Day 11 - Build a Christmas List with Martini - Hooking up MongoDB

@ccraig
Copy link

ccraig commented Apr 12, 2014

Thanks for this! I too am trying to figure out how to use MapTo with redigo. I'm still new to Go so maybe that's why I'm having difficulties trying to make it work with MapTo.

@agrison
Copy link

agrison commented Apr 29, 2014

You can just use m.Map(redisPool) to map the redis pool into your handlers and then just call redisPool.Get() to retrieve a connection, something like this:

func getFoo(render render.Render, pool *redis.Pool) {
    v, err := redis.Values(redisPool.Get().Do("HGETALL", "foo")) // or any redis command
    if err != nil {
        panic(err)
    }
    render.JSON(200, &v)
}

I too cannot map directly a *redis.Conn (it is backed as a *redis.conn which is not exported) into a handler, I have to use a connection pool, which is after all not a bad thing ;-)

@peterhellberg
Copy link
Author

@agrison Thank you, I’ve now updated the example.

@dougbarrett
Copy link

Thank you for the example! I have it working saving some MySQL results, and I've made a middleware handler that seems to work fine:

func SetupRedis() *redis.Pool {
    redisPool := redis.NewPool(func() (redis.Conn, error) {
        c, err := redis.Dial("tcp", *redisAddress)

        PanicIf(err)

        return c, err
    }, *maxConnections)

    return redisPool
}

but when I have defer redisPool.Close() in the middleware function, it won't run redis commands in the controllers because the connection pool closes. When I remove defer redisPool.Close() it all works fine, doing some local tests looks like it's reusing the connection as it should without using defer redisPool.Close(), so I'm not sure if this is really an issue or not, any thoughts on this?

@dougbarrett
Copy link

Disregard that last comment, here's what I did that keeps the main func a bit cleaner.

func SetupRedis() *redis.Pool {
    return redis.NewPool(func() (redis.Conn, error) {
        c, err := redis.Dial("tcp", *redisAddress)

        PanicIf(err)

        return c, err
    }, *maxConnections)
}

func main() {
        ...
    redisPool := SetupRedis()
    defer redisPool.Close()

    m.Map(redisPool)
        ...
}

@xeoncross
Copy link

Use the struct instead of the NewPool function.

NewPool creates a new pool. This function is deprecated. Applications should initialize the Pool fields directly as shown in example.

https://github.com/garyburd/redigo/blob/master/redis/pool.go#L89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment