Skip to content

Comments

nat: assorted optimisations and test-impovements#121

Merged
thaJeztah merged 5 commits intodocker:masterfrom
thaJeztah:nat_small_optim
Apr 24, 2025
Merged

nat: assorted optimisations and test-impovements#121
thaJeztah merged 5 commits intodocker:masterfrom
thaJeztah:nat_small_optim

Conversation

@thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Apr 23, 2025

nat: slight optimization in ParsePortSpec

  • validate protocol before doing additional parsing
  • use a switch instead of looping over supported protocols
  • lowercase the proto once
  • remove some uses of fmt.Sprintf in favor of string-concatenating

nat: TestSplitProtoPort: use table-test

nat: SplitProtoPort: optimize and improve GoDoc

Use strings.Cut to improve performance and simplify the code,
and update the GoDoc to better outline expectations.

nat: TestParsePort: use table-test

nat: ParsePort: improve error

Unwrap the error to don't include "strconv.ParseUint" in the
error message.

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah changed the title nat: slight optimization in ParsePortSpec nat: assorted optimisations and test-impovements Apr 24, 2025
Copy link
Contributor

@robmry robmry left a comment

Choose a reason for hiding this comment

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

Some non-blocking suggestions ...

nat/nat.go Outdated
Comment on lines 100 to 101
// SplitProtoPort does not validate or normalize the returned values.
func SplitProtoPort(rawPort string) (string, string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Splitting "port/proto" and returning proto, port (instead of port, proto) is surprising. Could name the return vals? ...

Suggested change
// SplitProtoPort does not validate or normalize the returned values.
func SplitProtoPort(rawPort string) (string, string) {
// SplitProtoPort does not validate or normalize the returned values.
func SplitProtoPort(rawPort string) (proto, port string) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yes, can do; I was actually considering that, as I also was a bit surprised that this one flipped the order of the fields (was even considering to mark it deprecated as it's a bit of a silly function, but the replacement (use strings.Cut) would also require users to fill in the default (tcp), so there's still "some" value.

}

p, err = ParsePort("1asdf")
for _, tc := range tests {
Copy link
Contributor

Choose a reason for hiding this comment

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

1asdf (number followed by junk) seems like a good test to keep?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh! Yes, possibly. I completely read that one as lasdf (lowercase L 😂); will add it back, good catch!

Copy link
Contributor

Choose a reason for hiding this comment

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

You're 1337 by-default!

nat/nat_test.go Outdated
Comment on lines 37 to 45
// FIXME currently this is a valid port. I don't think it should be.
// I'm leaving this test commented out until we make a decision.
// - erikh
// {
// doc: "octal value",
// port: "0123",
// expPort: 0,
// expErr: `some error message`,
// },
Copy link
Contributor

Choose a reason for hiding this comment

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

In the absence of a decision - it's probably worth un-commenting this to record current behaviour, in case it changes and someone's ended up relying on it? (Then a change would have to be deliberate.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, makes sense

- validate protocol before doing additional parsing
- use a switch instead of looping over supported protocols
- lowercase the proto once
- remove some uses of fmt.Sprintf in favor of string-concatenating

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Use strings.Cut to improve performance and simplify the code,
and update the GoDoc to better outline expectations.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Unwrap the error to don't include "strconv.ParseUint" in the
error message.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah
Copy link
Member Author

@robmry updated; PTAL 🤗

Copy link
Contributor

@robmry robmry left a comment

Choose a reason for hiding this comment

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

LGTM - thank you!

@thaJeztah thaJeztah merged commit 784042e into docker:master Apr 24, 2025
13 checks passed
@thaJeztah thaJeztah deleted the nat_small_optim branch April 24, 2025 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants