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

     1  // Copyright 2019 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  	"bytes"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/keys"
    17  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    18  )
    19  
    20  // ConnectionClass is the identifier of a group of RPC client sessions that are
    21  // allowed to share an underlying TCP connections; RPC sessions with different
    22  // connection classes are guaranteed to use separate gRPC client connections.
    23  //
    24  // RPC sessions that share a connection class are arbitrated using the gRPC flow
    25  // control logic, see google.golang.org/grpc/internal/transport. The lack of
    26  // support of prioritization in the current gRPC implementation is the reason
    27  // why we are separating different priority flows across separate TCP
    28  // connections. Future gRPC improvements may enable further simplification
    29  // here. See https://github.com/grpc/grpc-go/issues/1448 for progress on gRPC's
    30  // adoption of HTTP2 priorities.
    31  type ConnectionClass int8
    32  
    33  const (
    34  	// DefaultClass is the default ConnectionClass and should be used for most
    35  	// client traffic.
    36  	DefaultClass ConnectionClass = iota
    37  	// SystemClass is the ConnectionClass used for system traffic.
    38  	SystemClass
    39  
    40  	// NumConnectionClasses is the number of valid ConnectionClass values.
    41  	NumConnectionClasses int = iota
    42  )
    43  
    44  var systemClassKeyPrefixes = []roachpb.RKey{
    45  	roachpb.RKey(keys.Meta1Prefix),
    46  	roachpb.RKey(keys.NodeLivenessPrefix),
    47  }
    48  
    49  // ConnectionClassForKey determines the ConnectionClass which should be used
    50  // for traffic addressed to the RKey.
    51  func ConnectionClassForKey(key roachpb.RKey) ConnectionClass {
    52  	// An empty RKey addresses range 1 and warrants SystemClass.
    53  	if len(key) == 0 {
    54  		return SystemClass
    55  	}
    56  	for _, prefix := range systemClassKeyPrefixes {
    57  		if bytes.HasPrefix(key, prefix) {
    58  			return SystemClass
    59  		}
    60  	}
    61  	return DefaultClass
    62  }