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

feat: add configurations for keep alive on grpc server #416

Merged
3 commits merged into from
Jan 10, 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
5 changes: 5 additions & 0 deletions configs/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ redis_max_retry_backoff: "512ms"

blob_max_inline_size: 65536

grpc_keepalive_max_connection_idle: "5m"
grpc_keepalive_max_connection_age: "12h"
grpc_keepalive_max_connection_age_grace: "5m"
grpc_keepalive_time: "1h"
grpc_keepalive_timeout: "20s"
# The following enables OpenCensus Tracing via Cloud Trace
# for the Datastore client library.
# It is EXPERIMENTAL and subject to change or removal without notice.
Expand Down
14 changes: 13 additions & 1 deletion internal/app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package server

import (
"context"
"google.golang.org/grpc/keepalive"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -48,7 +49,18 @@ func Run(ctx context.Context, network string, cfg *config.ServiceConfig) error {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

s := grpc.NewServer()
grpcOptions := []grpc.ServerOption{
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: cfg.GRPCServerConfig.MaxConnectionIdle,
MaxConnectionAge: cfg.GRPCServerConfig.MaxConnectionAge,
MaxConnectionAgeGrace: cfg.GRPCServerConfig.MaxConnectionAgeGrace,
Time: cfg.GRPCServerConfig.Time,
Timeout: cfg.GRPCServerConfig.Timeout,
}),
}

s := grpc.NewServer(grpcOptions...)

healthcheck := health.NewServer()
healthcheck.SetServingStatus(serviceName, healthgrpc.HealthCheckResponse_SERVING)
healthgrpc.RegisterHealthServer(s, healthcheck)
Expand Down
17 changes: 13 additions & 4 deletions internal/pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,19 @@ func Load(path string) (*ServiceConfig, error) {
MaxInlineSize: viper.GetInt(BlobMaxInlineSize),
}

grpcServerConfig := GRPCServerConfig{
MaxConnectionIdle: viper.GetDuration(GRPCKeepAliveMaxConnectionIdle),
MaxConnectionAge: viper.GetDuration(GRPCKeepAliveMaxConnectionAge),
MaxConnectionAgeGrace: viper.GetDuration(GRPCKeepAliveMaxConnectionAgeGrace),
Time: viper.GetDuration(GRPCKeepAliveTime),
Timeout: viper.GetDuration(GRPCKeepAliveTimeout),
}

return &ServiceConfig{
ServerConfig: serverConfig,
CacheConfig: cacheConfig,
RedisConfig: redisConfig,
BlobConfig: blobConfig,
ServerConfig: serverConfig,
CacheConfig: cacheConfig,
RedisConfig: redisConfig,
BlobConfig: blobConfig,
GRPCServerConfig: grpcServerConfig,
}, nil
}
17 changes: 17 additions & 0 deletions internal/pkg/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const (

BlobMaxInlineSize = "blob_max_inline_size"

GRPCKeepAliveMaxConnectionIdle = "grpc_keepalive_max_connection_idle"
GRPCKeepAliveMaxConnectionAge = "grpc_keepalive_max_connection_age"
GRPCKeepAliveMaxConnectionAgeGrace = "grpc_keepalive_max_connection_age_grace"
GRPCKeepAliveTime = "grpc_keepalive_time"
GRPCKeepAliveTimeout = "grpc_keepalive_timeout"

// The following enables OpenCensus Tracing via Cloud Trace
// for the Datastore client library.
// It is EXPERIMENTAL and subject to change or removal without notice.
Expand All @@ -48,6 +54,7 @@ type ServiceConfig struct {
CacheConfig
RedisConfig
BlobConfig
GRPCServerConfig
}

// ServerConfig defines common fields needed to start Open Saves.
Expand Down Expand Up @@ -87,3 +94,13 @@ type RedisConfig struct {
type BlobConfig struct {
MaxInlineSize int
}

// GRPCServerConfig has the configurations for grpc server, for now keepAlive parameters
// all the parameters are handled in time.Duration
type GRPCServerConfig struct {
MaxConnectionIdle time.Duration
MaxConnectionAge time.Duration
MaxConnectionAgeGrace time.Duration
Time time.Duration
Timeout time.Duration
}