-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor sendAndReceive
#64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Warning Rate limit exceeded@ryan-gang has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 34 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
""" WalkthroughThe changes introduce a new Kafka client implementation, update request encoding logic, and refactor related APIs. The new client manages connections, sending, and receiving Kafka protocol messages, with enhanced error handling and logging. Request encoding is simplified and made self-contained. Utility functions for hexdump formatting and API key naming are added, and interface definitions are updated to support the new client. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Stage as stage_dtp2.go
participant Client as kafka_client.Client
participant Broker as Kafka Broker
User->>Stage: Initiate request
Stage->>Client: NewClient("localhost:9092")
Stage->>Client: ConnectWithRetries()
Client->>Broker: Establish TCP connection
Stage->>Client: SendAndReceive(request, logger)
Client->>Broker: Send encoded request bytes
Broker-->>Client: Return response bytes
Client-->>Stage: Return Response struct
Stage->>Client: Close()
Client-->>Stage: Connection closed
Estimated code review effort3 (~45 minutes) Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
ad06f4d
to
da10b54
Compare
@@ -3,19 +3,12 @@ package kafkaapi | |||
import ( | |||
"github.com/codecrafters-io/kafka-tester/protocol" | |||
"github.com/codecrafters-io/kafka-tester/protocol/decoder" | |||
"github.com/codecrafters-io/kafka-tester/protocol/encoder" | |||
"github.com/codecrafters-io/kafka-tester/protocol/errors" | |||
"github.com/codecrafters-io/tester-utils/logger" | |||
) | |||
|
|||
func EncodeDescribeTopicPartitionsRequest(request *DescribeTopicPartitionsRequest) []byte { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be removed.
da10b54
to
0e2f0c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 6
🧹 Nitpick comments (3)
protocol/utils/utils.go (1)
50-65
: Consider more graceful error handling for unknown API keys.While the function correctly maps known API keys to names, using
panic
for unknown keys might be too harsh. Consider returning an error or a default string like "Unknown API Key (X)" to make the code more robust.-func APIKeyToName(apiKey int16) string { +func APIKeyToName(apiKey int16) string { switch apiKey { case 0: return "Produce" case 1: return "Fetch" case 18: return "ApiVersions" case 19: return "CreateTopics" case 75: return "DescribeTopicPartitions" default: - panic(fmt.Sprintf("CodeCrafters Internal Error: Unknown API key: %v", apiKey)) + return fmt.Sprintf("Unknown API Key (%d)", apiKey) } }However, if this is specifically for a testing framework where unknown API keys indicate programming errors, the current panic approach may be acceptable.
protocol/builder/interface.go (1)
5-8
: Consider Go naming convention for interfaces.The interface design is solid and provides a clean abstraction for request handling. However, Go convention typically avoids the 'I' suffix for interfaces. Consider renaming to just
Request
orRequestEncoder
.-type RequestI interface { +type Request interface { Encode() []byte GetHeader() kafkaapi.RequestHeader }protocol/kafka_client/client.go (1)
197-221
: Improve error handling structure.The empty else block is unnecessary. Consider restructuring the error handling for better readability.
if err != nil && err != io.EOF { if netErr, ok := err.(net.Error); ok && netErr.Timeout() { - } else { + // Timeout is expected, return what we have + return buf.Bytes(), nil + } - return nil, fmt.Errorf("error reading from connection: %v", err) - } + return nil, fmt.Errorf("error reading from connection: %v", err) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
internal/stage_dtp2.go
(3 hunks)internal/utils.go
(1 hunks)protocol/api/describe_topic_partitions.go
(1 hunks)protocol/api/describe_topic_partitions_request.go
(1 hunks)protocol/api/header.go
(1 hunks)protocol/builder/interface.go
(1 hunks)protocol/kafka_client/client.go
(1 hunks)protocol/utils/utils.go
(1 hunks)
🧬 Code Graph Analysis (5)
protocol/builder/interface.go (1)
protocol/api/header.go (1)
RequestHeader
(12-21)
protocol/utils/utils.go (1)
internal/utils.go (1)
GetFormattedHexdump
(25-65)
protocol/api/describe_topic_partitions_request.go (2)
protocol/api/header.go (1)
RequestHeader
(12-21)protocol/encoder/encoder.go (1)
Encoder
(13-16)
internal/stage_dtp2.go (1)
protocol/kafka_client/client.go (2)
NewClient
(39-41)Client
(31-35)
protocol/kafka_client/client.go (3)
internal/kafka_executable/kafka_executable.go (1)
KafkaExecutable
(14-18)protocol/builder/interface.go (1)
RequestI
(5-8)protocol/utils/utils.go (1)
APIKeyToName
(50-65)
🧰 Additional context used
🧬 Code Graph Analysis (5)
protocol/builder/interface.go (1)
protocol/api/header.go (1)
RequestHeader
(12-21)
protocol/utils/utils.go (1)
internal/utils.go (1)
GetFormattedHexdump
(25-65)
protocol/api/describe_topic_partitions_request.go (2)
protocol/api/header.go (1)
RequestHeader
(12-21)protocol/encoder/encoder.go (1)
Encoder
(13-16)
internal/stage_dtp2.go (1)
protocol/kafka_client/client.go (2)
NewClient
(39-41)Client
(31-35)
protocol/kafka_client/client.go (3)
internal/kafka_executable/kafka_executable.go (1)
KafkaExecutable
(14-18)protocol/builder/interface.go (1)
RequestI
(5-8)protocol/utils/utils.go (1)
APIKeyToName
(50-65)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Cursor BugBot
🔇 Additional comments (7)
protocol/utils/utils.go (1)
8-48
: LGTM! Well-implemented hex dump formatter.The function correctly formats byte data into a readable hex dump with proper alignment, ASCII representation, and padding. The use of
strings.Builder
for efficient string concatenation is a good practice.internal/utils.go (1)
23-24
: Good practice to document the deprecation and migration path.The TODO comment clearly indicates the planned removal and replacement, which will help with the refactoring process.
protocol/api/describe_topic_partitions.go (1)
10-12
: Good simplification that aligns with the interface-driven refactoring.The change to delegate encoding to the request's own
Encode()
method makes the code cleaner and more self-contained, supporting the newRequestI
interface pattern.internal/stage_dtp2.go (3)
9-9
: Import statement correctly added for the new Kafka client.The import aligns with the PR's objective to replace the Broker component with the new Client implementation.
28-34
: Connection logic successfully migrated to the new Kafka client.The changes improve connection handling with built-in retry logic and proper resource cleanup. The integration with the Kafka executable for monitoring is a good enhancement.
48-48
: Request sending properly updated to use the new client interface.The change from manual encoding to structured request handling improves code maintainability and aligns with the new architecture.
protocol/kafka_client/client.go (1)
111-136
: Well-implemented request/response orchestration.The method provides excellent logging and clean separation of concerns between sending and receiving operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a bunch of things mixed in here. There's renaming broker -> client, there's changing the interface of SendAndReceive, and there's moving GetFormattedHexDump to utils, there's a bunch of interface stuff.
Split out, and try to avoid in-progress comments for things that can clearly be done in one PR, like migrating usages of GetFormattedHexDump
Summary by CodeRabbit
New Features
Refactor
Documentation