github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/rpc/keepalive.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package rpc
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/base"
    17  	"google.golang.org/grpc/keepalive"
    18  )
    19  
    20  // 10 seconds is the minimum keepalive interval permitted by gRPC.
    21  // Setting it to a value lower than this will lead to gRPC adjusting to this
    22  // value and annoyingly logging "Adjusting keepalive ping interval to minimum
    23  // period of 10s". See grpc/grpc-go#2642.
    24  const minimumClientKeepaliveInterval = 10 * time.Second
    25  
    26  // To prevent unidirectional network partitions from keeping an unhealthy
    27  // connection alive, we use both client-side and server-side keepalive pings.
    28  var clientKeepalive = keepalive.ClientParameters{
    29  	// Send periodic pings on the connection.
    30  	Time: minimumClientKeepaliveInterval,
    31  	// If the pings don't get a response within the timeout, we might be
    32  	// experiencing a network partition. gRPC will close the transport-level
    33  	// connection and all the pending RPCs (which may not have timeouts) will
    34  	// fail eagerly. gRPC will then reconnect the transport transparently.
    35  	Timeout: minimumClientKeepaliveInterval,
    36  	// Do the pings even when there are no ongoing RPCs.
    37  	PermitWithoutStream: true,
    38  }
    39  var serverKeepalive = keepalive.ServerParameters{
    40  	// Send periodic pings on the connection.
    41  	Time: base.NetworkTimeout,
    42  	// If the pings don't get a response within the timeout, we might be
    43  	// experiencing a network partition. gRPC will close the transport-level
    44  	// connection and all the pending RPCs (which may not have timeouts) will
    45  	// fail eagerly.
    46  	Timeout: base.NetworkTimeout,
    47  }
    48  
    49  // By default, gRPC disconnects clients that send "too many" pings,
    50  // but we don't really care about that, so configure the server to be
    51  // as permissive as possible.
    52  var serverEnforcement = keepalive.EnforcementPolicy{
    53  	MinTime:             time.Nanosecond,
    54  	PermitWithoutStream: true,
    55  }
    56  
    57  var serverTestingKeepalive = keepalive.ServerParameters{
    58  	Time:    200 * time.Millisecond,
    59  	Timeout: 300 * time.Millisecond,
    60  }