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

feat: add Stop method #110

Merged
merged 1 commit into from
Oct 1, 2024
Merged

feat: add Stop method #110

merged 1 commit into from
Oct 1, 2024

Conversation

hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Sep 30, 2024

📑 Description

Summary by CodeRabbit

  • New Features

    • Introduced a new Stop method for gracefully shutting down the server, replacing the deprecated Shutdown method.
    • Added RunTLSWithCert method for running a TLS server with specified certificate and key files.
  • Bug Fixes

    • Updated server methods to ensure correct handling of requests during stop operations.
  • Documentation

    • Improved comments for clarity on method deprecations and new functionalities.
  • Tests

    • Updated test cases to reflect the renaming of Shutdown to Stop and ensure proper functionality with the new methods.

✅ Checks

  • Added test cases for my code

@hwbrzzl hwbrzzl requested a review from a team as a code owner September 30, 2024 08:31
Copy link

coderabbitai bot commented Sep 30, 2024

Walkthrough

The pull request introduces updates to the go.mod file, reflecting changes in various module dependencies, including version increments for several packages. Additionally, the route.go file undergoes modifications, with the deprecation of the Shutdown method in favor of a new Stop method, which maintains backward compatibility. The RunTLS method is also updated to include a new RunTLSWithCert method for enhanced TLS server functionality. Corresponding changes are made in route_test.go to align test cases with the updated method names.

Changes

Files Change Summary
go.mod Updated module dependencies: github.com/goravel/framework, cloud.google.com/go/compute/metadata, golang.org/x/oauth2, google.golang.org/genproto/googleapis/api, google.golang.org/genproto/googleapis/rpc, and google.golang.org/grpc to newer versions.
route.go, route_test.go Deprecated Shutdown method in route.go and introduced Stop method; updated RunTLS to call RunTLSWithCert. Corresponding test cases in route_test.go renamed from TestShutdown to TestStop to reflect method changes.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Send request
    Server-->>Client: Process request
    Client->>Server: Stop request
    Server-->>Client: Acknowledge stop
    Client->>Server: Send request (after stop)
    Server-->>Client: Reject request (no new requests accepted)
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (4)
route.go (3)

188-191: LGTM! Consider adding more details to the deprecation notice.

The deprecation of the Shutdown method is correctly implemented, maintaining backward compatibility by calling the new Stop method.

Consider expanding the deprecation notice to include when it will be removed and why Stop is preferred:

-// DEPRECATED Use Stop instead
+// DEPRECATED: Use Stop instead. This method will be removed in v2.0.0. Stop provides more flexibility with context handling.

Line range hint 193-203: LGTM! Consider adding error logging.

The Stop method is well-implemented, providing a clean way to gracefully shut down the server with optional context support.

Consider logging the error returned by ShutdownWithContext before returning it:

 func (r *Route) Stop(ctx ...context.Context) error {
 	c := context.Background()
 	if len(ctx) > 0 {
 		c = ctx[0]
 	}

-	return r.instance.ShutdownWithContext(c)
+	err := r.instance.ShutdownWithContext(c)
+	if err != nil {
+		// Assuming you have a logger available
+		// log.Error("Error shutting down server", err)
+	}
+	return err
 }

This would provide more visibility into shutdown errors without changing the method's behavior.


Line range hint 114-150: LGTM! Consider unifying error messages for consistency.

The updates to RunTLS and the addition of RunTLSWithCert improve the flexibility of TLS configuration. The error checking is thorough and the implementation is clean.

For consistency, consider unifying the error messages:

 func (r *Route) RunTLS(host ...string) error {
 	if len(host) == 0 {
 		defaultHost := r.config.GetString("http.tls.host")
 		defaultPort := r.config.GetString("http.tls.port")
 		if defaultPort == "" {
-			return errors.New("port can't be empty")
+			return errors.New("TLS port can't be empty")
 		}
 		completeHost := defaultHost + ":" + defaultPort
 		host = append(host, completeHost)
 	}

 	certFile := r.config.GetString("http.tls.ssl.cert")
 	keyFile := r.config.GetString("http.tls.ssl.key")

 	return r.RunTLSWithCert(host[0], certFile, keyFile)
 }

 // RunTLSWithCert run TLS server with cert file and key file
 // RunTLSWithCert 使用证书文件和密钥文件运行 TLS 服务器
 func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error {
 	if host == "" {
-		return errors.New("host can't be empty")
+		return errors.New("TLS host can't be empty")
 	}
 	if certFile == "" || keyFile == "" {
-		return errors.New("certificate can't be empty")
+		return errors.New("TLS certificate or key can't be empty")
 	}

 	r.outputRoutes()
 	color.Green().Println(termlink.Link("[HTTPS] Listening and serving HTTPS on", "https://"+host))

 	return r.instance.ListenTLS(host, certFile, keyFile)
 }

This makes the error messages more specific and consistent across both methods.

route_test.go (1)

Line range hint 405-491: LGTM: TestStop function implementation

The TestStop function effectively tests the behavior of the Stop method, covering important scenarios such as rejecting new requests after stopping and processing received requests before stopping. The test cases are well-structured and provide good coverage of the functionality.

Consider using a more robust synchronization mechanism

While time.Sleep is used for synchronization, it might not be reliable in all environments or under different load conditions. Consider using a more robust synchronization mechanism, such as channels or sync.WaitGroup, to ensure the server has started before making requests and to wait for all goroutines to complete.

Here's an example of how you could improve the synchronization:

 go func() {
+    serverReady := make(chan struct{})
+    go func() {
+        // Signal that the server is ready
+        serverReady <- struct{}{}
         assert.Nil(t, route.Run())
+    }()
+    // Wait for the server to be ready
+    <-serverReady
 }()

-time.Sleep(1 * time.Second)

Enhance error handling in the setup function

The current implementation ignores the error returned by the setup function. Consider handling this error explicitly to ensure that any setup failures are properly reported.

Here's a suggestion to improve error handling:

-if err := test.setup(); err == nil {
-    assert.Nil(t, err)
+if err := test.setup(); err != nil {
+    t.Fatalf("Test setup failed: %v", err)
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 97a7a60 and d894e41.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • go.mod (3 hunks)
  • route.go (1 hunks)
  • route_test.go (3 hunks)
🔇 Additional comments (9)
route.go (1)

Line range hint 1-238: Overall, the changes look good and improve the codebase.

The updates to the Route struct and its methods enhance the server's shutdown process and TLS configuration flexibility. The code maintains backward compatibility while introducing new, more flexible methods. The minor suggestions provided will further improve code consistency and error handling.

go.mod (7)

23-23: Approve the Google Cloud compute metadata update and review new features.

The cloud.google.com/go/compute/metadata dependency has been updated from v0.3.0 to v0.5.0. This minor version update likely includes new features and improvements.

It's recommended to review the changelog for any new features or improvements that could benefit your project. You can find the changelog at: https://github.com/googleapis/google-cloud-go/blob/main/compute/metadata/CHANGES.md


154-154: Approve the OAuth2 library update and review new features.

The golang.org/x/oauth2 dependency has been updated from v0.21.0 to v0.22.0. This minor version update likely includes new features and improvements.

It's recommended to review the changelog for any new features or improvements that could benefit your project. You can find the release notes at: https://github.com/golang/oauth2/releases


163-163: Approve the Google RPC proto update.

The google.golang.org/genproto/googleapis/rpc dependency has been updated to a more recent commit (dated 2024-08-14), consistent with the previous googleapis/api update.

This update aligns with the googleapis/api update, which is a good practice for maintaining consistency across related Google API dependencies.


164-164: Approve the gRPC update and review new features.

The google.golang.org/grpc dependency has been updated from v1.66.2 to v1.67.0. This minor version update likely includes new features and improvements.

It's recommended to review the changelog for any new features or improvements that could benefit your project. You can find the release notes at: https://github.com/grpc/grpc-go/releases/tag/v1.67.0


Line range hint 1-180: Summary of dependency updates

This update includes several minor version bumps and commit-specific updates to various dependencies. These changes are likely to include bug fixes, performance improvements, and new features. Here's a summary of the key updates:

  1. github.com/goravel/framework: Updated to a newer development version.
  2. cloud.google.com/go/compute/metadata: Minor version update (v0.3.0 to v0.5.0).
  3. golang.org/x/oauth2: Minor version update (v0.21.0 to v0.22.0).
  4. google.golang.org/genproto/googleapis/api and google.golang.org/genproto/googleapis/rpc: Updated to a more recent commit.
  5. google.golang.org/grpc: Minor version update (v1.66.2 to v1.67.0).

These updates represent good maintenance practices. However, it's important to:

  1. Review the changelogs of updated dependencies for any new features or breaking changes.
  2. Run your test suite to ensure compatibility with the updated dependencies.
  3. Monitor your application's performance after deploying these changes to production.

162-162: Approve the Google API proto update and verify stability.

The google.golang.org/genproto/googleapis/api dependency has been updated to a more recent commit (dated 2024-08-14).

As this is a commit-specific update rather than a semantic version, please ensure that this version is stable and compatible with your project. Run the following command to check for any significant changes:

#!/bin/bash
# Description: Check for significant changes in the Google API proto update.

# Test: Compare the old and new versions
go mod download google.golang.org/genproto/googleapis/[email protected]
go mod download google.golang.org/genproto/googleapis/[email protected]

# List changes between the two versions
git diff --no-index $(go env GOPATH)/pkg/mod/google.golang.org/genproto/googleapis/[email protected] $(go env GOPATH)/pkg/mod/google.golang.org/genproto/googleapis/[email protected]

Review the output for any breaking changes or significant modifications that might affect your codebase.


11-11: Approve the framework update and verify compatibility.

The github.com/goravel/framework dependency has been updated to a newer development version. This update is likely to include bug fixes and minor improvements.

Please ensure that this update is compatible with your current implementation. Run the following command to check for any breaking changes or deprecations:

Review the output for any breaking changes or deprecations that might affect your codebase.

route_test.go (1)

431-431: Consistent use of the Stop method

The change from Shutdown to Stop is correctly implemented in the test cases. This renaming is consistent with the changes mentioned in the summary and maintains the expected functionality while using a more intuitive method name.

Also applies to: 456-456

Copy link
Member

@kkumar-gcc kkumar-gcc left a comment

Choose a reason for hiding this comment

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

Great!

@hwbrzzl hwbrzzl merged commit 7eb0339 into master Oct 1, 2024
7 of 8 checks passed
@hwbrzzl hwbrzzl deleted the bowen/add-stop branch October 1, 2024 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants