Skip to content

Commit

Permalink
feat: add grpc health service (#382)
Browse files Browse the repository at this point in the history
* feat: add grpc health service

* add health check test

* use t.Errorf
  • Loading branch information
hongalex authored Sep 7, 2022
1 parent 1a7d1b1 commit a7415ce
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
4 changes: 2 additions & 2 deletions api/open_saves.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/open_saves_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions internal/app/server/open_saves_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
"time"

"github.com/alicebob/miniredis/v2"
"github.com/googleforgames/open-saves/internal/pkg/cmd"
"github.com/googleforgames/open-saves/internal/pkg/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"

"cloud.google.com/go/datastore"
"github.com/google/uuid"
Expand All @@ -40,6 +43,8 @@ import (
"google.golang.org/api/iterator"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/types/known/timestamppb"
Expand All @@ -48,6 +53,7 @@ import (
const (
testProject = "triton-for-games-dev"
testBucket = "gs://triton-integration"
testPort = "8000"
testBufferSize = 1024 * 1024
// The threshold of comparing times.
// Since the server will actually access the backend datastore,
Expand All @@ -56,6 +62,45 @@ const (
blobKind = "blob"
)

func TestOpenSaves_HealthCheck(t *testing.T) {
configPath := cmd.GetEnvVarString("OPEN_SAVES_CONFIG", "../../../configs/")
viper.Set(config.OpenSavesBucket, testBucket)
viper.Set(config.OpenSavesProject, testProject)
viper.Set(config.OpenSavesPort, testPort)
cfg, err := config.Load(configPath)
if err != nil {
t.Fatalf("got err loading config: %v", err)
}

ctx := context.Background()
go func() {
if err := Run(ctx, "tcp", cfg); err != nil {
log.Errorf("got err calling server.Run: %v", err)
}
}()

options := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}

conn, err := grpc.Dial(fmt.Sprintf(":%s", testPort), options...)
if err != nil {
t.Fatalf("did not connect %v", err)
}
defer conn.Close()

hc := healthgrpc.NewHealthClient(conn)
got, err := hc.Check(ctx, &healthgrpc.HealthCheckRequest{
Service: serviceName,
})
if err != nil {
t.Errorf("healthClient.Check err: %v", err)
}
if want := healthgrpc.HealthCheckResponse_SERVING; got.Status != want {
t.Errorf("hc.Check got: %v, want: %v", got.Status, want)
}
}

func getOpenSavesServer(ctx context.Context, t *testing.T, cloud string) (*openSavesServer, *bufconn.Listener) {
t.Helper()
r := miniredis.RunT(t)
Expand Down
15 changes: 12 additions & 3 deletions internal/app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ package server

import (
"context"
"github.com/googleforgames/open-saves/internal/pkg/config"
"net"

"github.com/googleforgames/open-saves/internal/pkg/config"

pb "github.com/googleforgames/open-saves/api"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)

pb "github.com/googleforgames/open-saves/api"
const (
serviceName = "grpc.health.v1.opensaves"
)

// Run starts the Open Saves gRPC service.
Expand All @@ -36,11 +42,14 @@ func Run(ctx context.Context, network string, cfg *config.ServiceConfig) error {
}
defer func() {
if err := lis.Close(); err != nil {
log.Errorf("Failed to close %s %s: %v", network, cfg.ServerConfig.Address, err)
log.Errorf("Failed to close %s %s: %v\n", network, cfg.ServerConfig.Address, err)
}
}()

s := grpc.NewServer()
healthcheck := health.NewServer()
healthcheck.SetServingStatus(serviceName, healthgrpc.HealthCheckResponse_SERVING)
healthgrpc.RegisterHealthServer(s, healthcheck)
server, err := newOpenSavesServer(ctx, cfg)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetEnvVarUInt(name string, defValue uint64) uint64 {
return defValue
}

// GetEnvVarString returns a string value of an environmental variable speficied by name.
// GetEnvVarString returns a string value of an environmental variable specified by name.
// Returns defValue if the variable is empty (e.g. not defined).
func GetEnvVarString(name string, defValue string) string {
if value := os.Getenv(name); value != "" {
Expand Down

0 comments on commit a7415ce

Please sign in to comment.