Skip to content

Conversation

@metaphorics
Copy link
Contributor

This pull request introduces several improvements and optimizations across the codebase. The most significant changes include major performance optimizations for buffer pooling, a refactored WebAssembly (WASM) production build process, a migration to reflection-free protobufs for TinyGo compatibility, and enhanced developer tooling for code quality and consistency.

Performance and Memory Optimizations:

  • Updated buffer pools in both cmd/portal-tunnel/main.go and cmd/relay-server/manager/bps_manager.go to use pointers to byte slices (*[]byte) instead of slices directly, reducing interface boxing allocations and improving performance under high concurrency. [1] [2] [3] [4]

WebAssembly (WASM) Build Improvements:

  • Enhanced the build-wasm target in the Makefile to produce production-ready WASM builds by stripping debug symbols, enabling aggressive inlining, setting reproducible build IDs, and adding a prod build tag to disable debugging and HTML injection. Also improved WASM optimization by running a dual-pass with wasm-opt for both speed and size.
  • Added Go build constraints (//go:build !prod) to cmd/webclient/inject.go and cmd/webclient/main_js.go to exclude debugging and injection code from production builds. [1] [2]

Protobuf and Protocol Changes:

  • Migrated portal/core/proto/rdsec/rdsec.pb.go to a reflection-free, TinyGo-compatible protobuf implementation by removing dependencies on protoimpl and protoreflect, simplifying message types, and updating serialization/deserialization code in portal/core/cryptoops/handshaker.go to use MarshalVT/UnmarshalVT methods. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

Developer Tooling and Code Quality:

  • Added a .pre-commit-config.yaml with hooks for whitespace cleanup, line ending normalization, Go formatting, and configuration verification, as well as a new justfile with common formatting, linting, testing, and vetting commands to streamline development workflows. [1] [2]

Dependency Updates:

  • Updated several module dependencies in go.mod, including golang.org/x/crypto, golang.org/x/net, google.golang.org/protobuf, gopkg.eu.org/broccoli, and indirect dependencies, to newer versions for improved security and compatibility.

- Add //go:build !prod tags to main_js.go and inject.go
- Create main_js_prod.go with //go:build prod tag
- Remove zerolog dependency from production build
- Make InjectHTML a no-op in production (handled by service worker)

This reduces WASM binary size and runtime overhead by excluding
debug logging and HTML parsing from production builds.
- Add -gcflags="-l=4 -trimpath" for aggressive inlining
- Add -ldflags="-s -w -buildid=" to strip symbols and build ID
- Add -tags=prod to enable production-only code path
- Implement dual-pass wasm-opt: -O4 for speed, then -Oz for size
- Enable bulk-memory, sign-ext, and reference-types features

This prioritizes runtime performance over binary size.
The WebSocket dialer returns a response that was never closed,
causing a resource leak. Now closes the response body on error.
Replace dynamic slice growth with pre-allocation using make()
with known capacity. This reduces allocations during relay
iteration.
Change sync.Pool to store *[]byte instead of []byte to prevent
interface boxing allocation on each Get/Put operation.

Applied to:
- cmd/portal-tunnel/main.go
- cmd/relay-server/manager/bps_manager.go
- portal/utils/ratelimit/bucket.go
…y' instead of 'proto.Message'

- Changed the return type of CloneMessageVT methods in various structs from proto.Message to any.
- Updated the parameter type of EqualMessageVT methods to accept 'any' instead of 'proto.Message'.
- This change enhances type flexibility and aligns with recent Go language updates.
- Add rdsec_test.go covering all 4 message types (Identity, ClientInitPayload, SignedPayload, ServerInitPayload)
  - Round-trip MarshalVT/UnmarshalVT tests
  - CloneVT, EqualVT, SizeVT tests
  - Getter methods, Reset methods, and enum String/Enum tests
  - Coverage: 52.5%

- Add rdverb_test.go covering all 11 message types (Packet, RelayInfo, Lease, etc.)
  - All packet type and response code enum tests
  - Getter methods and Reset methods
  - Complex nested message tests
  - Coverage: 36.6%

- Add wsstream_test.go with comprehensive WebSocket stream wrapper tests
  - Mock implementation for isolated testing
  - Concurrent read/write safety tests
  - Multi-message streaming tests
  - Coverage: 93.1%

- Refactor wsstream to use webSocketConn interface for testability
- Update utils/ws.go to use wsstream.New() constructor
- Updated test cases in various files to use range-based loops for better readability and performance.
- Modified the `justfile` to include `tidy` in the `all` target.
- Enhanced `pre-commit-config.yaml` to ensure proper linting and formatting for Go files.
- Added comprehensive tests for rate limiting and WebSocket functionalities in `utils` and `wsstream`.
- Improved error handling and edge case coverage in existing tests.
    Fix flakiness and incorrect assumptions in bucket tests:
    - Trigger lazy state update explicitly in slack refill test
    - Adjust burst/rate ratios to reliably trigger throttling in concurrent tests
    - Increase rate for buffer pool test to avoid timeouts
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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.

1 participant