Skip to content

Commit c3f0cf9

Browse files
Cache and fix for runGetMethod
1 parent f37ad0e commit c3f0cf9

17 files changed

+480
-426
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ COPY CMakeLists.txt /app/CMakeLists.txt
2626

2727
WORKDIR /app/build
2828
RUN cmake -DCMAKE_BUILD_TYPE=Release -DPORTABLE=1 .. && make -j$(nproc) && make install
29-
COPY config/static_config_compose.yaml /app/static_config.yaml
29+
COPY config/static_config.yaml /app/static_config.yaml
3030
ENTRYPOINT [ "ton-http-api-cpp" ]
3131

3232
# FROM ubuntu:24.04

config/static_config.yaml

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,97 @@
11
components_manager:
22
task_processors:
33
main-task-processor:
4-
worker_threads: 16
4+
worker_threads: $main_worker_threads
5+
worker_threads#fallback: 4
56
fs-task-processor:
6-
worker_threads: 2
7+
thread_name: blocking_thread
8+
worker_threads: $fs_worker_threads
9+
worker_threads#fallback: 2
710
monitor-task-processor:
811
thread_name: monitor_thread
9-
worker_threads: 1
12+
worker_threads: $monitor_worker_threads
13+
worker_threads#fallback: 1
1014
default_task_processor: main-task-processor
1115
components:
1216
server:
1317
listener:
14-
port: 8877
18+
port: $server_port
19+
port#fallback: 8081
1520
connection:
1621
in_buffer_size: 1048576
1722
requests_queue_size_threshold: 1024
1823
task_processor: main-task-processor
24+
handler-defaults:
25+
max_url_size: 8192
26+
max_request_size: 1048576
27+
max_headers_size: 65536
28+
set_tracing_headers: false
1929
listener-monitor:
20-
port: 8878
30+
port: $monitor_port
31+
port#fallback: 8082
2132
task_processor: monitor-task-processor
2233
logging:
2334
fs-task-processor: fs-task-processor
2435
loggers:
2536
default:
26-
file_path: "@null"
27-
level: warning
28-
format: json
37+
file_path: $system_log_path
38+
file_path#fallback: "@stdout"
39+
level: $system_log_level
40+
level#fallback: warning
41+
format: $log_format
42+
format#fallback: json
2943
overflow_behavior: discard
3044
api-v2:
31-
file_path: "@stdout"
32-
level: info
33-
format: json
45+
file_path: $log_path
46+
file_path#fallback: "@stdout"
47+
level: $log_level
48+
level#fallback: warning
49+
format: $log_format
50+
format#fallback: json
3451
overflow_behavior: discard
3552
tonlib:
36-
global_config: ./private/global-config.json
37-
keystore: /tmp/keystore/
38-
threads: 16
39-
external_message_endpoints:
53+
global_config: $tonlib_config_path
54+
keystore: $tonlib_keystore_path
55+
keystore#fallback: /tmp/keystore/
56+
threads: $tonlib_threads
57+
threads#fallback: 4
58+
external_message_endpoints: $tonlib_boc_endpoints
59+
external_message_endpoints#fallback: []
4060
task_processor: main-task-processor
61+
cache-api-v2:
62+
ways: 64
63+
size: 1024
64+
lifetime: 1
65+
background-update: true
4166
dns-client:
4267
fs-task-processor: fs-task-processor
4368
http-client:
4469
pool-statistics-disable: false
4570
thread-name-prefix: http-client
46-
threads: 8
71+
threads: $http_worker_threads
72+
threads#fallback: 8
4773
fs-task-processor: fs-task-processor
4874
destination-metrics-auto-max-size: 100
49-
user-agent: empty
75+
user-agent: $http_worker_user_agent
76+
user-agent#fallback: empty
5077
handler-server-monitor:
51-
path: /stats
78+
path: $monitor_path
79+
path#fallback: /stats
5280
method: GET
5381
task_processor: monitor-task-processor
5482
format: json
5583
handler-ping:
56-
path: /health
84+
path: $ping_path
85+
path#fallback: /health
5786
method: GET
5887
task_processor: main-task-processor
5988
log-level: warning
89+
throttling_enabled: false
90+
url_trailing_slash: strict-match
6091
handler-api-v2:
6192
path: /api/v2/{ton_api_method}
6293
method: GET,POST
94+
status-codes-log-level:
95+
409: debug
96+
500: debug
6397
task_processor: main-task-processor

config/static_config_compose.yaml

Lines changed: 0 additions & 97 deletions
This file was deleted.

docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
build:
55
context: .
66
dockerfile: Dockerfile
7-
command: --config /app/static_config.yaml
7+
command: --config /app/static_config.yaml --config_vars /run/secrets/config-vars
88
ports:
99
- ${SERVER_PORT:-8081}:8081
1010
- ${MONITOR_PORT:-8082}:8082

ton-http-api/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ set(TON_HTTP_API_CPP_SOURCE
1616
handler_api_v2.cpp
1717
handler_api_v2.h
1818
tonlib_postprocessor.cpp
19-
utils.hpp
20-
tvm_utils.h
21-
tvm_utils.cpp
19+
utils.cpp
2220
tokens-tlb.cpp
21+
cache.cpp
22+
request.hpp
2323
)
2424

2525
# generate tokens schemas

ton-http-api/cache.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "cache.hpp"
2+
3+
#include "userver/cache/lru_cache_component_base.hpp"
4+
#include "userver/components/component_context.hpp"
5+
#include "userver/components/statistics_storage.hpp"
6+
#include "userver/yaml_config/merge_schemas.hpp"
7+
8+
9+
userver::yaml_config::Schema ton_http::cache::impl::GetExpirableLruCacheStaticConfigSchema() {
10+
return userver::yaml_config::MergeSchemas<userver::components::ComponentBase>(R"(
11+
type: object
12+
description: Expirable LRU-cache component
13+
additionalProperties: false
14+
properties:
15+
size:
16+
type: integer
17+
description: max amount of items to store in cache
18+
ways:
19+
type: integer
20+
description: number of ways for associative cache
21+
lifetime:
22+
type: string
23+
description: TTL for cache entries (0 is unlimited)
24+
defaultDescription: 0
25+
background-update:
26+
type: boolean
27+
description: enables asynchronous updates for expiring values
28+
defaultDescription: false
29+
config-settings:
30+
type: boolean
31+
description: enables dynamic reconfiguration with CacheConfigSet
32+
defaultDescription: true
33+
)");
34+
}

ton-http-api/cache.hpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma once
2+
#include <functional>
3+
4+
#include "request.hpp"
5+
#include "userver/cache/expirable_lru_cache.hpp"
6+
#include "userver/components/component_base.hpp"
7+
#include "userver/components/component_context.hpp"
8+
#include "userver/components/statistics_storage.hpp"
9+
#include "userver/utils/statistics/entry.hpp"
10+
#include "userver/yaml_config/schema.hpp"
11+
12+
13+
namespace ton_http::cache {
14+
15+
namespace impl {
16+
userver::yaml_config::Schema GetExpirableLruCacheStaticConfigSchema();
17+
}
18+
19+
// clang-format on
20+
template <typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
21+
// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
22+
class ExpirableLruCacheComponent : public userver::components::ComponentBase {
23+
public:
24+
using Cache = userver::cache::ExpirableLruCache<Key, Value, Hash, Equal>;
25+
26+
ExpirableLruCacheComponent(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context)
27+
: ComponentBase(config, context), name_(userver::components::GetCurrentComponentName(config)),
28+
static_config_(config), cache_(std::make_shared<Cache>(static_config_.ways, static_config_.GetWaySize())) {
29+
cache_->SetMaxLifetime(static_config_.config.lifetime);
30+
cache_->SetBackgroundUpdate(static_config_.config.background_update);
31+
32+
statistics_holder_ = context.FindComponent<userver::components::StatisticsStorage>().GetStorage().RegisterWriter(
33+
"cache", [this](userver::utils::statistics::Writer& writer) {
34+
writer = *cache_;
35+
}, {{"cache_name", name_}});
36+
}
37+
~ExpirableLruCacheComponent() {
38+
statistics_holder_.Unregister();
39+
}
40+
41+
static userver::yaml_config::Schema GetStaticConfigSchema() {
42+
return impl::GetExpirableLruCacheStaticConfigSchema();
43+
}
44+
45+
std::optional<Value> Get(const Key& key) {
46+
return cache_->GetOptionalNoUpdate(key);
47+
}
48+
void Put(const Key& key, const Value& value) {
49+
return cache_->Put(key, value);
50+
}
51+
private:
52+
const std::string name_;
53+
const userver::cache::LruCacheConfigStatic static_config_;
54+
const std::shared_ptr<Cache> cache_;
55+
56+
userver::utils::statistics::Entry statistics_holder_;
57+
};
58+
59+
class CacheApiV2Component final : public ExpirableLruCacheComponent<handlers::TonlibApiRequest, userver::formats::json::Value> {
60+
public:
61+
static constexpr std::string_view kName = "cache-api-v2";
62+
CacheApiV2Component(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context)
63+
: ExpirableLruCacheComponent(config, context) {};
64+
};
65+
66+
class ConstCacheApiV2Component final : public ExpirableLruCacheComponent<handlers::TonlibApiRequest, userver::formats::json::Value> {
67+
public:
68+
static constexpr std::string_view kName = "const-cache-api-v2";
69+
ConstCacheApiV2Component(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context)
70+
: ExpirableLruCacheComponent(config, context) {};
71+
};
72+
// clang-format off
73+
}

0 commit comments

Comments
 (0)