Unified connection management across all protocols#13
Open
zach-snell wants to merge 18 commits intomainfrom
Open
Unified connection management across all protocols#13zach-snell wants to merge 18 commits intomainfrom
zach-snell wants to merge 18 commits intomainfrom
Conversation
…essaging and base64 encoding for binary data
…e API documentation
Add full MQTT connection management across all API layers:
- Broker: ListClientInfos, GetClientInfo, DisconnectClient, GetConnectionStats
- Engine API: GET/DELETE /mqtt/connections/{id}, GET /mqtt/stats
- Control API adapter: bridges broker methods to engine API
- Engine client: HTTP client methods for MQTT endpoints
- Admin API: handlers with stats provider pattern, routes at /mqtt-connections/
- Auto-disconnect: cleanly disconnect all MQTT clients before stopping broker
on mock update/delete, giving clients a chance to reconnect
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add StreamTracker to the gRPC server that tracks active streaming RPCs
(server-stream, client-stream, bidirectional). Unary RPCs are counted
but not tracked as active connections since they are stateless.
Key changes:
- StreamTracker in pkg/grpc/stream_tracker.go with Register/Unregister,
message counting, Cancel/CancelAll, and aggregate stats
- Hooked into all three streaming handlers in server.go, replacing raw
metrics.ActiveConnections calls with tracker-based management
- Admin API endpoints: GET/DELETE /grpc/connections/{id}, GET /grpc/stats
- Engine Control API routes and ControlAPIAdapter methods
- Engine client methods for the admin server to call through
- Auto-disconnect on mock update: unregisterHandlerLocked cancels active
gRPC streams with codes.Unavailable before stopping the server, so
clients with retry policies reconnect automatically
- Full test coverage: stream tracker unit tests, engine API handler
tests, admin handler tests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When SSE mocks are updated, deleted, toggled, or cleared, active SSE connections now get disconnected automatically. This prevents clients from streaming stale data after configuration changes. Changes: - Add DisconnectSSEByMock() to Handler (handler_protocol.go) which delegates to SSEConnectionManager.CloseByMock() - Add case mock.TypeHTTP in unregisterHandlerLocked (mock_manager.go) that disconnects SSE connections when the HTTP mock has SSE config - Add 6 integration tests covering update, delete, toggle, clear, multi-connection, and cross-mock isolation scenarios The SSE close mechanism uses context cancellation: cancelling each connection's context causes the event loop to exit, the response writer to close, and the client to receive EOF. EventSource clients will auto-reconnect and pick up the new configuration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
SSE API responses now return full connection details (path, clientIp,
userAgent, connectedAt, eventsSent, bytesSent, status) instead of the
slimmed SSEStreamInfo (ID, MockID only). This aligns SSE with the
WebSocket API which already returns rich connection info.
Changes:
- SSE list, get-single, and mock-specific list endpoints now return
full SSEConnection structs from engineclient (admin/sse_handlers.go)
- Add mock-specific WebSocket connection endpoints for parity with SSE:
GET /mocks/{id}/websocket/connections and
DELETE /mocks/{id}/websocket/connections (admin/websocket_handlers.go)
- Register the new WebSocket routes (admin/routes.go)
- Add engine-level SSE handler tests covering list, get, close, and
stats handlers (engine/api/handlers_test.go)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `connections` subcommands (list, get, close) and `stats` commands for WebSocket, SSE, MQTT, and gRPC protocols. WebSocket also gets a `connections send` command with --binary flag support. New files: ws_connections.go, sse.go, mqtt_connections.go, grpc_connections.go Extended: client.go (16 new AdminClient interface methods + implementations), print.go (formatStringSlice helper) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…onnection management Critical fixes: - C3: MQTT ConnectedAt now populated via OnSessionEstablished/OnDisconnect hooks with clientConnectedAt tracking map on the Broker struct - C4: Canonical API types (WebSocketConnectionListResponse, SSEConnectionListResponse, MQTTConnectionListResponse, GRPCStreamListResponse) now use Stats field matching what admin handlers actually return, fixing CLI count:0 bug - C5: CHANGELOG expanded with SSE, MQTT, gRPC, CLI, and UI connection management entries - C6: API docs updated with correct WebSocket paths, full SSE/MQTT/gRPC connection management sections, mock-scoped WebSocket endpoints, and response examples High-priority fixes: - H1: gRPC stream handlers now return codes.Canceled for client-initiated cancellations and codes.Unavailable only for admin/mock-update cancellations, using per-stream adminCancelled tracking in StreamTracker - H2: Removed dead code CancelAllWithStatus() and UnavailableError() from stream_tracker.go Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Hi @zach-snell! I was able to test only the WebSocket functionality — it works correctly. The control API works as expected, and when mock data for a WebSocket is changed, the connection is terminated (as intended). A couple of points I’d like to highlight:
Great work — I’m really glad to see the project evolving so quickly! ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Unifies connection/session management across all connection-style protocols, based on the excellent work from @pr0head in #9 and #11.
codes.Unavailableon mock updatemockd [websocket|sse|mqtt|grpc] connections [list|get|close]+ stats for all protocolsWhat changed
codes.UnavailableBased on community contribution
Cherry-picked and extended @pr0head's work from PRs #9 and #11. Applied fixes identified in review (type validation, response field alignment, dead code removal, etc.) and extended the same patterns to SSE, MQTT, and gRPC.
Closes #8
Closes #10
Test plan
go build ./...passesgo vet ./...passesgo test ./...passesmockd websocket connectionsmockd websocket stats,mockd sse stats,mockd mqtt stats,mockd grpc stats@pr0head — would love your thoughts on this and if you could give it a test. We took your WebSocket work as the foundation and extended the same patterns to SSE, MQTT, and gRPC. Let us know if anything looks off or if you have suggestions.