Skip to content

Commit

Permalink
Add session pool example. (#107)
Browse files Browse the repository at this point in the history
* Add session pool example.

* Fix typo.

* Fix compile.

* Expose session pool symbols.
  • Loading branch information
Shylock-Hg authored Oct 20, 2022
1 parent 26979b8 commit dad0939
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ jobs:
rm /usr/lib/x86_64-linux-gnu/libstdc++.so.6
ln -s /opt/vesoft/toolset/clang/10.0.0/lib64/libstdc++.so /usr/lib/x86_64-linux-gnu/libstdc++.so.6
LD_LIBRARY_PATH=/usr/local/nebula/lib64:/usr/local/nebula/lib:$LD_LIBRARY_PATH ./build/session_example graphd:9669
LD_LIBRARY_PATH=/usr/local/nebula/lib64:/usr/local/nebula/lib:$LD_LIBRARY_PATH ./build/session_pool_example graphd:9669
popd
;;
esac
Expand Down
9 changes: 9 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ add_executable(session_example
SessionExample.cpp
)

add_executable(session_pool_example
SessionPoolExample.cpp
)

add_executable(storage_client_example
StorageClientExample.cpp
)
Expand All @@ -44,6 +48,11 @@ target_link_libraries(session_example
nebula_graph_client
)

target_link_libraries(session_pool_example
PRIVATE
nebula_graph_client
)

target_link_libraries(storage_client_example
PRIVATE
nebula_storage_client
Expand Down
49 changes: 49 additions & 0 deletions examples/SessionPoolExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2022 vesoft inc. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

#include <common/Init.h>
#include <nebula/client/SessionPool.h>

#include <atomic>
#include <chrono>
#include <thread>
#include "nebula/client/Config.h"

int main(int argc, char* argv[]) {
nebula::init(&argc, &argv);
{
// Create space
nebula::ConnectionPool pool;
auto config = nebula::Config{};
config.maxConnectionPoolSize_ = 1;
pool.init({"127.0.0.1:9669"}, config);
auto session = pool.getSession("root", "nebula");
assert(session.valid());

auto resp = session.execute(
"CREATE SPACE IF NOT EXISTS session_pool_test(vid_type = FIXED_STRING(16));");
assert(resp.errorCode == nebula::ErrorCode::SUCCEEDED);
}

nebula::SessionPoolConfig config;
config.username_ = "root";
config.password_ = "nebula";
config.addrs_ = {"127.0.0.1:9669"};
config.spaceName_ = "session_pool_test";
config.maxSize_ = 10;
nebula::SessionPool pool(config);
pool.init();

std::vector<std::thread> threads;
for (std::size_t i = 0; i < config.maxSize_; ++i) {
threads.emplace_back([&pool]() {
auto resp = pool.execute("YIELD 1");
assert(resp.errorCode == nebula::ErrorCode::SUCCEEDED);
std::cout << "Result: " << *resp.data << std::endl;
});
}
for (auto& t : threads) {
t.join();
}
}
1 change: 1 addition & 0 deletions exported-symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ global:
nebula::ConnectionPool::*;
"nebula::init(int*, char***)";
nebula::Session::*;
nebula::SessionPool::*;
nebula::Value::*;
nebula::DataSet::*;
nebula::Date::*;
Expand Down

0 comments on commit dad0939

Please sign in to comment.