Skip to content

Conversation

@DRadmir
Copy link
Contributor

@DRadmir DRadmir commented Dec 4, 2025

Refactor PriceObserverService to extract WebSocket connection logic into a reusable WebSocketClient package. This
enables adding new WebSocket connections in the future (e.g., perpetuals, real-time order updates).

Changes

  • Add WebSocketClient package with WebSocketConnection actor
  • Simplify PriceObserverService to focus on business logic only
  • Add WebSocketConnectionMock for testing

@DRadmir DRadmir self-assigned this Dec 4, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @DRadmir, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new, reusable WebSocketClient package to abstract and centralize WebSocket connection management. This refactoring decouples the PriceObserverService from the low-level details of WebSocket communication, making it more modular, testable, and adaptable for future features requiring WebSocket connections, such as real-time order updates or perpetuals.

Highlights

  • New WebSocketClient Package: A dedicated WebSocketClient package has been introduced, encapsulating all WebSocket connection, reconnection, and message handling logic.
  • PriceObserverService Refactoring: The PriceObserverService has been significantly refactored to delegate its WebSocket responsibilities to the new WebSocketClient, simplifying its internal implementation and focusing it on business logic.
  • Improved Testability: A WebSocketConnectionMock has been added within the WebSocketClientTestKit, enabling easier and more reliable testing of components that rely on WebSocket connections.
  • Centralized WebSocket Configuration: A WebSocketConfiguration struct is now used to define connection parameters, promoting consistency and easier management of WebSocket settings.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great refactoring effort that extracts the WebSocket connection logic into a new, reusable WebSocketClient package. This significantly simplifies PriceObserverService and improves the overall architecture by promoting modularity. The new WebSocketClient makes good use of modern Swift concurrency features like actors and AsyncStream.

My review includes one high-severity comment regarding a potential race condition in the new WebSocketConnection implementation. Addressing this will make the new component more robust. Overall, this is a high-quality contribution.

guard let task, state == .connected else {
throw WebSocketError.notConnected
}
try await task.send(.data(data))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should be able to push data in any state, it should just until connected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To support sending data id any state, we'd need to add a pending messages buffer and flush it on connect.

This adds complexity: handling send errors for buffered events, managing buffer grows, and what todo if connections fails? Needs add ping, and after ping decide what to do.

private func handleStreamTermination() {
guard state != .disconnected else { return }

task?.cancel(with: .goingAway, reason: nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

task?.cancel(with: .goingAway, reason: nil) is duplication

Copy link
Contributor

@gemcoder21 gemcoder21 left a comment

Choose a reason for hiding this comment

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

task?.cancel(with: .goingAway, reason: nil) is used in 3 places, you should simplify and consolidate many methods


// MARK: - URLSessionWebSocketTask.Message

extension URLSessionWebSocketTask.Message {
Copy link
Contributor

Choose a reason for hiding this comment

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

add to separate file

@DRadmir DRadmir merged commit 3aafb37 into main Dec 8, 2025
2 checks passed
@DRadmir DRadmir deleted the websocket-engine branch December 8, 2025 05:23
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