WebSocket Server Architecture Refactor & Key Updates
What developments have taken place today?
Today’s commit activity primarily focuses on refactoring the WebSocket server architecture, enhancing security, and improving message handling capabilities. Here’s a breakdown of the changes:
- WebSocket Server Core Refactor: A significant overhaul of the WebSocket server code has occurred, involving the removal of several files related to user data retrieval, email verification, channel management, Redis commands, and WSCommand structs. This suggests a move towards a more modular and streamlined design.
- New Authentication & Authorization: The introduction of
WebsocketAuthenticate
and associated methods implements WebSocket authentication using tokens from a token repository, incorporating validation logic for token integrity. This enhances security by restricting access to authorized users only. - Connection Management Enhancements: The creation of the
UserConnection
struct introduces a structured approach to managing WebSocket connections, including handling ping requests for connection persistence and integrating with mutexes for concurrent operations. - Message Handling Improvements: New
WSCommand
structs were introduced to standardize incoming messages. ThehandleMessage
function now parses and routes these commands to appropriate handlers, supporting new command types likeSubscribe
,NewMessage
, andMessageDelivered
. Helper functions (NewWS...
) streamline message creation with built-in validation. - Rate Limiting Implementation: A rate limiting middleware (
RateLimitMiddleware
) has been added to prevent abuse by restricting requests from each user within a specified time window, utilizing Redis for tracking request counts. - Redis Integration: The server now leverages Redis for storing channel subscriptions using the
UserChannelRepository
.
Additionally, there were updates related to the build process:
- Docker Compose Update: The Docker Compose command has been updated to reflect the project name change from “remotedaemon” to “levnobot”.
- Build Script Modifications: The Go build script now utilizes
-tags netgo
,-ldflags '-s -w -extldflags "-static"'
, and-buildvcs=false
flags, likely for optimizing executable size and building statically. Thecdn
program is built with slightly different flags to omit compression. - Product Repository Changes: The product repository schema has been refined to include internal primary keys (BIGINT UNSIGNED) and a unique constraint on the combination of merchant product ID and data source ID, ensuring data integrity. The
SaveProduct
,SearchProduct
andDeleteProduct
functions have also been updated to utilize these changes.
Finally, there was a substantial refactoring of Google Product Feed XML parsing:
- Enhanced Parsing Capabilities: The code now supports more fields in the feed (e.g.,
program_name
,catalog_name
), utilizes two-pass parsing for detailed error analysis, and introduces helper functions (getStructTags
) to automate tag discovery. - Data Structure Updates: The core structure for parsing the feed (
Feed
andEntry
) has been expanded to include more data fields.
In essence, these changes represent a significant architectural shift toward a more robust, secure, and scalable WebSocket server with improved message handling capabilities, alongside refinements in database schema management and build processes, as well as enhancements to Google Product Feed parsing.