-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathconnection_class.go
79 lines (68 loc) · 2.57 KB
/
connection_class.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package rpc
import (
"bytes"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
)
// ConnectionClass is the identifier of a group of RPC client sessions that are
// allowed to share an underlying TCP connections; RPC sessions with different
// connection classes are guaranteed to use separate gRPC client connections.
//
// RPC sessions that share a connection class are arbitrated using the gRPC flow
// control logic, see google.golang.org/grpc/internal/transport. The lack of
// support of prioritization in the current gRPC implementation is the reason
// why we are separating different priority flows across separate TCP
// connections. Future gRPC improvements may enable further simplification
// here. See https://github.com/grpc/grpc-go/issues/1448 for progress on gRPC's
// adoption of HTTP2 priorities.
type ConnectionClass int8
const (
// DefaultClass is the default ConnectionClass and should be used for most
// client traffic.
DefaultClass ConnectionClass = iota
// SystemClass is the ConnectionClass used for system traffic.
SystemClass
// RangefeedClass is the ConnectionClass used for rangefeeds.
RangefeedClass
// NumConnectionClasses is the number of valid ConnectionClass values.
NumConnectionClasses int = iota
)
// connectionClassName maps classes to their name.
var connectionClassName = map[ConnectionClass]string{
DefaultClass: "default",
SystemClass: "system",
RangefeedClass: "rangefeed",
}
// String implements the fmt.Stringer interface.
func (c ConnectionClass) String() string {
return connectionClassName[c]
}
// SafeValue implements the redact.SafeValue interface.
func (ConnectionClass) SafeValue() {}
var systemClassKeyPrefixes = []roachpb.RKey{
roachpb.RKey(keys.Meta1Prefix),
roachpb.RKey(keys.NodeLivenessPrefix),
}
// ConnectionClassForKey determines the ConnectionClass which should be used
// for traffic addressed to the RKey.
func ConnectionClassForKey(key roachpb.RKey) ConnectionClass {
// An empty RKey addresses range 1 and warrants SystemClass.
if len(key) == 0 {
return SystemClass
}
for _, prefix := range systemClassKeyPrefixes {
if bytes.HasPrefix(key, prefix) {
return SystemClass
}
}
return DefaultClass
}