Skip to content

Commit

Permalink
Merge pull request #398 from nokia/grpc-keepalive
Browse files Browse the repository at this point in the history
allow enabling and configuring gRPC keepalive for gNMI clients
  • Loading branch information
karimra authored Mar 22, 2024
2 parents 3301e61 + 8c8ee3b commit 5905af1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/user_guide/targets/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ targets:
# When empty or set to 0s, the Golang default (15s) applies.
# Disabled if set to a negative value.
tcp-keepalive: 0s
# sets gRPC keepalive parameters according to:
# https://github.com/grpc/proposal/blob/master/A8-client-side-keepalive.md
grpc-keepalive:
# After a duration of this time if the client doesn't see any activity
# it pings the server to see if the transport is still alive.
# If set below 10s, a minimum value of 10s will be used instead.
time:
# After having pinged for keepalive check, the client waits
# for a duration of Timeout and if no activity is seen even
# after that the connection is closed.
timeout:
# If true, client sends keepalive pings even with no active RPCs.
# If false, when there are no active RPCs,
# Time and Timeout will be ignored and no keepalive pings will be sent.
permit-without-stream: false
```
### Example
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/types/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/credentials/oauth"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/keepalive"

"github.com/openconfig/gnmic/pkg/api/utils"
)
Expand Down Expand Up @@ -150,6 +151,13 @@ type TargetConfig struct {
Metadata map[string]string `mapstructure:"metadata,omitempty" yaml:"metadata,omitempty" json:"metadata,omitempty"`
CipherSuites []string `mapstructure:"cipher-suites,omitempty" yaml:"cipher-suites,omitempty" json:"cipher-suites,omitempty"`
TCPKeepalive time.Duration `mapstructure:"tcp-keepalive,omitempty" yaml:"tcp-keepalive,omitempty" json:"tcp-keepalive,omitempty"`
GRPCKeepalive *clientKeepalive `mapstructure:"grpc-keepalive,omitempty" yaml:"grpc-keepalive,omitempty" json:"grpc-keepalive,omitempty"`
}

type clientKeepalive struct {
Time time.Duration `mapstructure:"time,omitempty"`
Timeout time.Duration `mapstructure:"timeout,omitempty"`
PermitWithoutStream bool `mapstructure:"permit-without-stream,omitempty"`
}

func (tc TargetConfig) String() string {
Expand Down Expand Up @@ -227,6 +235,14 @@ func (tc *TargetConfig) GrpcDialOptions() ([]grpc.DialOption, error) {
if tc.Gzip != nil && *tc.Gzip {
tOpts = append(tOpts, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name)))
}
// gRPC keepalive
if tc.GRPCKeepalive != nil {
tOpts = append(tOpts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: tc.GRPCKeepalive.Time,
Timeout: tc.GRPCKeepalive.Timeout,
PermitWithoutStream: tc.GRPCKeepalive.PermitWithoutStream,
}))
}
// insecure
if tc.Insecure != nil && *tc.Insecure {
tOpts = append(tOpts,
Expand Down

0 comments on commit 5905af1

Please sign in to comment.