diff --git a/src/client/persqueue_public/impl/write_session_impl.cpp b/src/client/persqueue_public/impl/write_session_impl.cpp index 1e2141a2450..9b02bff258e 100644 --- a/src/client/persqueue_public/impl/write_session_impl.cpp +++ b/src/client/persqueue_public/impl/write_session_impl.cpp @@ -29,6 +29,9 @@ TWriteSessionImpl::TWriteSessionImpl( , Client(std::move(client)) , Connections(std::move(connections)) , DbDriverState(std::move(dbDriverState)) + , PrevToken(DbDriverState->GetCredentialsProvider() + ? DbDriverState->GetCredentialsProvider()->GetAuthInfoAsync() + : NThreading::MakeFuture(std::string())) , InitSeqNoPromise(NThreading::NewPromise()) , WakeupInterval( Settings.BatchFlushInterval_ != TDuration::Zero() ? @@ -1301,12 +1304,12 @@ void TWriteSessionImpl::UpdateTokenImpl(const NThreading::TFuture& CloseImpl(EStatus::CLIENT_UNAUTHENTICATED, CurrentExceptionMessage()); return; } - if (token == PrevToken) { + if (PrevToken.HasValue() && token == PrevToken.GetValue()) { return; } UpdateTokenInProgress = true; - PrevToken = token; + PrevToken = future; LOG_LAZY(DbDriverState->Log, TLOG_DEBUG, LogPrefix() << "Write session: updating token"); diff --git a/src/client/persqueue_public/impl/write_session_impl.h b/src/client/persqueue_public/impl/write_session_impl.h index 061ad02656c..1af3f7b130e 100644 --- a/src/client/persqueue_public/impl/write_session_impl.h +++ b/src/client/persqueue_public/impl/write_session_impl.h @@ -389,7 +389,7 @@ class TWriteSessionImpl : public TContinuationTokenIssuer, std::string PreferredClusterByCDS; std::shared_ptr ConnectionFactory; TDbDriverStatePtr DbDriverState; - std::string PrevToken; + NThreading::TFuture PrevToken; std::atomic_bool UpdateTokenInProgress = false; TInstant LastTokenUpdate = TInstant::Zero(); std::shared_ptr EventsQueue; diff --git a/src/client/topic/impl/write_session_impl.cpp b/src/client/topic/impl/write_session_impl.cpp index 94142c07728..69a7434a27a 100644 --- a/src/client/topic/impl/write_session_impl.cpp +++ b/src/client/topic/impl/write_session_impl.cpp @@ -185,6 +185,9 @@ TWriteSessionImpl::TWriteSessionImpl( , Client(std::move(client)) , Connections(std::move(connections)) , DbDriverState(std::move(dbDriverState)) + , PrevToken(DbDriverState->GetCredentialsProvider() + ? DbDriverState->GetCredentialsProvider()->GetAuthInfoAsync() + : NThreading::MakeFuture(std::string())) , MaxBlockMessageCount(Settings.BatchFlushMessageCount_) , InitSeqNoPromise(NThreading::NewPromise()) , WakeupInterval( @@ -1911,14 +1914,14 @@ void TWriteSessionImpl::UpdateTokenImpl(const NThreading::TFuture& CloseImpl(EStatus::CLIENT_UNAUTHENTICATED, CurrentExceptionMessage()); return; } - if (token == PrevToken) { + if (PrevToken.HasValue() && token == PrevToken.GetValue()) { return; } LOG_LAZY(DbDriverState->Log, TLOG_DEBUG, LogPrefixImpl() << "Write session: updating token"); UpdateTokenInProgress = true; - PrevToken = token; + PrevToken = future; TClientMessage clientMessage; clientMessage.mutable_update_token_request()->set_token(TStringType{token}); diff --git a/src/client/topic/impl/write_session_impl.h b/src/client/topic/impl/write_session_impl.h index c4185e7b94f..0c4e35feca3 100644 --- a/src/client/topic/impl/write_session_impl.h +++ b/src/client/topic/impl/write_session_impl.h @@ -502,7 +502,7 @@ class TWriteSessionImpl : public TContinuationTokenIssuer, std::shared_ptr ConnectionFactory; TDbDriverStatePtr DbDriverState; - std::string PrevToken; + NThreading::TFuture PrevToken; std::atomic_bool UpdateTokenInProgress = false; TInstant LastTokenUpdate = TInstant::Zero(); std::shared_ptr EventsQueue; diff --git a/tests/integration/topic/basic_usage_it.cpp b/tests/integration/topic/basic_usage_it.cpp index ceef48a74af..cffab74bf09 100644 --- a/tests/integration/topic/basic_usage_it.cpp +++ b/tests/integration/topic/basic_usage_it.cpp @@ -86,6 +86,26 @@ TEST_F(BasicUsage, TEST_NAME(ConnectToYDB)) { } } +TEST_F(BasicUsage, TEST_NAME(WriteWithInitialToken)) { + TDriver driver(MakeDriverConfig().SetAuthToken("unused-token")); + TTopicClient client(driver); + auto session = client.CreateSimpleBlockingWriteSession( + TWriteSessionSettings().Path(GetTopicPath()).MessageGroupId(TEST_MESSAGE_GROUP_ID)); + + ASSERT_TRUE(session->Write("message", {}, {}, TDuration::Seconds(10))); + ASSERT_TRUE(session->Close(TDuration::Seconds(10))); +} + +TEST_F(BasicUsage, TEST_NAME(PersQueueWriteWithInitialToken)) { + TDriver driver(MakeDriverConfig().SetAuthToken("unused-token")); + NPersQueue::TPersQueueClient client(driver); + auto session = client.CreateSimpleBlockingWriteSession( + NPersQueue::TWriteSessionSettings().Path(GetTopicPath()).MessageGroupId(TEST_MESSAGE_GROUP_ID)); + + ASSERT_TRUE(session->Write("message", {}, {}, TDuration::Seconds(10))); + ASSERT_TRUE(session->Close(TDuration::Seconds(10))); +} + TEST_F(BasicUsage, TEST_NAME(WriteRead)) { auto driver = MakeDriver();