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

Refactor TopicClient event loop #359

Open
Gazizonoki opened this issue Dec 17, 2024 · 0 comments
Open

Refactor TopicClient event loop #359

Gazizonoki opened this issue Dec 17, 2024 · 0 comments

Comments

@Gazizonoki
Copy link
Collaborator

Gazizonoki commented Dec 17, 2024

Current example for topic reader:

// Create topic client.

NYdb::NTopic::TTopicClient topicClient(driver);

// Create read session.
NYdb::NTopic::TReadSessionSettings settings;
settings
    .ConsumerName(opts.ConsumerName)
    .AppendTopics(opts.TopicPath);

ReadSession = topicClient.CreateReadSession(settings);

std::cerr << "Session was created" << std::endl;

// [BEGIN read session process events]
// Event loop
while (true) {
    auto future = ReadSession->WaitEvent();
    // Wait for next event or ten seconds
    future.Wait(TDuration::Seconds(10));
    // Get event
    std::optional<NYdb::NTopic::TReadSessionEvent::TEvent> event = ReadSession->GetEvent(true/*block - will block if no event received yet*/);
    std::cerr << "Got new read session event: " << DebugString(*event) << std::endl;

    if (auto* dataEvent = std::get_if<NYdb::NTopic::TReadSessionEvent::TDataReceivedEvent>(&*event)) {
        for (const auto& message : dataEvent->GetMessages()) {
            std::cerr << "Data message: \"" << message.GetData() << "\"" << std::endl;
        }

        if (opts.CommitAfterProcessing) {
            dataEvent->Commit();
        }
    } else if (auto* startPartitionSessionEvent = std::get_if<NYdb::NTopic::TReadSessionEvent::TStartPartitionSessionEvent>(&*event)) {
        startPartitionSessionEvent->Confirm();
    } else if (auto* stopPartitionSessionEvent = std::get_if<NYdb::NTopic::TReadSessionEvent::TStopPartitionSessionEvent>(&*event)) {
        stopPartitionSessionEvent->Confirm();
    } else if (auto* endPartitionSessionEvent = std::get_if<NYdb::NTopic::TReadSessionEvent::TEndPartitionSessionEvent>(&*event)) {
        endPartitionSessionEvent->Confirm();
    } else if (auto* closeSessionEvent = std::get_if<NYdb::NTopic::TSessionClosedEvent>(&*event)) {
        break;
    }
}

It's unfriendly API, we should design more simple and friendly API like ISimpleWriteSession

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

No branches or pull requests

1 participant