forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor SessionPool out of ConnectionImpl. (googleapis/google-cloud-cp…
…p-spanner#916) * Factor SessionPool out of ConnectionImpl. Make `SessionPool` its own class to avoid clutter in `ConnectionImpl` and to make it easier to test. Since `SessionPool` is a member of `ConnectionImpl`, which guarantees the former cannot outlive the latter; only `ConnectionImpl` is required to be reference counted. This simplifies lifetime management, avoiding some subtle issues and race conditions, at the cost of requiring some ostensibly pool-related private methods in `ConnectionImpl` for the benefit of `SessionHolder` There are no functional changes compared to the current implementation. Part of googleapis/google-cloud-cpp-spanner#307 * Apparently I failed to learn my lesson about make_unique. * address review comments * fix build and add some comments to SessionPool * fix the build for real this time and remove an extra {} * Argh - I knew those {} were there for a reason (else cxx17 complains)
- Loading branch information
Showing
8 changed files
with
343 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/spanner/internal/session_pool.h" | ||
#include "google/cloud/spanner/internal/connection_impl.h" | ||
#include "google/cloud/spanner/internal/session.h" | ||
#include <memory> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace spanner { | ||
inline namespace SPANNER_CLIENT_NS { | ||
namespace internal { | ||
|
||
StatusOr<std::unique_ptr<Session>> SessionPool::Allocate() { | ||
{ | ||
std::lock_guard<std::mutex> lk(mu_); | ||
if (!sessions_.empty()) { | ||
auto session = std::move(sessions_.back()); | ||
sessions_.pop_back(); | ||
return {std::move(session)}; | ||
} | ||
} | ||
|
||
auto sessions = manager_->CreateSessions(/*num_sessions=*/1); | ||
if (!sessions.ok()) { | ||
return std::move(sessions).status(); | ||
} | ||
// TODO(#307) for now, assume CreateSessions() returns exactly one Session on | ||
// success (as we requested). Rewrite this to accommodate multiple sessions. | ||
return {std::move((*sessions)[0])}; | ||
} | ||
|
||
void SessionPool::Release(std::unique_ptr<Session> session) { | ||
std::lock_guard<std::mutex> lk(mu_); | ||
sessions_.push_back(std::move(session)); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace SPANNER_CLIENT_NS | ||
} // namespace spanner | ||
} // namespace cloud | ||
} // namespace google |
Oops, something went wrong.