11package main
22
33import (
4+ "io/ioutil"
45 "log"
56 "net"
67 "os"
78 "os/exec"
89 "os/signal"
10+ "strings"
911 "syscall"
1012 "time"
1113
1214 "github.com/coreos/go-etcd/etcd"
1315)
1416
1517const (
16- timeout time.Duration = 10 * time .Second
17- ttl time.Duration = timeout * 2
18- redisWait time.Duration = 5 * time .Second
18+ timeout time.Duration = 10 * time .Second
19+ ttl time.Duration = timeout * 2
20+ redisWait time.Duration = 5 * time .Second
21+ redisConf string = "/app/redis.conf"
22+ ectdKeyNotFound int = 100
23+ defaultMemory string = "50mb"
1924)
2025
2126func main () {
@@ -28,6 +33,19 @@ func main() {
2833
2934 client := etcd .NewClient ([]string {"http://" + host + ":" + etcdPort })
3035
36+ var maxmemory string
37+ result , err := client .Get ("/deis/cache/maxmemory" , false , false )
38+ if err != nil {
39+ if e , ok := err .(* etcd.EtcdError ); ok && e .ErrorCode == ectdKeyNotFound {
40+ maxmemory = defaultMemory
41+ } else {
42+ log .Fatalln (err )
43+ }
44+ } else {
45+ maxmemory = result .Node .Key
46+ }
47+ replaceMaxmemoryInConfig (maxmemory )
48+
3149 go launchRedis ()
3250
3351 go publishService (client , host , etcdPath , externalPort , uint64 (ttl .Seconds ()))
@@ -38,8 +56,20 @@ func main() {
3856 <- exitChan
3957}
4058
59+ func replaceMaxmemoryInConfig (maxmemory string ) {
60+ input , err := ioutil .ReadFile (redisConf )
61+ if err != nil {
62+ log .Fatalln (err )
63+ }
64+ output := strings .Replace (string (input ), "# maxmemory <bytes>" , "maxmemory " + maxmemory , 1 )
65+ err = ioutil .WriteFile (redisConf , []byte (output ), 0644 )
66+ if err != nil {
67+ log .Fatalln (err )
68+ }
69+ }
70+
4171func launchRedis () {
42- cmd := exec .Command ("/app/bin/redis-server" , "/app/redis.conf" )
72+ cmd := exec .Command ("/app/bin/redis-server" , redisConf )
4373 cmd .Stdout = os .Stdout
4474 cmd .Stderr = os .Stderr
4575
0 commit comments