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

Port WebSocket Threading Fixes STT Live -> TTS Speak, Port Fixes From TTS Speak -> STT Live #246

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

dvonthenen
Copy link
Contributor

@dvonthenen dvonthenen commented Jun 21, 2024

Proposed changes

This ports fixes from the STT WebSocket to the TTS WebSocket client. Please see for more details:
#243
#245

This also ports fixes made in TTS WebSocket -> STT WebSocket client made in this PR:
#239

Some other notes on changes:

  • I removed the directory creation on tts-output since the mp3 file is getting overwritten on subsequent streams.
  • updated stream micro example to use updated error response struct
  • fixed examples/prerecorded/file to fix the VTT and SRT

Tested STT and TTS examples!

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

Copy link

coderabbitai bot commented Jun 21, 2024

Warning

Rate limit exceeded

@dvonthenen has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 47 minutes and 5 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between ed94f73 and ba6f846.

Walkthrough

The recent changes encompass a mix of adding new settings, refactoring code, simplifying file paths, and consolidating types for better code maintainability. Additionally, error message handling has been improved, constants have been updated, and new utility functions have been introduced for managing websocket connections and logging.

Changes

Files Change Summary
examples/prerecorded/file/main.go Added Utterances: true to Deepgram client configuration.
examples/speak/stream/interactive/main.go Removed AUDIO_DIR constant, simplified file path generation, removed SpeakStream method.
examples/streaming/microphone/main.go Changed error message to include ErrCode rather than Message.
pkg/api/live/v1/default.go Renamed error fields Type to ErrCode and Message to ErrMsg.
pkg/api/live/v1/interfaces/types.go Aliased LiveOptions and ErrorResponse to interfaces structs.
pkg/api/speak-stream/v1/interfaces/types.go Removed SpeakStreamResponse struct and its fields.
pkg/client/interfaces/utils.go Removed RequestID field from DeepgramError struct.
pkg/client/live/client.go Renamed log messages, added message type differentiation, new functions for managing websocket connections.
pkg/client/live/constants.go Added MessageTypeKeepAlive, removed MessageTypeCloseStream, added new error messages.
pkg/client/speak/client_stream.go Added new controlMessage type, updated function signatures, new websocket management functions.
pkg/client/speak/constants.go Added new message type constants and an error message, modified existing error constants.
Expanded Changes Table
Files Change Summary
examples/prerecorded/file/main.go - Added Utterances: true to Deepgram client configuration.
examples/speak/stream/interactive/main.go - Removed AUDIO_DIR constant. - Simplified file path generation in Binary method of MyCallback. - Removed SpeakStream method.
examples/streaming/microphone/main.go - Changed error message in MyCallback function to include ErrCode instead of Message from api.ErrorResponse.
pkg/api/live/v1/default.go - Renamed error fields in Error struct within DefaultCallbackHandler from Type to ErrCode and from Message to ErrMsg.
pkg/api/live/v1/interfaces/types.go - Changed type LiveOptions to alias interfaces.LiveTranscriptionOptions. - Changed type ErrorResponse to alias interfaces.DeepgramError.
pkg/api/speak-stream/v1/interfaces/types.go - Removed SpeakStreamResponse struct and its fields (ContentType, RequestID, ModelUUID, ModelName, and Date).
pkg/client/interfaces/utils.go - Removed RequestID field from DeepgramError struct.
pkg/client/live/client.go - Changed log messages for clarity. - Added differentiation for message types (binary or text). - Modified error handling in various methods. - Changed the field name from Message to ErrMsg in the errorToResponse function for error response handling.
pkg/client/live/constants.go - Added MessageTypeKeepAlive, removed MessageTypeCloseStream. - Added error messages for ErrInvalidInput and ErrInvalidConnection.
pkg/client/speak/client_stream.go - Added controlMessage type. - Modified signatures for Connect, ConnectWithCancel, AttemptReconnect, AttemptReconnectWithCancel, internalConnect, and internalConnectWithCancel with an additional parameter. - Added functions listen, closeStream, normalClosure. - Updated WriteJSON logic. - Added Reset function.
pkg/client/speak/constants.go - Added new constants: MessageTypeFlush, MessageTypeReset, MessageTypeClose. - Added error message UseOfClosedSocket. - Modified error constants ErrInvalidInput and ErrInvalidConnection.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@dvonthenen dvonthenen force-pushed the port-timing-and-drain-fix branch 2 times, most recently from ad1cb20 to ed94f73 Compare June 21, 2024 17:11
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d56e144 and ed94f73.

Files selected for processing (11)
  • examples/prerecorded/file/main.go (1 hunks)
  • examples/speak/stream/interactive/main.go (2 hunks)
  • examples/streaming/microphone/main.go (1 hunks)
  • pkg/api/live/v1/default.go (1 hunks)
  • pkg/api/live/v1/interfaces/types.go (2 hunks)
  • pkg/api/speak-stream/v1/interfaces/types.go (1 hunks)
  • pkg/client/interfaces/utils.go (1 hunks)
  • pkg/client/live/client.go (8 hunks)
  • pkg/client/live/constants.go (2 hunks)
  • pkg/client/speak/client_stream.go (17 hunks)
  • pkg/client/speak/constants.go (1 hunks)
Files skipped from review due to trivial changes (4)
  • examples/prerecorded/file/main.go
  • pkg/api/live/v1/interfaces/types.go
  • pkg/api/speak-stream/v1/interfaces/types.go
  • pkg/client/interfaces/utils.go
Additional context used
Learnings (1)
pkg/client/speak/client_stream.go (2)
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:150-277
Timestamp: 2024-06-19T14:40:11.197Z
Learning: The `internalConnectWithCancel` method in `pkg/client/speak/client_stream.go` is complex and is planned to be refactored in the future. This is tracked in issue https://github.com/deepgram/deepgram-go-sdk/issues/242.
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: examples/speak/stream/http/main.go:124-127
Timestamp: 2024-06-19T14:42:20.980Z
Learning: The `Connect` method in the `pkg/client/speak/client_stream.go` returns a boolean, and any errors during the connection are handled through the `Error` method in the `MyCallback` struct.
GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 424-424:
ifElseChain: rewrite if-else to switch statement (gocritic)

Additional comments not posted (18)
pkg/client/speak/constants.go (4)

27-27: Added constant for closed socket usage.

The constant UseOfClosedSocket has been added to handle specific socket closure errors. This is a good practice as it helps in precise error handling and improves the maintainability of error management.


49-49: Added new error constant for invalid input.

The addition of ErrInvalidInput is a good practice, as it provides a more specific error message for input validation failures. This enhances the robustness of error handling.


49-49: Added new error constant for invalid connections.

The constant ErrInvalidConnection has been introduced to handle errors related to invalid connections. This is useful for debugging and error reporting, ensuring that connection issues are clearly communicated to the developers and users.


36-42: Review of new message types.

The addition of MessageTypeFlush, MessageTypeReset, and MessageTypeClose extends the functionality for managing stream states. Ensure these constants are integrated properly in the rest of the codebase where stream states are managed.

pkg/client/live/constants.go (1)

39-41: New constant for keeping the connection alive.

The introduction of MessageTypeKeepAlive is crucial for maintaining live connections, especially in streaming scenarios. This constant should be used to ensure the connection remains active during periods of inactivity.

examples/speak/stream/interactive/main.go (1)

34-34: Refactor file path creation in Binary method.

The change to simplify the file path creation in the Binary method is a positive step towards simplifying the code. However, ensure that the AUDIO_FILE constant is defined in a way that it does not lead to conflicts or unintended behavior, especially in environments with different file system permissions or configurations.

Verification successful

The AUDIO_FILE constant is only defined and used within examples/speak/stream/interactive/main.go. There are no other instances of its usage in the codebase, indicating no conflicts or unintended behavior.

  • examples/speak/stream/interactive/main.go
    • AUDIO_FILE is defined as "output.mp3"
    • AUDIO_FILE is used in the filePath variable.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the AUDIO_FILE constant is defined correctly and does not cause conflicts.

# Test: Check the definition and usage of AUDIO_FILE constant.
rg --type go $'AUDIO_FILE'

Length of output: 179



Script:

#!/bin/bash
# Description: Check for other instances of AUDIO_FILE usage in the codebase to ensure no conflicts or unintended behavior.

# Search for all occurrences of AUDIO_FILE in the codebase.
rg AUDIO_FILE

Length of output: 169

examples/streaming/microphone/main.go (1)

91-91: Updated error handling to use ErrCode.

The change to use ErrCode for error handling instead of Type aligns with the SDK's strategy to standardize error codes across different modules. This update should help in better categorization and handling of errors.

pkg/api/live/v1/default.go (1)

252-253: Update error handling to use new fields.

The error handling in the Error method has been updated to use ErrCode and ErrMsg fields instead of Message. This aligns with the changes mentioned in the PR summary and improves the clarity and specificity of error messages.

pkg/client/speak/client_stream.go (3)

30-32: Addition of controlMessage type.

The addition of the controlMessage type, which includes a Type field, is a good practice for managing different types of WebSocket messages. This will help in organizing the control messages sent over the WebSocket connection.


127-127: Ensure connection methods handle new parameter correctly.

The connection and reconnection methods (Connect, ConnectWithCancel, AttemptReconnect, AttemptReconnectWithCancel) have been modified to include a new boolean parameter lock. This parameter is important for thread safety when accessing the WebSocket connection. Please verify that the lock parameter is being used correctly across all methods to avoid potential race conditions.

Also applies to: 133-133, 140-140, 146-146

Verification successful

The internalConnectWithCancel method implementations in both pkg/client/speak/client_stream.go and pkg/client/live/client.go correctly utilize the lock parameter to conditionally lock the muConn mutex for thread safety. The lock parameter is checked, and if true, the muConn.Lock() method is called, followed by defer c.muConn.Unlock() to ensure the mutex is properly unlocked.

Ensure connection methods handle new parameter correctly.

The lock parameter is used correctly in the internalConnectWithCancel method to manage thread safety when accessing the WebSocket connection.

  • pkg/client/speak/client_stream.go: Lines 154-289
  • pkg/client/live/client.go: Lines 154-295
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct usage of the `lock` parameter in connection methods.

# Test: Search for the method usage. Expect: Correct handling of the `lock` parameter.
rg --type go $'internalConnectWithCancel'

Length of output: 3137



Script:

#!/bin/bash
# Locate the implementation of the `internalConnectWithCancel` method in Go files.
ast-grep --lang go --pattern $'func (c *$_) internalConnectWithCancel($_, $_, $_, $_) $_' 

Length of output: 18230


154-154: > [!TIP]

Codebase Verification

Ensure Test Coverage for Error Paths in the listen Method

It appears that there are no direct tests for the listen method in the test files. To ensure robust error handling and reduce complexity, please add comprehensive tests covering all error scenarios in the listen method.

  • File: pkg/client/speak/client_stream.go
  • Method: listen

Action Required:

  • Add test cases for each error path in the listen method to ensure all scenarios are covered and handled correctly.
Analysis chain

Complexity and error handling in the listen method.

The listen method is highly complex and handles various error scenarios. It's important to ensure that all error paths are tested thoroughly to prevent unexpected behavior. Additionally, consider refactoring to reduce complexity and improve readability.

- // existing complex method
+ // proposed simplified method with better error handling

Also applies to: 279-279, 291-291, 305-305, 315-315, 321-321, 323-323, 327-327, 335-335, 343-343, 347-347, 352-352, 355-355, 361-361, 365-365, 371-371, 375-375, 381-381, 385-385, 389-389, 393-393, 395-395, 397-397, 403-403, 405-405

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all error paths in the `listen` method are covered by tests.

# Test: Check test coverage for the `listen` method.
rg --type go $'listen'

Length of output: 5589



Script:

#!/bin/bash
# Description: Extract the implementation of the `listen` method from `pkg/client/speak/client_stream.go` to review its complexity.
ast-grep --lang go --pattern 'func (c *StreamClient) listen() { $$$ }' pkg/client/speak/client_stream.go

Length of output: 10057



Script:

#!/bin/bash
# Description: Verify if all error paths in the `listen` method are covered by tests.

# Search for test functions that call the `listen` method.
rg --type go 'listen()' | grep '_test.go'

Length of output: 246

Skipped due to learnings
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:150-277
Timestamp: 2024-06-19T14:40:11.197Z
Learning: The `internalConnectWithCancel` method in `pkg/client/speak/client_stream.go` is complex and is planned to be refactored in the future. This is tracked in issue https://github.com/deepgram/deepgram-go-sdk/issues/242.
pkg/client/live/client.go (7)

214-214: Improved logging for restored headers:

The addition of detailed logging for restored headers is beneficial for debugging purposes, especially when tracing the flow of headers through the system.


329-329: Logging for empty messages:

Adding a log for empty messages can help in identifying issues with message flows or unexpected empty transmissions, which is useful for debugging.


431-438: Handling text messages in WebSocket communications:

The logic to handle text messages and route them through router.Message is crucial for processing received messages. The added logging for message processing failures enhances error visibility, which is essential for maintaining robust communication channels.


441-441: Log for unhandled message types:

Logging a warning when an unexpected message type is received is a good practice. It helps in diagnosing issues when the system behaves unexpectedly, contributing to better fault tolerance.


543-543: Improved error handling in JSON marshaling:

Explicitly logging marshaling errors in WriteJSON helps in quickly identifying serialization issues, which can be critical in systems relying on structured data exchange.


600-602: Enhanced KeepAlive functionality:

Implementing and logging the KeepAlive functionality with error handling is a good practice for maintaining active WebSocket connections. This is crucial for ensuring that connections remain alive during periods of inactivity.


617-617: Finalize function implementation:

The addition of a Finalize function to send a control message for session termination is a good practice. It allows for clean session terminations, reducing potential resource leaks or session hang-ups.

pkg/client/speak/client_stream.go Show resolved Hide resolved
@dvonthenen dvonthenen changed the title Port WebSocket Threading Fixes Live -> Speak, Port Fixes From Speak - Live Port WebSocket Threading Fixes Live -> Speak, Port Fixes From Speak -> Live Jun 21, 2024
@dvonthenen dvonthenen changed the title Port WebSocket Threading Fixes Live -> Speak, Port Fixes From Speak -> Live Port WebSocket Threading Fixes STT Live -> TTS Speak, Port Fixes From TTS Speak -> STT Live Jun 21, 2024
@dvonthenen dvonthenen merged commit 2c83727 into deepgram:main Jun 21, 2024
2 checks passed
@dvonthenen dvonthenen deleted the port-timing-and-drain-fix branch June 21, 2024 17:27
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