Skip to content

Commit

Permalink
fix healtcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
lghinet committed Oct 15, 2021
1 parent efe1470 commit e0aeff3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
28 changes: 22 additions & 6 deletions cmd/rusid/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"flag"
"k8s.io/klog/v2"
"os"
"os/signal"
"rusi/internal/tracing"
grpc_api "rusi/pkg/api/runtime/grpc"
components_loader "rusi/pkg/custom-resource/components/loader"
Expand All @@ -16,7 +18,7 @@ import (
)

func main() {
mainCtx := context.Background()
mainCtx, cancel := context.WithCancel(context.Background())

//https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md
klog.InitFlags(nil)
Expand Down Expand Up @@ -62,20 +64,34 @@ func main() {
"app id", cfg.AppID, "mode", cfg.Mode)
klog.InfoS("Rusid is using", "config", cfg)

//setup HealthzServer
startHealthzServer(cfg.HealthzPort,
//Healthz server
go startHealthzServer(mainCtx, cfg.HealthzPort,
// WithTimeout allows you to set a max overall timeout.
healthcheck.WithTimeout(5*time.Second),
healthcheck.WithChecker("component manager", compManager))

err = rt.Run()
shutdownOnInterrupt(cancel)

err = rt.Run(mainCtx) //blocks
if err != nil {
klog.Error(err)
}
}

func startHealthzServer(healthzPort int, options ...healthcheck.Option) {
if err := healthcheck.Run(context.Background(), healthzPort, options...); err != nil {
func shutdownOnInterrupt(cancel func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

go func() {
<-c
klog.InfoS("Shutdown requested")
cancel()
}()

}

func startHealthzServer(ctx context.Context, healthzPort int, options ...healthcheck.Option) {
if err := healthcheck.Run(ctx, healthzPort, options...); err != nil {
klog.Fatalf("failed to start healthz server: %s", err)
}
}
7 changes: 5 additions & 2 deletions pkg/api/runtime/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package runtime

import "rusi/pkg/messaging"
import (
"context"
"rusi/pkg/messaging"
)

type Api interface {
Serve() error
Serve(ctx context.Context) error
Refresh() error
SetPublishHandler(messaging.PublishRequestHandler)
SetSubscribeHandler(messaging.SubscribeRequestHandler)
Expand Down
9 changes: 8 additions & 1 deletion pkg/api/runtime/grpc/grpc_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (srv *grpcApi) Refresh() error {
return srv.server.Refresh()
}

func (srv *grpcApi) Serve() error {
func (srv *grpcApi) Serve(ctx context.Context) error {
grpcServer := grpc.NewServer(srv.serverOptions...)
v1.RegisterRusiServer(grpcServer, srv.server)

Expand All @@ -53,6 +53,13 @@ func (srv *grpcApi) Serve() error {
klog.Fatalf("failed to listen: %v", err)
}

go func() {
select {
case <-ctx.Done():
grpcServer.Stop()
}
}()

return grpcServer.Serve(lis)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/api/runtime/test_api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"context"
"rusi/pkg/messaging"
)

Expand All @@ -14,7 +15,7 @@ func NewTestApi() *TestApi {
return &TestApi{RefreshChan: make(chan bool)}
}

func (TestApi) Serve() error {
func (TestApi) Serve(ctx context.Context) error {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ func (rt *runtime) SubscribeHandler(ctx context.Context, request messaging.Subsc
return srv.StartSubscribing(request)
}

func (rt *runtime) Run() error {
return rt.api.Serve()
func (rt *runtime) Run(ctx context.Context) error {
return rt.api.Serve(ctx)
}

0 comments on commit e0aeff3

Please sign in to comment.