Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

some work towards finishing & shipping custom bin IDs #128

Merged
merged 3 commits into from
Jul 1, 2015
Merged
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
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"encoding/json"
"log"
"os"
"time"
)

const (
// Path to the config file
configFile = "./config.json"
// Requests per second
rateLimit = 1
)

// Config holds configuration values read in from the config file
Expand All @@ -23,6 +22,7 @@ type Config struct {
NameVals string
NameLength int
RateLimit int
BinExpires time.Duration
}

// loadConfig reads configuration values from the config file
Expand All @@ -39,6 +39,7 @@ func loadConfig() *Config {
log.Fatal(err)
}

conf.RateLimit = rateLimit
conf.BinExpires = conf.BinExpires * time.Hour

return &conf
}
4 changes: 3 additions & 1 deletion config.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"RedisPass": "",
"RedisDB": 0,
"NameVals": "023456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ",
"NameLength": 10
"NameLength": 10,
"RateLimit": 1,
"BinExpires": 48
}
2 changes: 1 addition & 1 deletion geobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net/http"
"runtime"

"github.com/go-redis/redis"
redis "gopkg.in/redis.v1"
)

// some read-only global vars
Expand Down
3 changes: 2 additions & 1 deletion geobinserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"time"

"github.com/bmizerany/assert"
"github.com/go-redis/redis"
redis "gopkg.in/redis.v1"
)

var testConf = &Config{
Expand All @@ -27,6 +27,7 @@ var testConf = &Config{
"023456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ",
10,
999,
48 * time.Hour,
}

type MockRedis struct {
Expand Down
19 changes: 15 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strings"
"time"

"github.com/go-redis/redis"
"github.com/nu7hatch/gouuid"
redis "gopkg.in/redis.v1"
)

func (gb *geobinServer) createBin(n string, w http.ResponseWriter) (time.Time, error) {
Expand All @@ -25,16 +25,24 @@ func (gb *geobinServer) createBin(n string, w http.ResponseWriter) (time.Time, e
}

// Set expiration
d := 48 * time.Hour
if _, err = gb.Expire(n, d); err != nil {
log.Println("Failure to set EXPIRE for", n, err)
d := gb.conf.BinExpires
if err = gb.setBinExpires(n, d); err != nil {
http.Error(w, "Could not generate new Geobin!", http.StatusInternalServerError)
return t, err
}

return t.Add(d), nil
}

// setBinExpires sets the redis EXPIRE key for the given bin id.
func (gb *geobinServer) setBinExpires(n string, d time.Duration) error {
if _, err := gb.Expire(n, d); err != nil {
log.Println("Failure to set EXPIRE for", n, err)
return err
}
return nil
}

// createHandler handles requests to /api/1/create. It creates a randomly generated bin_id,
// creates an entry in redis for it, with a 48 hour expiration time and writes a json object
// to the response with the following structure:
Expand Down Expand Up @@ -157,6 +165,9 @@ func (gb *geobinServer) binHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Failure to ZADD to", name, err)
}

// reset the bin expiry time to 48 hours from now
gb.setBinExpires(name, gb.conf.BinExpires)

if _, err = gb.Publish(name, string(encoded)); err != nil {
log.Println("Failure to PUBLISH to", name, err)
}
Expand Down
2 changes: 1 addition & 1 deletion rediswrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"time"

"github.com/go-redis/redis"
redis "gopkg.in/redis.v1"
)

// mock our use of redis pubsub for modularity/testing purposes
Expand Down