Skip to content
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

A new method for Batch called ReadIntoMessage #1267

Open
rouzier opened this issue Feb 21, 2024 · 0 comments
Open

A new method for Batch called ReadIntoMessage #1267

rouzier opened this issue Feb 21, 2024 · 0 comments

Comments

@rouzier
Copy link
Contributor

rouzier commented Feb 21, 2024

Describe the solution you would like

Could we have a method that combines Batch.Read() and Batch.ReadMessage()
Where if there is enough space in msg.Value and msg.Key it will reuse those buffers otherwise create a new buffer.

Example usage.

        // to consume messages
        topic := "my-topic"
        partition := 0

        conn, err := kafka.DialLeader(context.Background(), "tcp", "localhost:9092", topic, partition)
        if err != nil {
                log.Fatal("failed to dial leader:", err)
        }

        conn.SetReadDeadline(time.Now().Add(10 * time.Second))
        batch := conn.ReadBatch(10e3, 1e6) // fetch 10KB min, 1MB max
        msg := kafka.Message{
                Value: make([]byte, 10e3), // 10KB max per message
                Key:   make([]byte, 256),  // 256 bytes max per message
        }
        for {
                err := batch.ReadIntoMessage(&msg)
                if err != nil {
                        break
                }
                fmt.Println(string(msg.Value))
        }

        if err := batch.Close(); err != nil {
                log.Fatal("failed to close batch:", err)
        }

        if err := conn.Close(); err != nil {
                log.Fatal("failed to close connection:", err)
        }

I could provide a PR if you guys are interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant