-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Multiple kafka brokers #1658
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
Merged
aryanmehrotra
merged 26 commits into
gofr-dev:development
from
coolwednesday:multiple-kafka-brokers
Apr 30, 2025
Merged
Multiple kafka brokers #1658
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
fef90eb
adding support for multiple brokers
coolwednesday a2a5740
adding support for multiple-kafka-brokers
coolwednesday d888fa6
managing open connections to kafka multiple brokers
coolwednesday f165f3d
refactoring & fixing tests
coolwednesday fbb93c4
fixing linters
coolwednesday 013765d
fixing linters
coolwednesday 4e92868
Merge branch 'development' into multiple-kafka-brokers
coolwednesday e1cece7
Merge branch 'development' into multiple-kafka-brokers
coolwednesday f7e4ce9
fixing code climate issues
coolwednesday e0e8290
Merge branch 'development' into multiple-kafka-brokers
coolwednesday 5bdea50
adding documentation and fixing linters
coolwednesday 1ee999c
fixing code climate issues
coolwednesday 2a7837d
Merge branch 'development' into multiple-kafka-brokers
coolwednesday 75e4ba1
Merge branch 'development' into multiple-kafka-brokers
coolwednesday 313c087
fiixng linters
coolwednesday 192e450
Merge branch 'development' into multiple-kafka-brokers
Umang01-hash 1c068a9
Merge branch 'development' of github.com:gofr-dev/gofr into multiple-…
Umang01-hash 5aa3f68
fix tests
Umang01-hash 98fb1fd
resolve review comments
Umang01-hash b718710
Merge branch 'development' into multiple-kafka-brokers
coolwednesday 6a0214b
refactoring kafka initialization
coolwednesday 6defac1
Merge branch 'development' into multiple-kafka-brokers
Umang01-hash db11ce8
Merge branch 'development' into multiple-kafka-brokers
coolwednesday c610b74
applying review suggestions:
coolwednesday ed10d7f
minor fix
coolwednesday c7d3ba9
fixing variable missrepresentation
coolwednesday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package kafka | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"strconv" | ||
"sync" | ||
|
||
"github.com/segmentio/kafka-go" | ||
) | ||
|
||
var errFailedToConnectBrokers = errors.New("failed to connect to any kafka brokers") | ||
|
||
//nolint:unused // We need this wrap around for testing purposes. | ||
type Conn struct { | ||
conns []*kafka.Conn | ||
} | ||
|
||
// initialize creates and configures all Kafka client components. | ||
func (k *kafkaClient) initialize(ctx context.Context) error { | ||
dialer, err := setupDialer(&k.config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
conns, err := connectToBrokers(ctx, k.config.Broker, dialer, k.logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
multi := &multiConn{ | ||
conns: conns, | ||
dialer: dialer, | ||
} | ||
|
||
writer := createKafkaWriter(&k.config, dialer, k.logger) | ||
reader := make(map[string]Reader) | ||
|
||
k.logger.Logf("connected to %d Kafka brokers", len(conns)) | ||
|
||
k.dialer = dialer | ||
k.conn = multi | ||
k.writer = writer | ||
k.reader = reader | ||
|
||
return nil | ||
} | ||
|
||
func (k *kafkaClient) getNewReader(topic string) Reader { | ||
reader := kafka.NewReader(kafka.ReaderConfig{ | ||
GroupID: k.config.ConsumerGroupID, | ||
Brokers: k.config.Broker, | ||
Topic: topic, | ||
MinBytes: 10e3, | ||
MaxBytes: 10e6, | ||
Dialer: k.dialer, | ||
StartOffset: int64(k.config.OffSet), | ||
}) | ||
|
||
return reader | ||
} | ||
|
||
func (k *kafkaClient) DeleteTopic(_ context.Context, name string) error { | ||
return k.conn.DeleteTopics(name) | ||
} | ||
|
||
func (k *kafkaClient) Controller() (broker kafka.Broker, err error) { | ||
return k.conn.Controller() | ||
} | ||
|
||
func (k *kafkaClient) CreateTopic(_ context.Context, name string) error { | ||
topics := kafka.TopicConfig{Topic: name, NumPartitions: 1, ReplicationFactor: 1} | ||
|
||
return k.conn.CreateTopics(topics) | ||
} | ||
|
||
type multiConn struct { | ||
conns []Connection | ||
dialer *kafka.Dialer | ||
mu sync.RWMutex | ||
} | ||
|
||
func (m *multiConn) Controller() (kafka.Broker, error) { | ||
if len(m.conns) == 0 { | ||
return kafka.Broker{}, errNoActiveConnections | ||
} | ||
|
||
// Try all connections until we find one that works | ||
for _, conn := range m.conns { | ||
if conn == nil { | ||
continue | ||
} | ||
|
||
controller, err := conn.Controller() | ||
if err == nil { | ||
return controller, nil | ||
} | ||
} | ||
|
||
return kafka.Broker{}, errNoActiveConnections | ||
} | ||
|
||
func (m *multiConn) CreateTopics(topics ...kafka.TopicConfig) error { | ||
controller, err := m.Controller() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
controllerAddr := net.JoinHostPort(controller.Host, strconv.Itoa(controller.Port)) | ||
|
||
controllerResolvedAddr, err := net.ResolveTCPAddr("tcp", controllerAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
|
||
for _, conn := range m.conns { | ||
if conn == nil { | ||
continue | ||
} | ||
|
||
if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { | ||
if tcpAddr.IP.Equal(controllerResolvedAddr.IP) && tcpAddr.Port == controllerResolvedAddr.Port { | ||
return conn.CreateTopics(topics...) | ||
} | ||
} | ||
} | ||
|
||
// If not found, create a new connection | ||
conn, err := m.dialer.DialContext(context.Background(), "tcp", controllerAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m.conns = append(m.conns, conn) | ||
|
||
return conn.CreateTopics(topics...) | ||
} | ||
|
||
func (m *multiConn) DeleteTopics(topics ...string) error { | ||
controller, err := m.Controller() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
controllerAddr := net.JoinHostPort(controller.Host, strconv.Itoa(controller.Port)) | ||
|
||
controllerResolvedAddr, err := net.ResolveTCPAddr("tcp", controllerAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, conn := range m.conns { | ||
if conn == nil { | ||
continue | ||
} | ||
|
||
if tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr); ok { | ||
// Match IP (after resolution) and Port | ||
if tcpAddr.IP.Equal(controllerResolvedAddr.IP) && tcpAddr.Port == controllerResolvedAddr.Port { | ||
return conn.DeleteTopics(topics...) | ||
} | ||
} | ||
} | ||
|
||
// If not found, create a new connection | ||
conn, err := m.dialer.DialContext(context.Background(), "tcp", controllerAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m.conns = append(m.conns, conn) | ||
|
||
return conn.DeleteTopics(topics...) | ||
} | ||
|
||
func (m *multiConn) Close() error { | ||
var err error | ||
|
||
for _, conn := range m.conns { | ||
if conn != nil { | ||
err = errors.Join(err, conn.Close()) | ||
} | ||
} | ||
|
||
return err | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.