Skip to content

Commit

Permalink
Merge pull request #12 from launchdarkly/dr/pprof
Browse files Browse the repository at this point in the history
Update go-client. Enhance Redis caching behavior
  • Loading branch information
drichelson authored Oct 26, 2016
2 parents 31b694d + c230312 commit 385d696
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions Godeps/_workspace/src/gopkg.in/launchdarkly/go-client.v2/redis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To set up LDR in this mode, provide a redis host and port, and supply a Redis ke
[redis]
host = "localhost"
port = 6379
ttl = 30000
localTtl = 30000

[main]
...
Expand All @@ -73,7 +73,7 @@ To set up LDR in this mode, provide a redis host and port, and supply a Redis ke
prefix = "ld:spree:test"
apiKey = "SPREE_TEST_API_KEY"

You can also configure an in-memory cache for the relay to use so that connections do not always hit redis. To do this, set the `ttl` parameter in your `redis` configuration section to a number (in milliseconds).
You can also configure an in-memory cache for the relay to use so that connections do not always hit redis. To do this, set the `localTtl` parameter in your `redis` configuration section to a number (in milliseconds).

If you're not using a load balancer in front of LDR, you can configure your SDKs to connect to Redis directly by setting `use_ldd` mode to `true` in your SDK, and connecting to Redis with the same host and port in your SDK configuration.

Expand Down
2 changes: 1 addition & 1 deletion deb-contents/etc/ld-relay.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ heartbeatIntervalSecs = 15
; [redis]
; host = "localhost"
; port = 6379
; ttl = 30000
; localTtl = 30000

; You can configure LDR to synchronize as many environments as you want,
; even across different projects. If using Redis to persist flag data,
Expand Down
47 changes: 27 additions & 20 deletions ld-relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ import (
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"regexp"
"strings"
"time"
)

var (
Debug *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
const (
defaultRedisLocalTtlMs = 30000
)

var VERSION = "DEV"

var uuidHeaderPattern = regexp.MustCompile(`^(?:api_key )?((?:[a-z]{3}-)?[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12})$`)

const defaultRedisTtlMs = 30000
var (
VERSION = "DEV"
Debug *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
uuidHeaderPattern = regexp.MustCompile(`^(?:api_key )?((?:[a-z]{3}-)?[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12})$`)
configFile string
)

type EnvConfig struct {
ApiKey string
Expand All @@ -47,9 +49,9 @@ type Config struct {
HeartbeatIntervalSecs int
}
Redis struct {
Host string
Port int
Ttl *int
Host string
Port int
LocalTtl *int
}
Environment map[string]*EnvConfig
}
Expand All @@ -58,8 +60,6 @@ type StatusEntry struct {
Status string `json:"status"`
}

var configFile string

func main() {

flag.StringVar(&configFile, "config", "/etc/ld-relay.conf", "configuration file location")
Expand All @@ -79,9 +79,9 @@ func main() {
os.Exit(1)
}

if c.Redis.Ttl == nil {
redisTtl := defaultRedisTtlMs
c.Redis.Ttl = &redisTtl
if c.Redis.LocalTtl == nil {
localTtl := defaultRedisLocalTtlMs
c.Redis.LocalTtl = &localTtl
}

publisher := eventsource.NewServer()
Expand All @@ -101,7 +101,8 @@ func main() {
go func(envName string, envConfig EnvConfig) {
var baseFeatureStore ld.FeatureStore
if c.Redis.Host != "" && c.Redis.Port != 0 {
baseFeatureStore = ld.NewRedisFeatureStore(c.Redis.Host, c.Redis.Port, envConfig.Prefix, time.Duration(*c.Redis.Ttl)*time.Millisecond, Info)
Info.Printf("Using Redis Feature Store: %s:%d with prefix: %s", c.Redis.Host, c.Redis.Port, envConfig.Prefix)
baseFeatureStore = ld.NewRedisFeatureStore(c.Redis.Host, c.Redis.Port, envConfig.Prefix, time.Duration(*c.Redis.LocalTtl)*time.Millisecond, Info)
} else {
baseFeatureStore = ld.NewInMemoryFeatureStore(Info)
}
Expand Down Expand Up @@ -189,7 +190,13 @@ func main() {

Info.Printf("Listening on port %d\n", c.Main.Port)

http.ListenAndServe(fmt.Sprintf(":%d", c.Main.Port), nil)
err = http.ListenAndServe(fmt.Sprintf(":%d", c.Main.Port), nil)
if err != nil {
if c.Main.ExitOnError {
Error.Fatalf("Error starting http listener on port: %d %s", c.Main.Port, err.Error())
}
Error.Printf("Error starting http listener on port: %d %s", c.Main.Port, err.Error())
}
}

func fetchAuthToken(req *http.Request) (string, error) {
Expand Down

0 comments on commit 385d696

Please sign in to comment.