diff options
author | Valery Piashchynski <[email protected]> | 2021-04-06 15:15:01 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-04-06 15:15:01 +0300 |
commit | ad8db0ad8907fbc562123b41323b34ec6c0dec9f (patch) | |
tree | 036884e8f37d3c9abe965e487fe2eecef903ccf2 | |
parent | 4e468f900e82c344269be0129624cbfcde706e26 (diff) |
- Add new configuration option to the status plugin
-rw-r--r-- | plugins/http/plugin.go | 4 | ||||
-rw-r--r-- | plugins/status/config.go | 13 | ||||
-rw-r--r-- | plugins/status/plugin.go | 42 | ||||
-rwxr-xr-x | tests/plugins/config/config_test.go | 4 | ||||
-rwxr-xr-x | tests/plugins/config/plugin2.go | 2 | ||||
-rw-r--r-- | tests/plugins/status/plugin_test.go | 6 |
6 files changed, 51 insertions, 20 deletions
diff --git a/plugins/http/plugin.go b/plugins/http/plugin.go index 13a76329..86fcb329 100644 --- a/plugins/http/plugin.go +++ b/plugins/http/plugin.go @@ -405,7 +405,7 @@ func (s *Plugin) Status() status.Status { } // if there are no workers, threat this as error return status.Status{ - Code: http.StatusNoContent, + Code: http.StatusServiceUnavailable, } } @@ -423,7 +423,7 @@ func (s *Plugin) Ready() status.Status { } // if there are no workers, threat this as no content error return status.Status{ - Code: http.StatusNoContent, + Code: http.StatusServiceUnavailable, } } diff --git a/plugins/status/config.go b/plugins/status/config.go index 23a6ede2..f751898b 100644 --- a/plugins/status/config.go +++ b/plugins/status/config.go @@ -1,5 +1,18 @@ package status +import "net/http" + +// Config is the configuration reference for the Status plugin type Config struct { + // Address of the http server Address string + // Status code returned in case of fail, 503 by default + UnavailableStatusCode int `mapstructure:"unavailable_status_code"` +} + +// InitDefaults configuration options +func (c *Config) InitDefaults() { + if c.UnavailableStatusCode == 0 { + c.UnavailableStatusCode = http.StatusServiceUnavailable + } } diff --git a/plugins/status/plugin.go b/plugins/status/plugin.go index 693440bf..dc4e506d 100644 --- a/plugins/status/plugin.go +++ b/plugins/status/plugin.go @@ -37,10 +37,14 @@ func (c *Plugin) Init(log logger.Logger, cfg config.Configurer) error { return errors.E(op, errors.Disabled, err) } + // init defaults for the status plugin + c.cfg.InitDefaults() + c.readyRegistry = make(map[string]Readiness) c.statusRegistry = make(map[string]Checker) c.log = log + return nil } @@ -134,8 +138,8 @@ type Plugins struct { const template string = "Service: %s: Status: %d\n" -func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { - const op = errors.Op("checker_plugin_readiness_handler") +func (c *Plugin) healthHandler(ctx *fiber.Ctx) error { + const op = errors.Op("checker_plugin_health_handler") plugins := &Plugins{} err := ctx.QueryParser(plugins) if err != nil { @@ -144,16 +148,22 @@ func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { if len(plugins.Plugins) == 0 { ctx.Status(http.StatusOK) - _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: ready?plugin=plugin1&plugin=plugin2 \n") + _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: health?plugin=plugin1&plugin=plugin2 \n") return nil } // iterate over all provided plugins for i := 0; i < len(plugins.Plugins); i++ { // check if the plugin exists - if plugin, ok := c.readyRegistry[plugins.Plugins[i]]; ok { - st := plugin.Ready() - _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + if plugin, ok := c.statusRegistry[plugins.Plugins[i]]; ok { + st := plugin.Status() + if st.Code >= 500 { + // if there is 500 or 503 status code return immediately + ctx.Status(c.cfg.UnavailableStatusCode) + return nil + } else if st.Code >= 100 && st.Code <= 400 { + _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + } } else { _, _ = ctx.WriteString(fmt.Sprintf("Service: %s not found", plugins.Plugins[i])) } @@ -163,8 +173,10 @@ func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { return nil } -func (c *Plugin) healthHandler(ctx *fiber.Ctx) error { - const op = errors.Op("checker_plugin_health_handler") +// readinessHandler return 200OK if all plugins are ready to serve +// if one of the plugins return status from the 5xx range, the status for all query will be 503 +func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { + const op = errors.Op("checker_plugin_readiness_handler") plugins := &Plugins{} err := ctx.QueryParser(plugins) if err != nil { @@ -173,16 +185,22 @@ func (c *Plugin) healthHandler(ctx *fiber.Ctx) error { if len(plugins.Plugins) == 0 { ctx.Status(http.StatusOK) - _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: health?plugin=plugin1&plugin=plugin2 \n") + _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: ready?plugin=plugin1&plugin=plugin2 \n") return nil } // iterate over all provided plugins for i := 0; i < len(plugins.Plugins); i++ { // check if the plugin exists - if plugin, ok := c.statusRegistry[plugins.Plugins[i]]; ok { - st := plugin.Status() - _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + if plugin, ok := c.readyRegistry[plugins.Plugins[i]]; ok { + st := plugin.Ready() + if st.Code >= 500 { + // if there is 500 or 503 status code return immediately + ctx.Status(c.cfg.UnavailableStatusCode) + return nil + } else if st.Code >= 100 && st.Code <= 400 { + _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + } } else { _, _ = ctx.WriteString(fmt.Sprintf("Service: %s not found", plugins.Plugins[i])) } diff --git a/tests/plugins/config/config_test.go b/tests/plugins/config/config_test.go index 79de2f59..b6063cec 100755 --- a/tests/plugins/config/config_test.go +++ b/tests/plugins/config/config_test.go @@ -97,7 +97,7 @@ func TestConfigOverwriteValid(t *testing.T) { vp := &config.Viper{} vp.Path = "configs/.rr.yaml" vp.Prefix = "rr" - vp.Flags = []string{"rpc.listen=tcp://localhost:6061"} + vp.Flags = []string{"rpc.listen=tcp://localhost:36643"} err = container.RegisterAll( &logger.ZapLogger{}, @@ -143,7 +143,7 @@ func TestConfigEnvVariables(t *testing.T) { t.Fatal(err) } - err = os.Setenv("SUPER_RPC_ENV", "tcp://localhost:6061") + err = os.Setenv("SUPER_RPC_ENV", "tcp://localhost:36643") assert.NoError(t, err) vp := &config.Viper{} diff --git a/tests/plugins/config/plugin2.go b/tests/plugins/config/plugin2.go index 0fea9007..9639b170 100755 --- a/tests/plugins/config/plugin2.go +++ b/tests/plugins/config/plugin2.go @@ -37,7 +37,7 @@ func (f *Foo2) Serve() chan error { return errCh } - if allCfg.RPC.Listen != "tcp://localhost:6061" { + if allCfg.RPC.Listen != "tcp://localhost:36643" { errCh <- errors.E(op, errors.Str("RPC.Listen should be overwritten")) return errCh } diff --git a/tests/plugins/status/plugin_test.go b/tests/plugins/status/plugin_test.go index 85e07fcf..663f4ee3 100644 --- a/tests/plugins/status/plugin_test.go +++ b/tests/plugins/status/plugin_test.go @@ -368,8 +368,8 @@ func checkHTTPReadiness2(t *testing.T) { assert.NoError(t, err) b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) - assert.Equal(t, 200, r.StatusCode) - assert.Equal(t, resp2, string(b)) + assert.Equal(t, 503, r.StatusCode) + assert.Equal(t, "", string(b)) err = r.Body.Close() assert.NoError(t, err) @@ -384,5 +384,5 @@ func checkRPCReadiness(t *testing.T) { err = client.Call("status.Ready", "http", &st) assert.NoError(t, err) - assert.Equal(t, st.Code, 204) + assert.Equal(t, st.Code, 503) } |