Skip to content

Commit

Permalink
Merge pull request #45 from lalamove/feature/max-watcher-panics
Browse files Browse the repository at this point in the history
Add recovery on panics in watcher and add a MaxWatcherPanics setting
  • Loading branch information
francoispqt authored Apr 2, 2019
2 parents 5d5c2d3 + 99b7729 commit 816a5a4
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 63 deletions.
84 changes: 39 additions & 45 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@

[[constraint]]
name = "github.com/lalamove/nui"
version = "~0.1.0"
version = "~0.2.0"
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type Config struct {
Logger nlogger.Provider
// Metrics sets whether a konfig.Store should record metrics for config loaders
Metrics bool
// MaxWatcherPanics is the maximum number of times to restart a watcher when it panics, default is 0.
MaxWatcherPanics int
}

// Store is the interface
Expand Down
11 changes: 11 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func TestConfigWatcherLoader(t *testing.T) {

gomock.InOrder(
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("foo"),
l.EXPECT().Load(Values{}).Return(nil),
)

Expand All @@ -163,6 +164,7 @@ func TestConfigWatcherLoader(t *testing.T) {

gomock.InOrder(
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().Load(Values{}).Return(nil),
)

Expand All @@ -186,8 +188,11 @@ func TestConfigWatcherLoader(t *testing.T) {

gomock.InOrder(
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().StopOnFailure().Return(false),
)

Expand All @@ -214,8 +219,11 @@ func TestConfigWatcherLoader(t *testing.T) {
gomock.InOrder(
l.EXPECT().Load(Values{}).Return(nil),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().StopOnFailure().Return(true),
)
wl.EXPECT().Start().Times(1).Return(nil)
Expand Down Expand Up @@ -263,8 +271,11 @@ func TestConfigWatcherLoader(t *testing.T) {
gomock.InOrder(
l.EXPECT().Load(Values{}).Return(nil),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().Load(Values{}).Return(errors.New("")),
l.EXPECT().Name().Return("l"),
l.EXPECT().StopOnFailure().Return(true),
)
wl2.EXPECT().Start().Times(1).Return(nil)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/grpc-ecosystem/grpc-gateway v1.8.1 // indirect
github.com/hashicorp/consul v1.4.2
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-gcp-common v0.0.0-20180425173946-763e39302965 // indirect
github.com/hashicorp/go-hclog v0.8.0 // indirect
github.com/hashicorp/go-memdb v0.0.0-20181108192425-032f93b25bec // indirect
Expand All @@ -82,7 +83,7 @@ require (
github.com/jefferai/jsonx v1.0.0 // indirect
github.com/jinzhu/copier v0.0.0-20180308034124-7e38e58719c3
github.com/keybase/go-crypto v0.0.0-20181127160227-255a5089e85a // indirect
github.com/lalamove/nui v0.1.0
github.com/lalamove/nui v0.2.0
github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b // indirect
github.com/michaelklishin/rabbit-hole v1.5.0 // indirect
github.com/micro/go-config v1.0.0
Expand All @@ -98,6 +99,7 @@ require (
github.com/ory/dockertest v3.3.4+incompatible // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pierrec/lz4 v0.0.0-20181005164709-635575b42742 // indirect
github.com/pkg/errors v0.8.1
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/pquerna/otp v1.1.0 // indirect
github.com/prometheus/client_golang v0.9.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/U
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6KdvN3Gig=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-gcp-common v0.0.0-20180425173946-763e39302965 h1:CRWAJ7atEOq9OfAa5rBKvL4wFNbUrPil8hN1knLuEG0=
github.com/hashicorp/go-gcp-common v0.0.0-20180425173946-763e39302965/go.mod h1:LNbios2fdMAuLA1dsYUvUcoCYIfywcCEK8/ooaWjoOA=
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
Expand Down Expand Up @@ -313,6 +315,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lalamove/nui v0.1.0 h1:dG/KfB4IoFejTUuOZ+fR+f98p9V1HVBXQ6LYWtdT7NM=
github.com/lalamove/nui v0.1.0/go.mod h1:IMgw/td9JHjoU1dpHjGj6o1i7bSeG/UO1/DODuPdz+o=
github.com/lalamove/nui v0.2.0 h1:IG6BxqLcIvMB2kdw0kBfm8ZKxlAmedJWgChZXdvIYso=
github.com/lalamove/nui v0.2.0/go.mod h1:IMgw/td9JHjoU1dpHjGj6o1i7bSeG/UO1/DODuPdz+o=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
Expand Down
45 changes: 45 additions & 0 deletions konfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Package konfig provides a composable, observable and performant
// config handling for Go.
// Written for larger distributed systems where you may have plenty of
// configuration sources - it allows you to compose configurations from
// multiple sources with reload hooks making it simple to build apps that live
// in a highly dynamic environment.
//
// Konfig is built around 4 small interfaces: Loader, Watcher, Parser, Closer
//
// Get started:
// var configFiles = []klfile.File{
// {
// Path: "./config.json",
// Parser: kpjson.Parser,
// },
// }
//
// func init() {
// konfig.Init(konfig.DefaultConfig())
// }
//
// func main() {
// // load from json file with a file wacher
// konfig.RegisterLoaderWatcher(
// klfile.New(&klfile.Config{
// Files: configFiles,
// Watch: true,
// }),
// // optionally you can pass config hooks to run when a file is changed
// func(c konfig.Store) error {
// return nil
// },
// )
//
// // Load and start watching
// if err := konfig.LoadWatch(); err != nil {
// log.Fatal(err)
// }
//
// // retrieve value from config file
// konfig.Bool("debug")
// }
//
//
package konfig
Loading

0 comments on commit 816a5a4

Please sign in to comment.