-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add flags to allow changing ports for HotROD services #951
Add flags to allow changing ports for HotROD services #951
Conversation
Codecov Report
@@ Coverage Diff @@
## master #951 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 139 139
Lines 6399 6399
=====================================
Hits 6399 6399 Continue to review full report at Codecov.
|
examples/hotrod/cmd/root.go
Outdated
fixCustomerPort int | ||
fixDriverPort int | ||
fixFrontendPort int | ||
fixRoutePort int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"fix" prefix was used for variables about because they can be used to "fix" performance problems instead of changing source code as was done in the blog post. For port numbers there's no "fixing".
examples/hotrod/cmd/customer.go
Outdated
@@ -35,7 +36,7 @@ var customerCmd = &cobra.Command{ | |||
zapLogger := logger.With(zap.String("service", "customer")) | |||
logger := log.NewFactory(zapLogger) | |||
server := customer.NewServer( | |||
net.JoinHostPort(customerOptions.serverInterface, strconv.Itoa(customerOptions.serverPort)), | |||
net.JoinHostPort(customerOptions.serverInterface, strconv.Itoa(config.CustomerPort)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about customerOptions.serverPort? If it's no longer used anywhere we should delete it from the struct.
examples/hotrod/cmd/root.go
Outdated
RootCmd.PersistentFlags().IntVarP(&fixCustomerPort, "set-customer-service-port", "c", 8081, "Set port for customer service") | ||
RootCmd.PersistentFlags().IntVarP(&fixDriverPort, "set-driver-service-port", "d", 8082, "Set port for driver service") | ||
RootCmd.PersistentFlags().IntVarP(&fixFrontendPort, "set-frontend-service-port", "f", 8080, "Set port for frontend service") | ||
RootCmd.PersistentFlags().IntVarP(&fixRoutePort, "set-route-service-port", "r", 8083, "Set port for routing service") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for set- prefix.
No need for "Set " in the description.
examples/hotrod/cmd/root.go
Outdated
@@ -90,6 +102,26 @@ func onInitialize() { | |||
logger.Info("fix: overriding route worker pool size", zap.Int("old", config.RouteWorkerPoolSize), zap.Int("new", fixRouteWorkerPoolSize)) | |||
config.RouteWorkerPoolSize = fixRouteWorkerPoolSize | |||
} | |||
|
|||
if config.CustomerPort != fixCustomerPort { | |||
logger.Info("fix: changing customer service port", zap.Int("old", config.CustomerPort), zap.Int("new", fixCustomerPort)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for "fix: " prefix
Please make sure all commits are signed. you can git commit --amend -s If you already missed signing of an earlier commit, please squash them. |
Thanks for the PR |
749095b
to
ddbc1a7
Compare
Wasn't able to squash them properly and thus made more of a mess in this PR thread by rebasing commits. The new commits are at least signed off. My apologies for the inconvenience. |
ddbc1a7
to
8cef0fe
Compare
fyi, I added some tips on how to squash (the easiest way I know of): |
// Port settings, can be overwritten from command line | ||
|
||
// Customer Service Port | ||
CustomerPort = 8081 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: why do we need these in the config? The variables that hold the parsed flag values are already in the same package as the commands for services, so we can access the variables directly.
I am curious about the other command-local flags like the "interface":
Not quite sure why they were added as flags (here #694, cc @gbaufake), because they suffer from the same problem as the port numbers, i.e. if the flag is local to the command then the other services won't be able to find the given service if it runs on a non-default IP/interface. |
I did spend some time trying to directly access the flags provided, but I found that when searching for the proper flags in the customer.go init() function, as well as in the customerCmd function, the flags are not yet parsed. In other words, when I called RootCmd.Flags().Visit(), and set up a simple function to print any flags that are provided, nothing was printed. I read on the cobra docs that the flags are not passed in until the command is executed, which is why I think we see this behavior. |
I understand that, but the value is used inside each command's execute method
so instead of reading from config you should be able to read directly from |
85d487f
to
b7d0540
Compare
I can clean up the usage of command-local flags for server interface as well. I think it may be easier to follow a similar approach to the port flags, and have default values in the root.go file that can be changed with flags. This would remove the need for the customerOptions struct altogether, and the init function in each service would just have the one line:
|
Also, I just discovered that in each service's client.go file, the service searches on the original hardcoded hostPort value, so it does not find the service if the port has been changed.
or in services/route/route.go line 56:
I am looking into how to share the variable across packages, but maybe having them in the config file is the way to go? I appreciate your help and patience on something so minor |
riiiiight... Yes, we need all those references to be accessible from some central location or passed in a combined configOptions struct, similar to the former customerOptions but shared between all services. My preference is the options struct rather than using global variables in /config/ |
So if taking the configOptions struct approach, it would be one struct containing the ports for each service? Then this one struct could be created in the cmd/frontend.go file, which would create a new frontend server with the struct, which would then pass each port to the proper service when it spins up each service client? Gonna get to work. |
2bbb3ec
to
43a5d22
Compare
Signed-off-by: Corey Boornazian <coreyb220@gmail.com> Update client.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com>
c8a12fa
to
327f1d1
Compare
Signed-off-by: Corey Boornazian <coreyb220@gmail.com> Fix typo on root.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com>
74b987f
to
589331d
Compare
Hello, just wondering if there was anything else I could add/change to get this PR accepted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, didn't see the last update.
LGTM! Thanks for your contribution.
) * Add ConfigOptions so clients find correct ports Signed-off-by: Corey Boornazian <coreyb220@gmail.com> Update client.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com> * Fix typo on client.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com> Fix typo on root.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com>
Which problem is this PR solving?
Resolves #948
Short description of the changes
Added four flags, one for each service (driver "-d", route "-r", customer "-c", and frontend "-f") so that a user can specify on the command line which port they want each service to be run on. The ports get added to the services/config file and are checked before starting each server. The default port values are still used if the user does not add these flags.
Signed-off-by: Corey Boornazian coreyb220@gmail.com