Skip to content

Commit

Permalink
feat(plugin): add a type parameter for the health check plugin (#286)
Browse files Browse the repository at this point in the history
* feat(plugin): add a type parameter for the health check plugin

* feat(plugin): HealthCheckerWithType

* feat(plugin): fix ci

* feat(plugin): remove NewCheckerWithType
  • Loading branch information
Xinzhao Xu authored and caicloud-bot committed Aug 23, 2019
1 parent 4b95683 commit 854a462
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 5 deletions.
89 changes: 89 additions & 0 deletions examples/getting-started/healthcheck/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2018 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"

"github.com/caicloud/nirvana"
"github.com/caicloud/nirvana/config"
"github.com/caicloud/nirvana/definition"
"github.com/caicloud/nirvana/errors"
"github.com/caicloud/nirvana/log"
"github.com/caicloud/nirvana/plugins/healthcheck"
)

var echo = definition.Descriptor{
Path: "/echo",
Description: "Echo API",
Definitions: []definition.Definition{
{
Method: definition.Get,
Function: Echo,
Consumes: []string{definition.MIMEAll},
Produces: []string{definition.MIMEJSON},
Parameters: []definition.Parameter{
{
Source: definition.Query,
Name: "msg",
Description: "Corresponding to the second parameter",
},
},
Results: []definition.Result{
{
Destination: definition.Data,
Description: "Corresponding to the first result",
},
{
Destination: definition.Error,
Description: "Corresponding to the second result",
},
},
},
},
}

// API function.
func Echo(ctx context.Context, msg string) (string, error) {
return msg, nil
}

func main() {
cmd := config.NewDefaultNirvanaCommand()
cfg := nirvana.NewDefaultConfig()
cfg.Configure(
nirvana.Descriptor(echo),
healthcheck.CheckerWithType(func(ctx context.Context, checkType string) error {
switch checkType {
case healthcheck.LivenessCheck:
// do something
return nil
case healthcheck.ReadinessCheck:
// do something
return nil
default:
return errors.BadRequest.Build("error", "unknown type ${type}").Error(checkType)
}
}),
// healthcheck.Checker(func(ctx context.Context) error {
// return nil
// }),
)
if err := cmd.ExecuteWithConfig(cfg); err != nil {
log.Fatal(err)
}
}
49 changes: 44 additions & 5 deletions plugins/healthcheck/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,29 @@ func defaultHealthChecker(ctx context.Context) error {
return nil
}

// HealthCheckerWithType checks if current server is healthy.
// The `checkType` parameter indicates the type of health check, such as liveness or readiness.
type HealthCheckerWithType func(ctx context.Context, checkType string) error

func defaultHealthCheckerWithType(ctx context.Context, checkType string) error {
return nil
}

const (
// LivenessCheck represents liveness check.
LivenessCheck = "liveness"
// ReadinessCheck represents readiness check.
ReadinessCheck = "readiness"
)

// ExternalConfigName is the external config name of health check.
const ExternalConfigName = "healthcheck"

// config is healthcheck config.
type config struct {
path string
checker HealthChecker
path string
checker HealthChecker
checkerWithType HealthCheckerWithType
}

type healthcheckInstaller struct{}
Expand All @@ -55,14 +71,24 @@ func (i *healthcheckInstaller) Name() string {
func (i *healthcheckInstaller) Install(builder service.Builder, cfg *nirvana.Config) error {
var err error
wrapper(cfg, func(c *config) {
var parameters []definition.Parameter
var function interface{} = c.checker
if c.checkerWithType != nil {
parameters = []definition.Parameter{
definition.QueryParameterFor("type", "the type of health check"),
}
function = c.checkerWithType
}

err = builder.AddDescriptor(definition.Descriptor{
Path: c.path,
Consumes: []string{definition.MIMEAll},
Produces: []string{definition.MIMEAll},
Definitions: []definition.Definition{{
Method: definition.Get,
Results: []definition.Result{definition.ErrorResult()},
Function: c.checker,
Method: definition.Get,
Results: []definition.Result{definition.ErrorResult()},
Parameters: parameters,
Function: function,
}},
})
})
Expand Down Expand Up @@ -108,6 +134,19 @@ func Checker(checker HealthChecker) nirvana.Configurer {
}
}

// CheckerWithType returns a configurer to set health checker with a type parameter.
func CheckerWithType(checker HealthCheckerWithType) nirvana.Configurer {
if checker == nil {
checker = defaultHealthCheckerWithType
}
return func(c *nirvana.Config) error {
wrapper(c, func(c *config) {
c.checkerWithType = checker
})
return nil
}
}

func wrapper(c *nirvana.Config, f func(c *config)) {
conf := c.Config(ExternalConfigName)
var cfg *config
Expand Down

0 comments on commit 854a462

Please sign in to comment.