1// Copyright 2018 Istio Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package keepalive 16 17import ( 18 "math" 19 "time" 20 21 "github.com/spf13/cobra" 22) 23 24const ( 25 // Infinity is the maximum possible duration for keepalive values 26 Infinity = time.Duration(math.MaxInt64) 27) 28 29// Options defines the set of options used for grpc keepalive. 30// The Time and Timeout options are used for both client and server connections, 31// whereas MaxServerConnectionAge* options are applicable on the server side only 32// (as implied by the options' name...) 33type Options struct { 34 // After a duration of this time if the server/client doesn't see any activity it pings the peer to see if the transport is still alive. 35 Time time.Duration 36 // After having pinged for keepalive check, the server waits for a duration of Timeout and if no activity is seen even after that 37 // the connection is closed. 38 Timeout time.Duration 39 // MaxServerConnectionAge is a duration for the maximum amount of time a 40 // connection may exist before it will be closed by the server sending a GoAway. 41 // A random jitter is added to spread out connection storms. 42 // See https://github.com/grpc/grpc-go/blob/bd0b3b2aa2a9c87b323ee812359b0e9cda680dad/keepalive/keepalive.go#L49 43 MaxServerConnectionAge time.Duration // default value is infinity 44 // MaxServerConnectionAgeGrace is an additive period after MaxServerConnectionAge 45 // after which the connection will be forcibly closed by the server. 46 MaxServerConnectionAgeGrace time.Duration // default value 10s 47} 48 49// DefaultOption returns the default keepalive options. 50func DefaultOption() *Options { 51 return &Options{ 52 Time: 30 * time.Second, 53 Timeout: 10 * time.Second, 54 MaxServerConnectionAge: Infinity, 55 MaxServerConnectionAgeGrace: 10 * time.Second, 56 } 57} 58 59// AttachCobraFlags attaches a set of Cobra flags to the given Cobra command. 60// 61// Cobra is the command-line processor that Istio uses. This command attaches 62// the necessary set of flags to configure the grpc keepalive options. 63func (o *Options) AttachCobraFlags(cmd *cobra.Command) { 64 cmd.PersistentFlags().DurationVar(&o.Time, "keepaliveInterval", o.Time, 65 "The time interval if no activity on the connection it pings the peer to see if the transport is alive") 66 cmd.PersistentFlags().DurationVar(&o.Timeout, "keepaliveTimeout", o.Timeout, 67 "After having pinged for keepalive check, the client/server waits for a duration of keepaliveTimeout "+ 68 "and if no activity is seen even after that the connection is closed.") 69 cmd.PersistentFlags().DurationVar(&o.MaxServerConnectionAge, "keepaliveMaxServerConnectionAge", 70 o.MaxServerConnectionAge, "Maximum duration a connection will be kept open on the server before a graceful close.") 71} 72