Skip to content

Commit

Permalink
Changing from an array to a map
Browse files Browse the repository at this point in the history
  • Loading branch information
john-westcott-iv committed Jan 21, 2022
1 parent d603d20 commit 855c3fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 13 additions & 5 deletions pkg/netceptor/netceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,23 @@ type routingUpdate struct {
}

const (
// If adding/changing an ConnType, make sure to change ConnTypeStrings as well. These are friendly strings printed out in a status command
// If adding/changing an ConnType, make sure to change ConnTypeStrings as well.
// These are friendly strings printed out in a status command.
// ConnTypeDatagram indicates a packetconn (datagram) service listener.
ConnTypeDatagram = 0
// ConnTypeStream indicates a conn (stream) service listener, without a user-defined TLS.
ConnTypeStream = 1
// ConnTypeStreamTLS indicates the service listens on a packetconn connection, with a user-defined TLS.
ConnTypeStreamTLS = 2
// Default Label for an unknown connection type.
UnknownConnTypeStr = "Unknown"
)

var ConnTypeStrings = [...]string{"Datagram", "Stream", "StreamTLS"}
var ConnTypeStrings = map[byte]string{
ConnTypeDatagram: "Datagram",
ConnTypeStream: "Stream",
ConnTypeStreamTLS: "StreamTLS",
}

// WorkCommand tracks available work types and whether they verify work submissions.
type WorkCommand struct {
Expand Down Expand Up @@ -444,11 +451,12 @@ func (s *Netceptor) MaxConnectionIdleTime() time.Duration {
// Convert the connection type to a string.
func (s *Netceptor) GetConnectionTypeAsString(connectionType byte) string {
// A byte can't be < 0 so we don't need to check the lower bounds
if connectionType < byte(len(ConnTypeStrings)) {
return ConnTypeStrings[connectionType]
connTypeString, ok := ConnTypeStrings[connectionType]
if !ok {
connTypeString = UnknownConnTypeStr
}

return "Unknown"
return connTypeString
}

type backendInfo struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/netceptor/netceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,11 +795,11 @@ func TestConnTypeString(t *testing.T) {
}()

n1 := New(context.Background(), "node1")
if ConnTypeStrings[ConnTypeDatagram] != n1.GetConnectionTypeAsString(0) {
t.Fatal("Datagram should be the first entry in ConnTypeStrings")
if ConnTypeStrings[ConnTypeDatagram] != n1.GetConnectionTypeAsString(ConnTypeDatagram) {
t.Fatal("The function did not properly return the constant for a datagram type")
}
if n1.GetConnectionTypeAsString(254) != "Unknown" {
t.Fatal("Either we now have 254 ConnTypes or GetConnectionTypeAsString did not properly return Unknown")
if n1.GetConnectionTypeAsString(254) != UnknownConnTypeStr {
t.Fatal("Either we now have 254 ConnTypes or GetConnectionTypeAsString did not properly return the string constant")
}
// Shutdown the network
n1.Shutdown()
Expand Down

0 comments on commit 855c3fe

Please sign in to comment.