Skip to content

Commit

Permalink
rpc: use "+" concatination in hot path RPC rate limit metrics. (#16923)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Apr 18, 2023
1 parent 3067191 commit bef109d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
8 changes: 4 additions & 4 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,15 @@ func (ai *AuthenticatedIdentity) String() string {
return "unauthenticated"
}
if ai.ACLToken != nil {
return fmt.Sprintf("token:%s", ai.ACLToken.AccessorID)
return "token:" + ai.ACLToken.AccessorID
}
if ai.Claims != nil {
return fmt.Sprintf("alloc:%s", ai.Claims.AllocationID)
return "alloc:" + ai.Claims.AllocationID
}
if ai.ClientID != "" {
return fmt.Sprintf("client:%s", ai.ClientID)
return "client:" + ai.ClientID
}
return fmt.Sprintf("%s:%s", ai.TLSName, ai.RemoteIP.String())
return ai.TLSName + ":" + ai.RemoteIP.String()
}

func (ai *AuthenticatedIdentity) IsExpired(now time.Time) bool {
Expand Down
57 changes: 57 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package structs

import (
"fmt"
"net"
"os"
"reflect"
"strings"
Expand All @@ -22,6 +23,62 @@ import (
"github.com/stretchr/testify/require"
)

func TestAuthenticatedIdentity_String(t *testing.T) {
ci.Parallel(t)

testCases := []struct {
name string
inputAuthenticatedIdentity *AuthenticatedIdentity
expectedOutput string
}{
{
name: "nil",
inputAuthenticatedIdentity: nil,
expectedOutput: "unauthenticated",
},
{
name: "ACL token",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
ACLToken: &ACLToken{
AccessorID: "my-testing-accessor-id",
},
},
expectedOutput: "token:my-testing-accessor-id",
},
{
name: "alloc claim",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
Claims: &IdentityClaims{
AllocationID: "my-testing-alloc-id",
},
},
expectedOutput: "alloc:my-testing-alloc-id",
},
{
name: "client",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
ClientID: "my-testing-client-id",
},
expectedOutput: "client:my-testing-client-id",
},
{
name: "tls remote IP",
inputAuthenticatedIdentity: &AuthenticatedIdentity{
TLSName: "my-testing-tls-name",
RemoteIP: net.IPv4(192, 168, 135, 232),
},
expectedOutput: "my-testing-tls-name:192.168.135.232",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualOutput := tc.inputAuthenticatedIdentity.String()
must.Eq(t, tc.expectedOutput, actualOutput)
})
}
}

func TestJob_Validate(t *testing.T) {
ci.Parallel(t)

Expand Down

0 comments on commit bef109d

Please sign in to comment.