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

Cherry pick expander changes #123

Merged
merged 2 commits into from
Dec 13, 2024

Conversation

rrangith
Copy link

Cherry picking the following 2 PRs

kubernetes#6926
kubernetes#6941

Already tested to confirm these work

Both of these PRs have lgtm from upstream

s.RegisterService(&Expander_ServiceDesc, srv)
}

func _Expander_BestOptions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {

Choose a reason for hiding this comment

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

🟠 Code Quality Violation

The Context should be the first argument. (...read more)

In Go, it is a best practice to use the context package and pass a context.Context as the first argument to functions that are expected to be long-running, have potential timeouts, or require cancellation.

Here are the reasons why using context as the first argument is recommended:

  1. Cancellation and timeouts: The context package provides functionality to handle cancellation and timeouts in a clean and controlled way. By passing a context.Context as the first argument, you allow callers of the function to control the lifecycle of the function execution by canceling it or setting timeouts. This promotes better resource management and avoids situations where long-running operations are left unfinished or hang indefinitely.
  2. Propagation of Context: By passing the context.Context as the first argument, you create a clear and consistent pattern for propagating the context throughout your codebase. This allows the context to flow seamlessly across different function calls, enabling features like cancellation and timeout to be propagated correctly to the lower levels of your program.
  3. Testability: Using context as the first argument makes it easier to write unit tests for your functions. You can create a context.Context object with specific cancellation or timeouts in your tests and validate the behavior of your function under different scenarios. This enables better test coverage and ensures that your functions respond correctly to different context states.
  4. Code readability: By making it explicit that your function requires a context.Context, you improve the readability of your code. Developers who read your code can immediately understand the intent and requirements of the function just by looking at the function signature. This makes it easier to reason about the behavior of the function in different context scenarios.

By following the convention of using context as the first argument, you adhere to Go's idiomatic practices, improve code readability, facilitate testability, and allow for better control and propagation of cancellation and timeouts. This helps in building robust, maintainable, and scalable Go codebases.

View in Datadog  Leave us feedback  Documentation

@rrangith rrangith merged commit d565386 into datadog-master-14.0 Dec 13, 2024
7 checks passed
@rrangith rrangith deleted the cherry-pick-expander-changes branch December 13, 2024 19:21
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.

3 participants