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

Fix Using ws:// With Self Hosted #269

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

davidvonthenen
Copy link
Contributor

@davidvonthenen davidvonthenen commented Nov 6, 2024

Proposed changes

This fixes an issue with using ws:// and Self-Hosted Configurations.

Also:

  • Fixes a bunch of logging messages that were incorrect

Types of changes

What types of changes does your code introduce to the community Go SDK?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have lint'ed all of my code using repo standards
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

NA

Summary by CodeRabbit

  • New Features
    • Enhanced WebSocket connection setup with improved flexibility for secure and non-secure connections.
  • Bug Fixes
    • Improved error handling and logging clarity for binary message processing in both the WSCallback and WSChannel structures.
  • Chores
    • Updated logging namespaces for consistency across WebSocket client methods.

Copy link

coderabbitai bot commented Nov 6, 2024

Walkthrough

This pull request introduces modifications to the WebSocket handling and API URL construction in the codebase. Key changes include updates to the getAPIURL function to enhance protocol handling for WebSocket connections. Additionally, the WSClient struct in websocket.go has undergone updates for improved logging, error handling, and connection management. Similar logging improvements were made in the ProcessMessage methods of both WSCallback and WSChannel structs to ensure accurate error reporting for binary messages. Overall, the changes focus on enhancing clarity and maintainability without altering the core functionalities.

Changes

File Path Change Summary
pkg/api/version/version.go Modified getAPIURL function to expand apiType conditions for WebSocket protocols; added logging statements for URL construction.
pkg/client/common/v1/websocket.go Updated WSClient struct methods with logging changes, error handling improvements, and added gocyclo directive for internalConnectWithCancel.
pkg/client/speak/v1/websocket/client_callback.go Updated logging in ProcessMessage method to accurately reflect handling of binary messages.
pkg/client/speak/v1/websocket/client_channel.go Updated logging in ProcessMessage method to accurately reflect handling of binary messages.

Possibly related PRs

  • Reintroduce TTS WS #258: The changes in this PR involve WebSocket functionalities, which are relevant to the modifications made to the getAPIURL function in the main PR, particularly regarding the handling of WebSocket protocols.

Suggested reviewers

  • lukeocodes
  • jpvajda
  • naomi-lgbt

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: 1

🧹 Outside diff range and nitpick comments (2)
pkg/api/version/version.go (1)

40-42: Consider adding security warning for ws:// usage.

The regex pattern correctly handles both ws:// and wss:// protocols, which aligns with the PR objective for self-hosted support. However, consider adding a warning log when ws:// is used to ensure users are aware of the security implications.

 	if apiType == APITypeLive || apiType == APITypeSpeakStream {
 		r = regexp.MustCompile(`^(wss?)://(.+)$`)
+		// Add warning for insecure WebSocket usage
+		if strings.HasPrefix(host, "ws://") {
+			klog.Warningf("Using insecure WebSocket protocol (ws://) - This is not recommended for production environments")
+		}
 	}
pkg/client/common/v1/websocket.go (1)

Line range hint 553-595: Consider error handling in close sequence.

While the close sequence is well-structured, the error handling in closeStream and normalClosure is silently ignored with _ = c.closeStream(false). Consider logging these errors at debug level for troubleshooting connection closure issues.

-		_ = c.closeStream(false)
+		if err := c.closeStream(false); err != nil {
+			klog.V(6).Infof("Error during stream closure: %v\n", err)
+		}
-		_ = c.normalClosure(false)
+		if err := c.normalClosure(false); err != nil {
+			klog.V(6).Infof("Error during normal closure: %v\n", err)
+		}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 85b9256 and 268d1d2.

📒 Files selected for processing (4)
  • pkg/api/version/version.go (3 hunks)
  • pkg/client/common/v1/websocket.go (23 hunks)
  • pkg/client/speak/v1/websocket/client_callback.go (1 hunks)
  • pkg/client/speak/v1/websocket/client_channel.go (1 hunks)
🔇 Additional comments (10)
pkg/api/version/version.go (2)

101-101: LGTM! Logging improvements enhance debugging capabilities.

The added logging statements provide valuable visibility into the URL construction process, using appropriate verbosity levels consistent with the rest of the file.

Also applies to: 110-110


Line range hint 40-110: Verify WebSocket protocol handling across the codebase.

Let's ensure consistent protocol handling across the codebase.

✅ Verification successful

Let me gather more context about the WebSocket protocol handling by checking the implementation details.


WebSocket protocol handling is consistent and secure across the codebase.

The implementation shows a well-structured and secure approach to WebSocket protocol handling:

  • The protocol is properly defined as a constant WSProtocol = "wss" (secure WebSocket)
  • The WebSocket client implementation correctly handles both ws:// and wss:// protocols with appropriate TLS configuration
  • Protocol selection is consistent with the API type (APITypeLive or APITypeSpeakStream)
  • The client includes proper connection security with TLS configuration and authentication headers
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for WebSocket protocol handling and any hardcoded protocols

# Test 1: Check for WebSocket protocol handling
echo "Checking WebSocket protocol handling..."
rg -l "ws://" || rg -l "wss://"

# Test 2: Check for protocol-related constants or variables
echo "Checking protocol-related definitions..."
rg -i "protocol.*=.*ws" || rg -i "protocol.*=.*wss"

# Test 3: Check for URL construction with WebSocket protocols
echo "Checking URL construction with WebSocket protocols..."
ast-grep --pattern 'url.URL{Scheme: $protocol'

Length of output: 526


Script:

#!/bin/bash
# Check the WebSocket protocol implementation details

# Show the constants file content
echo "Checking constants definition..."
cat pkg/api/version/constants.go

# Show the websocket client implementation
echo -e "\nChecking WebSocket client implementation..."
cat pkg/client/common/v1/websocket.go

# Check for any other protocol-related code
echo -e "\nChecking other protocol usage..."
rg "WSProtocol" -A 3 -B 3

Length of output: 18489

pkg/client/speak/v1/websocket/client_callback.go (1)

Line range hint 63-69: Verify WebSocket protocol handling for self-hosted setups.

While the logging improvements are good, let's verify the main PR objective regarding ws:// protocol handling:

#!/bin/bash
# Search for WebSocket URL/protocol handling logic
echo "Checking WebSocket protocol handling implementation:"
rg -l "ws://"

echo -e "\nChecking URL construction logic:"
ast-grep --pattern 'func GetSpeakStreamAPI($$$) {
  $$$
}'
pkg/client/speak/v1/websocket/client_channel.go (2)

92-92: LGTM! Logging message correction improves clarity.

The updated error message now correctly reflects that the error occurred in the binary message handling (router.Binary) rather than generic message handling, making the logs more accurate and easier to debug.


Line range hint 63-70: Verify WebSocket URL protocol handling for self-hosted configurations.

Since this PR aims to fix ws:// protocol handling for self-hosted configurations, we should verify that the URL construction through version.GetSpeakStreamAPI correctly handles both ws:// and wss:// protocols.

pkg/client/common/v1/websocket.go (5)

193-208: LGTM: Proper handling of ws:// protocol.

The implementation correctly handles both secure and non-secure WebSocket connections by configuring the dialer based on the URL scheme. For ws:// URLs, TLS is disabled, while for wss:// URLs, TLS is enabled with the configured server authentication settings.


102-106: LGTM: Improved mutex handling in error paths.

The code now consistently unlocks the mutex in all error paths, preventing potential deadlocks. This is a good improvement to the error handling logic.

Also applies to: 115-119, 134-138, 185-188, 227-230, 251-256


Line range hint 263-393: LGTM: Comprehensive error handling in WebSocket listener.

The error handling is well-structured and covers all important cases:

  • Panic recovery with proper cleanup
  • Graceful socket closure detection
  • Fatal socket errors with appropriate error propagation
  • Deepgram-specific error handling
  • EOF conditions

467-469: LGTM: Enhanced debugging with payload logging.

The addition of payload logging at debug level helps with troubleshooting while maintaining security by using appropriate log levels.


193-208: Verify the WebSocket protocol handling in self-hosted environments.

The changes correctly implement the PR objective of fixing ws:// protocol usage. To ensure the changes work as expected in self-hosted environments:

  1. Test with both ws:// and wss:// URLs
  2. Verify TLS settings are correctly applied based on the URL scheme
✅ Verification successful

Let me gather more information about the URL construction and WebSocket configuration to ensure comprehensive verification.


WebSocket protocol handling implementation is correctly implemented

The verification confirms that:

  1. The WebSocket URL scheme detection (ws://) is properly implemented in pkg/client/common/v1/websocket.go
  2. TLS configuration is correctly disabled for ws:// and enabled for other schemes (implicitly wss://)
  3. URL construction is consistently handled through proper getter methods across different client implementations
  4. The SkipServerAuth option is properly propagated to both WebSocket and TLS configurations

The changes successfully achieve the PR objective of supporting ws:// protocol in self-hosted environments while maintaining secure defaults for other cases.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify the WebSocket URL handling across the codebase
echo "Checking WebSocket URL construction and usage..."
rg -A 5 "ws://" || echo "No hardcoded ws:// URLs found (good)"
rg -A 5 "wss://" || echo "No hardcoded wss:// URLs found (good)"

# Check for proper URL construction
ast-grep --pattern 'GetURL($$$)'

Length of output: 774


Script:

#!/bin/bash
# Check for URL construction and WebSocket configuration patterns
echo "Checking URL construction patterns..."
ast-grep --pattern 'url := $$$'

echo "\nChecking WebSocket configuration and options..."
ast-grep --pattern 'type $_ struct {
  $$$
  SkipServerAuth bool
  $$$
}'

echo "\nChecking for TLS configuration patterns..."
rg -A 5 "TLSClientConfig"

Length of output: 1912

@davidvonthenen davidvonthenen merged commit 7d9d003 into deepgram:main Nov 7, 2024
3 checks passed
@davidvonthenen davidvonthenen deleted the fix-ws-self-hosted branch November 7, 2024 14:30
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