-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ns-update-deps
- Loading branch information
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package simple_k8s | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"google.golang.org/grpc/health" | ||
healthpb "google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
type AliveGuard struct { | ||
mutex sync.Mutex | ||
healthCheckServer *health.Server | ||
timeThreshold time.Duration | ||
serviceName string | ||
failed bool | ||
} | ||
|
||
func NewAliveGuard(healtServer *health.Server, threshold time.Duration, serviceName string) *AliveGuard { | ||
return &AliveGuard{ | ||
healthCheckServer: healtServer, | ||
timeThreshold: threshold, | ||
serviceName: serviceName, | ||
failed: false, | ||
} | ||
} | ||
|
||
func (g *AliveGuard) CheckTimeDuration(startTime time.Time) { | ||
if g.failed { | ||
return | ||
} | ||
|
||
if time.Since(startTime) > g.timeThreshold { | ||
g.mutex.Lock() | ||
defer g.mutex.Unlock() | ||
if !g.failed { | ||
g.failed = true | ||
g.healthCheckServer.SetServingStatus(g.serviceName, healthpb.HealthCheckResponse_NOT_SERVING) | ||
} | ||
} | ||
|
||
} | ||
|
||
func GetAppAliveGuard(threshold time.Duration) *AliveGuard { | ||
return NewAliveGuard(healthCheckServer, threshold, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package simple_k8s | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/health" | ||
healthgrpc "google.golang.org/grpc/health/grpc_health_v1" | ||
healthpb "google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
const HealthPort = 9666 | ||
const CheckName = "alive" | ||
|
||
var healthCheckServer *health.Server = &health.Server{} | ||
|
||
func EnableLivelinessCheck() { | ||
|
||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", HealthPort)) | ||
if err != nil { | ||
log.Fatalf("failed to listen: %v", err) | ||
} | ||
|
||
s := grpc.NewServer() | ||
*healthCheckServer = *health.NewServer() | ||
healthCheckServer.SetServingStatus(CheckName, healthpb.HealthCheckResponse_SERVING) | ||
healthgrpc.RegisterHealthServer(s, healthCheckServer) | ||
s.Serve(lis) | ||
} | ||
|
||
func DisableLivelinessCheck() { | ||
|
||
healthCheckServer.SetServingStatus(CheckName, healthpb.HealthCheckResponse_NOT_SERVING) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters