Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alts: Read max number of concurrent ALTS handshakes from environment variable. #6267

Merged
merged 30 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1852f83
Read max number of concurrent ALTS handshakes from environment variable.
matthewstevenson88 May 10, 2023
6e70e9b
Refactor to use new envconfig file.
matthewstevenson88 May 10, 2023
e3f3dcc
Remove impossible if condition in acquire().
matthewstevenson88 May 10, 2023
f8d609a
Use weighted semaphore.
matthewstevenson88 May 17, 2023
6f61a5a
Add e2e test for concurrent ALTS handshakes.
matthewstevenson88 May 17, 2023
6f2edc6
Merge branch 'grpc:master' into alts-read-envvar
matthewstevenson88 May 17, 2023
e41e933
Merge branch 'master' of https://github.com/matthewstevenson88/grpc-g…
matthewstevenson88 May 26, 2023
e53683d
Separate into client and server semaphores.
matthewstevenson88 May 30, 2023
93e546c
Merge branch 'master' of https://github.com/matthewstevenson88/grpc-g…
matthewstevenson88 May 30, 2023
97177fa
Use TryAcquire instead of Acquire.
matthewstevenson88 May 31, 2023
ad5e053
Attempt to fix go.sum error.
matthewstevenson88 May 31, 2023
a151bb3
Run go mod tidy compat=1.17.
matthewstevenson88 May 31, 2023
b486c31
Update go.mod for examples subdirectory.
matthewstevenson88 May 31, 2023
e8933fd
Run go mod tidy -compat=1.17 on examples subdirectory.
matthewstevenson88 May 31, 2023
803c907
Update go.mod in subdirectories.
matthewstevenson88 May 31, 2023
cbb7ac2
Update go.mod in security/advancedtls/examples.
matthewstevenson88 May 31, 2023
ac93ee8
Missed another go.mod update.
matthewstevenson88 May 31, 2023
c1216c0
Do not upgrade glog because it requires Golang 1.19.
matthewstevenson88 May 31, 2023
d98424f
Fix glog version in examples/go.sum.
matthewstevenson88 May 31, 2023
9d5d1dc
More glog cleanup.
matthewstevenson88 May 31, 2023
ff48d09
Fix glog issue in gcp/observability/go.sum.
matthewstevenson88 May 31, 2023
75b0dc0
Move ALTS env var into envconfig.go.
matthewstevenson88 Jun 7, 2023
caa6ad8
Merge branch 'master' of https://github.com/matthewstevenson88/grpc-g…
matthewstevenson88 Jun 7, 2023
0fec934
Fix go.mod files.
matthewstevenson88 Jun 7, 2023
0ab0b7d
Revert go.sum files.
matthewstevenson88 Jun 7, 2023
f247acb
Merge branch 'master' of https://github.com/matthewstevenson88/grpc-g…
matthewstevenson88 Jun 7, 2023
d046f3a
Revert interop/observability/go.mod change.
matthewstevenson88 Jun 7, 2023
ab17f8d
Run go mod tidy -compat=1.17 on examples/.
matthewstevenson88 Jun 7, 2023
f75369c
Run gofmt.
matthewstevenson88 Jun 7, 2023
8c5bc34
Add comment describing test init function.
matthewstevenson88 Jun 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions credentials/alts/internal/handshaker/handshaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"fmt"
"io"
"net"
"os"
"strconv"
"sync"

grpc "google.golang.org/grpc"
Expand All @@ -41,9 +43,10 @@ const (
// The maximum byte size of receive frames.
frameLimit = 64 * 1024 // 64 KB
rekeyRecordProtocolName = "ALTSRP_GCM_AES128_REKEY"
// maxPendingHandshakes represents the maximum number of concurrent
// defaultMaxPendingHandshakes represents the default maximum number of concurrent
// handshakes.
maxPendingHandshakes = 100
defaultMaxPendingHandshakes = 100
maxConcurrentHandshakesEnvVariable = "GRPC_ALTS_MAX_CONCURRENT_HANDSHAKES"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointers! I've cleaned up the PR accordingly. :)

)

var (
Expand All @@ -59,6 +62,7 @@ var (
return conn.NewAES128GCMRekey(s, keyData)
},
}
maxPendingHandshakes int64
// control number of concurrent created (but not closed) handshakers.
mu sync.Mutex
concurrentHandshakes = int64(0)
Expand All @@ -70,6 +74,7 @@ var (
)

func init() {
maxPendingHandshakes = getMaxConcurrentHandshakes()
for protocol, f := range altsRecordFuncs {
if err := conn.RegisterProtocol(protocol, f); err != nil {
panic(err)
Expand Down Expand Up @@ -391,3 +396,18 @@ func (h *altsHandshaker) Close() {
h.stream.CloseSend()
}
}

func getMaxConcurrentHandshakes() int64 {
maxConcurrentHandshakesString, ok := os.LookupEnv(maxConcurrentHandshakesEnvVariable)
if !ok {
return defaultMaxPendingHandshakes
}
maxConcurrentHandshakes, err := strconv.ParseInt(maxConcurrentHandshakesString, 10, 64)
if err != nil {
return defaultMaxPendingHandshakes
}
if maxConcurrentHandshakes < 0 {
return defaultMaxPendingHandshakes
}
return maxConcurrentHandshakes
}
31 changes: 27 additions & 4 deletions credentials/alts/internal/handshaker/handshaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"context"
"errors"
"os"
"testing"
"time"

Expand Down Expand Up @@ -134,7 +135,7 @@ func (s) TestClientHandshake(t *testing.T) {
numberOfHandshakes int
}{
{0 * time.Millisecond, 1},
{100 * time.Millisecond, 10 * maxPendingHandshakes},
{100 * time.Millisecond, 10 * int(maxPendingHandshakes)},
} {
errc := make(chan error)
stat.Reset()
Expand Down Expand Up @@ -182,7 +183,7 @@ func (s) TestClientHandshake(t *testing.T) {
}

// Ensure that there are no concurrent calls more than the limit.
if stat.MaxConcurrentCalls > maxPendingHandshakes {
if stat.MaxConcurrentCalls > int(maxPendingHandshakes) {
t.Errorf("Observed %d concurrent handshakes; want <= %d", stat.MaxConcurrentCalls, maxPendingHandshakes)
}
}
Expand All @@ -194,7 +195,7 @@ func (s) TestServerHandshake(t *testing.T) {
numberOfHandshakes int
}{
{0 * time.Millisecond, 1},
{100 * time.Millisecond, 10 * maxPendingHandshakes},
{100 * time.Millisecond, 10 * int(maxPendingHandshakes)},
} {
errc := make(chan error)
stat.Reset()
Expand Down Expand Up @@ -239,7 +240,7 @@ func (s) TestServerHandshake(t *testing.T) {
}

// Ensure that there are no concurrent calls more than the limit.
if stat.MaxConcurrentCalls > maxPendingHandshakes {
if stat.MaxConcurrentCalls > int(maxPendingHandshakes) {
t.Errorf("Observed %d concurrent handshakes; want <= %d", stat.MaxConcurrentCalls, maxPendingHandshakes)
}
}
Expand Down Expand Up @@ -349,3 +350,25 @@ func (s) TestNewServerHandshaker(t *testing.T) {
}
hs.Close()
}

func (s) TestGetMaxConcurrentHandshakes(t *testing.T) {
for _, testCase := range []struct {
setMaxConcurrentHandshakeEnvVariable bool
maxConcurrentHandshakeString string
expectedMaxConcurrentHandshakes int64
}{
{false, "", defaultMaxPendingHandshakes},
{true, "", defaultMaxPendingHandshakes},
{true, "not-an-integer", defaultMaxPendingHandshakes},
{true, "-10", defaultMaxPendingHandshakes},
{true, "10", int64(10)},
} {
os.Unsetenv(maxConcurrentHandshakesEnvVariable)
if testCase.setMaxConcurrentHandshakeEnvVariable {
os.Setenv(maxConcurrentHandshakesEnvVariable, testCase.maxConcurrentHandshakeString)
}
if got, want := getMaxConcurrentHandshakes(), testCase.expectedMaxConcurrentHandshakes; got != want {
t.Errorf("getMaxConcurrentHandshakes() = %d, want = %d", got, want)
}
}
}