-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: added topic exists check (#59) * chore: * chore: dummy * feat: implement topic verify process * chore: lint fix * feat: bump kafka-cronsumer verify topic commit * feat: add verifyTopicOnStartup flag --------- Co-authored-by: keremcankabadayi <[email protected]>
- Loading branch information
1 parent
b4d988b
commit ca225fb
Showing
10 changed files
with
306 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,70 @@ | ||
package kafka | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/segmentio/kafka-go" | ||
) | ||
|
||
type kafkaClient interface { | ||
Metadata(ctx context.Context, req *kafka.MetadataRequest) (*kafka.MetadataResponse, error) | ||
GetClient() *kafka.Client | ||
} | ||
|
||
type client struct { | ||
*kafka.Client | ||
} | ||
|
||
func newKafkaClient(cfg *ConsumerConfig) (kafkaClient, error) { | ||
kc := client{ | ||
Client: &kafka.Client{ | ||
Addr: kafka.TCP(cfg.Reader.Brokers...), | ||
}, | ||
} | ||
|
||
transport := &Transport{ | ||
Transport: &kafka.Transport{ | ||
MetadataTopics: cfg.getTopics(), | ||
}, | ||
} | ||
if err := fillLayer(transport, cfg.SASL, cfg.TLS); err != nil { | ||
return nil, fmt.Errorf("error when initializing kafka client for verify topic purpose %w", err) | ||
} | ||
|
||
kc.Transport = transport | ||
return &kc, nil | ||
} | ||
|
||
func (k *client) GetClient() *kafka.Client { | ||
return k.Client | ||
} | ||
|
||
func verifyTopics(client kafkaClient, cfg *ConsumerConfig) (bool, error) { | ||
topics := cfg.getTopics() | ||
|
||
metadata, err := client.Metadata(context.Background(), &kafka.MetadataRequest{ | ||
Topics: topics, | ||
}) | ||
if err != nil { | ||
return false, fmt.Errorf("error when during verifyTopics metadata request %w", err) | ||
} | ||
return checkTopicsWithinMetadata(metadata, topics) | ||
} | ||
|
||
func checkTopicsWithinMetadata(metadata *kafka.MetadataResponse, topics []string) (bool, error) { | ||
metadataTopics := make(map[string]struct{}, len(metadata.Topics)) | ||
for _, topic := range metadata.Topics { | ||
if topic.Error != nil { | ||
continue | ||
} | ||
metadataTopics[topic.Name] = struct{}{} | ||
} | ||
|
||
for _, topic := range topics { | ||
if _, exist := metadataTopics[topic]; !exist { | ||
return false, nil | ||
} | ||
} | ||
return true, nil | ||
} |
Oops, something went wrong.