Skip to content

Conversation

@mcollina
Copy link
Member

@mcollina mcollina commented May 26, 2025

Summary

  • Implement HTTP request/response routing through Kafka topics
  • Add correlation ID handling for request matching with automatic UUID generation
  • Support configurable timeouts (default 30 seconds) with proper error handling
  • Enable custom HTTP status codes in responses via x-status-code header
  • Preserve all HTTP headers in Kafka messages for full context passing

Features Added

Core Request/Response Flow

  • HTTP Endpoints: Configure custom paths that route through Kafka topics
  • Correlation Tracking: Automatic correlation ID generation and matching
  • Timeout Handling: Configurable timeouts prevent hanging requests
  • Status Code Support: Response messages can include HTTP status codes

Configuration

New requestResponse array in kafka config:

{
  "requestResponse": [
    {
      "path": "/api/process",
      "requestTopic": "request-topic", 
      "responseTopic": "response-topic",
      "timeout": 5000
    }
  ]
}

Enhanced Header Processing

  • All HTTP headers are now preserved in Kafka messages
  • Correlation ID automatically added to request messages
  • Response headers properly forwarded to HTTP response

Usage Example

HTTP Request:

curl -X POST http://localhost:3042/api/process \
  -H "Content-Type: application/json" \
  -d '{"data": "request payload"}'

Flow:

  1. Request published to request-topic with correlation ID
  2. External service processes request and publishes response to response-topic
  3. Response automatically routed back to original HTTP caller
  4. HTTP request completes with response data

Test Coverage

  • ✅ Successful request/response round-trip
  • ✅ Timeout handling when no response received
  • ✅ Correlation ID generation and matching
  • ✅ HTTP status code propagation
  • ✅ Header preservation
  • ✅ Schema validation

Backward Compatibility

Fully backward compatible - existing functionality unchanged.

🤖 Generated with Claude Code

Fixes #2

mcollina and others added 2 commits May 26, 2025 13:24
- Add HTTP request/response routing through Kafka topics
- Implement correlation ID handling for request matching
- Add configurable timeout support (default 30s)
- Support custom HTTP status codes in responses
- Preserve all HTTP headers in Kafka messages
- Add comprehensive tests for request/response flows
- Update schema to support requestResponse configuration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add detailed How It Works section explaining the flow
- Include complete configuration examples with all options
- Provide step-by-step usage examples with curl commands
- Document response headers and error handling scenarios
- Add use cases for microservice communication patterns
- Update main feature list to highlight new capability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@mcollina mcollina requested a review from ShogunPanda May 26, 2025 11:31
mcollina and others added 3 commits May 26, 2025 13:39
- Add new header constants for path params and query string data
- Automatically extract path parameters from URL routes
- Pass path parameters as JSON in x-plt-kafka-hooks-path-params header
- Pass query string parameters as JSON in x-plt-kafka-hooks-query-string header
- Support Fastify path parameter syntax (e.g., /users/:userId/orders/:orderId)
- Add comprehensive tests for path params, query strings, and combined usage
- Update documentation with examples and header reference tables
- Maintain backward compatibility with existing functionality

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add complete end-to-end integration tests demonstrating real-world usage
- Test path parameters and query string extraction and forwarding
- Test error handling with custom HTTP status codes
- Demonstrate microservice communication patterns via Kafka
- Show how kafka-hooks enables decoupled, reliable service architecture
- Include realistic scenarios with user profiles, orders, and error cases
- Verify correlation ID handling and message flow integrity
- Test both successful responses and error conditions

The integration tests demonstrate:
- HTTP → Kafka → Processing Service → Kafka → HTTP flow
- Path parameters: /api/users/:userId/orders/:orderId
- Query strings: ?include=items&format=detailed&currency=USD
- JSON request/response payloads with complex nested data
- Error responses with custom status codes (404, 500, etc.)
- Complete microservice architecture patterns

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
README.md Outdated
"code": "HTTP_ERROR_REQUEST_TIMEOUT",
"error": "Request Timeout",
"message": "Request timeout",
"statusCode": 408
Copy link
Contributor

Choose a reason for hiding this comment

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

This is wrong. 4xx are client errors, but here we're returning a server error. I would use 500, 503 or 504.

Use 504 Gateway Timeout instead of 408 Request Timeout for
request/response pattern timeouts, as this is more appropriate
for scenarios where the gateway times out waiting for an upstream service.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@mcollina
Copy link
Member Author

@ShogunPanda PTAL

Copy link
Contributor

@ShogunPanda ShogunPanda left a comment

Choose a reason for hiding this comment

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

LGTM!

@mcollina mcollina merged commit 565bfd0 into main May 27, 2025
4 checks passed
@mcollina mcollina deleted the feat/request-response-pattern branch May 27, 2025 12: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.

Add support for request/response pattern

3 participants