Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Healthcheck. Readiness #31

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}()
}