Skip to content

Commit eb44a64

Browse files
authored
☕️ Prepare new release v0.3.0 (#1)
- Add more options + TLSConfig parse from `redis.options` - Move to UniversalClient (interface, be careful) - Update to Helium v0.11.11
1 parent 77472e2 commit eb44a64

7 files changed

+356
-47
lines changed

go.mod

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module github.com/go-helium/redis
22

33
require (
4-
github.com/go-redis/redis v6.15.1+incompatible
5-
github.com/im-kulikov/helium v0.10.1
4+
github.com/go-redis/redis v6.15.2+incompatible
5+
github.com/im-kulikov/helium v0.11.11
66
github.com/pkg/errors v0.8.1
77
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa
8-
github.com/spf13/viper v1.3.1
8+
github.com/spf13/viper v1.3.2
9+
github.com/stretchr/testify v1.3.0
10+
go.uber.org/zap v1.10.0
911
)

go.sum

+22
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
2323
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
2424
github.com/go-redis/redis v6.15.1+incompatible h1:BZ9s4/vHrIqwOb0OPtTQ5uABxETJ3NRuUNoSUurnkew=
2525
github.com/go-redis/redis v6.15.1+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
26+
github.com/go-redis/redis v6.15.2+incompatible h1:9SpNVG76gr6InJGxoZ6IuuxaCOQwDAhzyXg+Bs+0Sb4=
27+
github.com/go-redis/redis v6.15.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
2628
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
2729
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
2830
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
2931
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
3032
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
3133
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
3234
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
35+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3336
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
3437
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
3538
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
@@ -39,6 +42,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
3942
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
4043
github.com/im-kulikov/helium v0.10.1 h1:rZX5Xwq3UpJV0y2pOHzkRDuEMm3vUPnjW5FLPsj/rMk=
4144
github.com/im-kulikov/helium v0.10.1/go.mod h1:7biJn9yXenAH3vYe+58wyFS7jm89bjI0EF6C5XgPXgA=
45+
github.com/im-kulikov/helium v0.11.11 h1:lysZLdP+GeSjn0ZLWwwihny39+Ne6V76rRSW01l+yWA=
46+
github.com/im-kulikov/helium v0.11.11/go.mod h1:0tRW/abe6UL1colwVxxD63sLjy1O/CPQlKaJ1r8XxoU=
4247
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
4348
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
4449
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
@@ -59,8 +64,10 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
5964
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
6065
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
6166
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
67+
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
6268
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
6369
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
70+
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
6471
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
6572
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
6673
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -74,17 +81,20 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:
7481
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
7582
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
7683
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
84+
github.com/prometheus/common v0.3.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
7785
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
7886
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
7987
github.com/prometheus/procfs v0.0.0-20190129233650-316cf8ccfec5/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
8088
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
8189
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
90+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
8291
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
8392
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PXuP99tXNrhbq2BaPz9B+jNAvH1JPQQpG/9GCXY=
8493
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
8594
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
8695
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa h1:E+gaaifzi2xF65PbDmuKI3PhLWY6G5opMLniFq8vmXA=
8796
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa/go.mod h1:2RVY1rIf+2J2o/IM9+vPq9RzmHDSseB7FoXiSNIUsoU=
97+
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
8898
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
8999
github.com/spf13/afero v1.2.1 h1:qgMbHoJbPbw579P+1zVY+6n4nIFuIchaIjzZ/I/Yq8M=
90100
github.com/spf13/afero v1.2.1/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
@@ -96,6 +106,8 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
96106
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
97107
github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38=
98108
github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
109+
github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
110+
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
99111
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
100112
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
101113
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -109,18 +121,22 @@ go.uber.org/dig v1.7.0 h1:E5/L92iQTNJTjfgJF2KgU+/JpMaiuvK2DHLBj0+kSZk=
109121
go.uber.org/dig v1.7.0/go.mod h1:z+dSd2TP9Usi48jL8M3v63iSBVkiwtVyMKxMZYYauPg=
110122
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
111123
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
124+
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
112125
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
113126
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
127+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
114128
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
115129
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
116130
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
131+
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
117132
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
118133
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
119134
golang.org/x/net v0.0.0-20181106065722-10aee1819953/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
120135
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
121136
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
122137
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 h1:ulvT7fqt0yHWzpJwI57MezWnYDVpCAYBVuYst/L+fAY=
123138
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
139+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
124140
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
125141
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
126142
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -133,15 +149,20 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
133149
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
134150
golang.org/x/sys v0.0.0-20190203050204-7ae0202eb74c h1:YeMXU0KQqExdpG959DFhAhfpY8myIsnfqj8lhNFRzzE=
135151
golang.org/x/sys v0.0.0-20190203050204-7ae0202eb74c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
152+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
153+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
154+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
136155
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
137156
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
138157
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
139158
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
159+
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
140160
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
141161
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
142162
google.golang.org/genproto v0.0.0-20190201180003-4b09977fb922/go.mod h1:L3J43x8/uS+qIUoksaLKe6OS3nUKxOKuIFz1sl2/jx4=
143163
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
144164
google.golang.org/grpc v1.18.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
165+
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
145166
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
146167
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
147168
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
@@ -154,3 +175,4 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
154175
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
155176
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
156177
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
178+
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

redis.go

+82-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
package redis
22

33
import (
4+
"strconv"
5+
46
"github.com/go-redis/redis"
57
"github.com/im-kulikov/helium/module"
6-
"github.com/pkg/errors"
78
"github.com/spf13/viper"
89
)
910

1011
type (
1112
// Config alias
12-
Config = redis.Options
13+
Config = redis.UniversalOptions
1314

1415
// Client alias
15-
Client = redis.Client
16+
Client = redis.UniversalClient
17+
18+
// Error is constant error
19+
Error string
20+
)
21+
22+
const (
23+
// ErrEmptyConfig when given empty options
24+
ErrEmptyConfig = Error("missing redis config key")
25+
// ErrEmptyAddresses when given empty addresses
26+
ErrEmptyAddresses = Error("missing addresses")
27+
// ErrPemParse when couldn't parse pem in sslrootcert
28+
ErrPemParse = Error("couldn't parse pem in sslrootcert")
29+
// ErrEmptyLogger when logger not initialized
30+
ErrEmptyLogger = Error("database empty logger")
31+
// ErrSSLKeyHasWorldPermissions when pk permissions no u=rw (0600) or less
32+
ErrSSLKeyHasWorldPermissions = Error("private key file has group or world access. Permissions should be u=rw (0600) or less")
33+
34+
errUnsupportedSSLMode = `unsupported sslmode %q; only "require" (default), "verify-full", "verify-ca", and "disable" supported`
1635
)
1736

1837
var (
@@ -21,36 +40,89 @@ var (
2140
{Constructor: NewDefaultConfig},
2241
{Constructor: NewConnection},
2342
}
24-
// ErrEmptyConfig when given empty options
25-
ErrEmptyConfig = errors.New("missing redis config key")
2643
)
2744

45+
// Error represented in string
46+
func (e Error) Error() string { return string(e) }
47+
2848
// NewDefaultConfig for connection
2949
func NewDefaultConfig(v *viper.Viper) (*Config, error) {
30-
if !v.IsSet("redis.address") {
50+
if !v.IsSet("redis.address") && !v.IsSet("redis.addresses") && !v.IsSet("redis.addresses_0") {
3151
return nil, ErrEmptyConfig
3252
}
3353

54+
var addresses []string
55+
56+
if addr := v.GetString("redis.address"); addr != "" {
57+
addresses = append(addresses, addr)
58+
} else if addresses = fetchAddresses(v); len(addresses) == 0 {
59+
return nil, ErrEmptyAddresses
60+
}
61+
62+
v.SetDefault("redis.options.sslmode", "disable")
63+
64+
// re-fetch by full key
65+
options := v.GetStringMapString("redis.options")
66+
if len(options) > 0 {
67+
for opt := range options {
68+
options[opt] = v.GetString("redis.options." + opt)
69+
}
70+
}
71+
72+
tlsConfig, err := ssl(options)
73+
if err != nil {
74+
return nil, err
75+
}
76+
3477
return &Config{
35-
Addr: v.GetString("redis.address"),
36-
Password: v.GetString("redis.password"),
78+
Addrs: addresses,
3779
DB: v.GetInt("redis.db"),
80+
Password: v.GetString("redis.password"),
3881
MaxRetries: v.GetInt("redis.max_retries"),
3982
MinRetryBackoff: v.GetDuration("redis.min_retry_backoff"),
4083
MaxRetryBackoff: v.GetDuration("redis.max_retry_backoff"),
4184
DialTimeout: v.GetDuration("redis.dial_timeout"),
4285
ReadTimeout: v.GetDuration("redis.read_timeout"),
4386
WriteTimeout: v.GetDuration("redis.write_timeout"),
4487
PoolSize: v.GetInt("redis.pool_size"),
88+
MinIdleConns: v.GetInt("redis.min_idle_cons"),
89+
MaxConnAge: v.GetDuration("redis.max_con_age"),
4590
PoolTimeout: v.GetDuration("redis.pool_timeout"),
4691
IdleTimeout: v.GetDuration("redis.idle_timeout"),
4792
IdleCheckFrequency: v.GetDuration("redis.idle_check_frequency"),
93+
MaxRedirects: v.GetInt("redis.max_redirects"),
94+
ReadOnly: v.GetBool("redis.read_only"),
95+
RouteByLatency: v.GetBool("redis.router_by_latency"),
96+
RouteRandomly: v.GetBool("redis.router_randomly"),
97+
MasterName: v.GetString("redis.master_name"),
98+
TLSConfig: tlsConfig,
4899
}, nil
49100
}
50101

102+
func fetchAddresses(v *viper.Viper) []string {
103+
var (
104+
addresses []string
105+
)
106+
107+
for i := 0; ; i++ {
108+
addr := v.GetString("redis.addresses_" + strconv.Itoa(i))
109+
if addr == "" {
110+
break
111+
}
112+
113+
addresses = append(addresses, addr)
114+
}
115+
116+
if len(addresses) == 0 {
117+
addresses = v.GetStringSlice("redis.addresses")
118+
}
119+
120+
return addresses
121+
}
122+
51123
// NewConnection of redis client
52-
func NewConnection(opts *Config) (cache *Client, err error) {
53-
cache = redis.NewClient(opts)
124+
func NewConnection(opts *Config) (cache Client, err error) {
125+
cache = redis.NewUniversalClient(opts)
54126

55127
if _, err = cache.Ping().Result(); err != nil {
56128
return nil, err

redis_test.go

+67-34
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,81 @@ import (
44
"strings"
55
"testing"
66

7-
. "github.com/smartystreets/goconvey/convey"
87
"github.com/spf13/viper"
8+
"github.com/stretchr/testify/require"
99
)
1010

11+
func prepare() *viper.Viper {
12+
v := viper.New()
13+
v.AutomaticEnv()
14+
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
15+
return v
16+
}
17+
1118
func TestRedis(t *testing.T) {
12-
Convey("Redis test suite", t, func() {
13-
v := viper.New()
14-
v.AutomaticEnv()
15-
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
16-
v.SetDefault("redis.address", "localhost:6379")
19+
_ = ErrEmptyAddresses.Error()
20+
21+
t.Run("Config", func(t *testing.T) {
22+
t.Run("should return error when config file is empty", func(t *testing.T) {
23+
cfg, err := NewDefaultConfig(viper.New())
24+
require.Empty(t, cfg)
25+
require.Error(t, err)
26+
})
27+
28+
t.Run("should return config when address exists", func(t *testing.T) {
29+
v := prepare()
30+
v.SetDefault("redis.address", "localhost:6379")
31+
cfg, err := NewDefaultConfig(v)
32+
require.NoError(t, err)
33+
require.NotEmpty(t, cfg)
34+
})
35+
36+
t.Run("should return config when address exists", func(t *testing.T) {
37+
v := prepare()
38+
v.SetDefault("redis.addresses_0", "localhost:6379")
39+
cfg, err := NewDefaultConfig(v)
40+
require.NoError(t, err)
41+
require.NotEmpty(t, cfg)
42+
})
1743

18-
Convey("Config", func() {
19-
Convey("should return error when config file is empty", func() {
20-
cfg, err := NewDefaultConfig(viper.New())
21-
So(cfg, ShouldBeNil)
22-
So(err, ShouldNotBeNil)
23-
})
24-
25-
Convey("should return config when address exists", func() {
26-
cfg, err := NewDefaultConfig(v)
27-
So(err, ShouldBeNil)
28-
So(cfg, ShouldNotBeNil)
29-
})
44+
t.Run("should fail on empty address 1", func(t *testing.T) {
45+
v := prepare()
46+
v.SetDefault("redis.addresses", "")
47+
cfg, err := NewDefaultConfig(v)
48+
require.Error(t, err)
49+
require.Empty(t, cfg)
3050
})
3151

32-
Convey("Connection", func() {
52+
t.Run("should fail on empty address 2", func(t *testing.T) {
53+
v := prepare()
54+
v.SetDefault("redis.addresses_0", "")
3355
cfg, err := NewDefaultConfig(v)
34-
So(err, ShouldBeNil)
35-
So(cfg, ShouldNotBeNil)
36-
37-
Convey("should create redis client", func() {
38-
cli, err := NewConnection(cfg)
39-
So(err, ShouldBeNil)
40-
So(cli, ShouldNotBeNil)
41-
})
42-
43-
Convey("should return error when address incorrect", func() {
44-
cfg.Addr = "foo"
45-
cli, err := NewConnection(cfg)
46-
So(cli, ShouldBeNil)
47-
So(err, ShouldNotBeNil)
48-
})
56+
require.Error(t, err)
57+
require.Empty(t, cfg)
58+
})
59+
})
60+
61+
t.Run("Connection", func(t *testing.T) {
62+
v := prepare()
63+
v.SetDefault("redis.address", "localhost:6379")
64+
cfg, err := NewDefaultConfig(v)
65+
require.NoError(t, err)
66+
require.NotEmpty(t, cfg)
67+
68+
t.Run("should create redis client", func(t *testing.T) {
69+
cli, err := NewConnection(cfg)
70+
require.NoError(t, err)
71+
require.NotNil(t, cli)
72+
73+
cli.Ping()
74+
//
75+
})
76+
77+
t.Run("should return error when address incorrect", func(t *testing.T) {
78+
cfg.Addrs = []string{"foo"}
79+
cli, err := NewConnection(cfg)
80+
require.Nil(t, cli)
81+
require.Error(t, err)
4982
})
5083
})
5184
}

0 commit comments

Comments
 (0)