Skip to content

Commit

Permalink
Merge pull request #31 from bavix/readiness-fix
Browse files Browse the repository at this point in the history
[2.0] Healthcheck. Readiness
  • Loading branch information
rez1dent3 authored Sep 9, 2023
2 parents 3c9b559 + b988378 commit 6557a7c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
31 changes: 30 additions & 1 deletion gripmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package main

import (
"bytes"
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/exec"
"os/signal"
"path"
"strings"
"syscall"
"time"

_ "github.com/bavix/gripmock/protogen"
"github.com/bavix/gripmock/stub"
Expand Down Expand Up @@ -47,8 +50,12 @@ func main() {
}
}

ctx := context.Background()

chReady := make(chan struct{})

// run admin stub server
stub.RunRestServer(stub.Options{
stub.RunRestServer(chReady, stub.Options{
StubPath: *stubPath,
Port: *adminport,
BindAddr: *adminBindAddr,
Expand Down Expand Up @@ -76,6 +83,28 @@ func main() {
// and run
run, runerr := runGrpcServer(output)

// This is a kind of crutch, but now there is no other solution.
//I have an idea to combine gripmock and grpcmock services into one, then this check will be easier to do.
// Checking the grpc port of the service. If the port appears, the service has started successfully.
go func() {
var d net.Dialer

for {
dialCtx, cancel := context.WithTimeout(ctx, time.Second)

conn, err := d.DialContext(dialCtx, "tcp", net.JoinHostPort(*grpcBindAddr, *grpcPort))
cancel()

if err == nil && conn != nil {
chReady <- struct{}{}

conn.Close()

break
}
}
}()

term := make(chan os.Signal, 1)
signal.Notify(term, syscall.SIGTERM, syscall.SIGINT)
select {
Expand Down
13 changes: 13 additions & 0 deletions internal/app/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type StubsServer struct {
convertor *yaml2json.Convertor
caser cases.Caser
clock *clock.Clock
ok bool
}

func NewRestServer(path string) (*StubsServer, error) {
Expand Down Expand Up @@ -54,11 +55,23 @@ type findStubPayload struct {
Data map[string]interface{} `json:"data"`
}

func (h *StubsServer) ServiceReady() {
h.ok = true
}

func (h *StubsServer) Liveness(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(rest.MessageOK{Message: "ok", Time: h.clock.Now()})
}

func (h *StubsServer) Readiness(w http.ResponseWriter, _ *http.Request) {
if !h.ok {
w.WriteHeader(http.StatusServiceUnavailable)

return
}

w.Header().Set("Content-Type", "application/json")

_ = json.NewEncoder(w).Encode(rest.MessageOK{Message: "ok", Time: h.clock.Now()})
}

Expand Down
11 changes: 10 additions & 1 deletion stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Options struct {

const DefaultPort = "4771"

func RunRestServer(opt Options) {
func RunRestServer(ch chan struct{}, opt Options) {
if opt.Port == "" {
opt.Port = DefaultPort
}
Expand All @@ -38,4 +38,13 @@ func RunRestServer(opt Options) {
err := http.ListenAndServe(addr, handler)
log.Fatal(err)
}()

go func() {
select {
case <-ch:
apiServer.ServiceReady()
}

log.Println("GRPC-service is ready to accept requests")
}()
}

0 comments on commit 6557a7c

Please sign in to comment.