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 /plugins | |
parent | 4e468f900e82c344269be0129624cbfcde706e26 (diff) |
- Add new configuration option to the status plugin
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/http/plugin.go | 4 | ||||
-rw-r--r-- | plugins/status/config.go | 13 | ||||
-rw-r--r-- | plugins/status/plugin.go | 42 |
3 files changed, 45 insertions, 14 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])) } |