healthcheck

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2019 License: MIT Imports: 5 Imported by: 0

README

go-grpc-healthcheck

Build Status Go Report Card

Simple implementation of gRPC Health Checking Protocol.

How to use

package main

import (
	"errors"
	"log"
	"net"

	healthcheck "github.com/allisson/go-grpc-healthcheck"
	"google.golang.org/grpc"
	healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

// dumbChecker implements healthcheck.Checker interface
type dumbChecker struct {
	fail bool
}

func (d *dumbChecker) Check() error {
	d.fail = !d.fail
	if d.fail {
		return errors.New("Fail")
	}
	return nil
}

func main() {
	checker := dumbChecker{}
	healthcheckServer := healthcheck.NewServer()
	healthcheckServer.AddChecker("dumb-checker", &checker)
	listener, err := net.Listen("tcp", ":50051")
	if err != nil {
		log.Fatal("failed-to-listen-tcp-port")
	}
	grpcServer := grpc.NewServer()
	healthpb.RegisterHealthServer(grpcServer, &healthcheckServer)
	if err := grpcServer.Serve(listener); err != nil {
		log.Fatal("failed-to-serve-grpc-requests")
	}
}
go run main.go
grpc_health_probe-linux-amd64 -addr=localhost:50051
service unhealthy (responded with "NOT_SERVING")
grpc_health_probe-linux-amd64 -addr=localhost:50051
status: SERVING
grpc_health_probe-linux-amd64 -addr=localhost:50051 -service "not-found-checker"
error: health rpc failed: rpc error: code = NotFound desc = unknown service

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker interface {
	Check() error
}

Checker is an interface that handles health check status

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a implementation of healthpb.HealthServer

func NewServer

func NewServer() Server

NewServer returns a Server with initialized checkers maps

func (*Server) AddChecker

func (s *Server) AddChecker(name string, checker Checker)

AddChecker includes a new checker on the checkers maps

func (*Server) Check

Check is a implementation of healthpb.HealthServer.Check

func (*Server) Watch

Watch is a implementation of healthpb.HealthServer.Watch

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL