Skip to content

Commit 65334fc

Browse files
authored
Merge pull request #36 from lkpworkspace/dev
v0.9.6 - other: - 日志大小可配置 - 新增接收队列溢出告警日志 - 设置默认运行队列最大为2个 - 新增API使用示例
2 parents bbb8149 + 90098c5 commit 65334fc

15 files changed

+112
-96
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10)
22
if (POLICY CMP0091)
33
cmake_policy(SET CMP0091 NEW)
44
endif()
5-
project(myframe VERSION 0.9.5)
5+
project(myframe VERSION 0.9.6)
66

77
### option
88
option(MYFRAME_USE_CV "Using conditional variables for thread communication" ON)

README.md

+9-63
Original file line numberDiff line numberDiff line change
@@ -37,72 +37,18 @@ cmake -S . -B build -DCMAKE_INSTALL_PREFIX="./output" -DCMAKE_PREFIX_PATH="./out
3737
cmake --build build -j --config Release --target install
3838
```
3939

40-
### Hello,World 示例
41-
```c
42-
#include <string.h>
43-
#include <iostream>
40+
### Hello,World API示例
41+
- [API 示例](test/hello_test.cpp)
4442

45-
#include "myframe/msg.h"
46-
#include "myframe/actor.h"
47-
48-
using namespace myframe;
49-
/*
50-
该actor实现:
51-
自己给自己发送一条消息
52-
*/
53-
class Demo : public Actor
54-
{
55-
public:
56-
/* actor模块加载完毕后调用 */
57-
int Init(const char* param) override {
58-
/* 构造 hello,world 消息发送给自己 */
59-
auto mailbox = GetMailbox();
60-
mailbox->Send("actor.demo.echo_hello_world", std::make_shared<Msg>("hello,world"));
61-
}
62-
63-
void Proc(const std::shared_ptr<const Msg>& msg) override {
64-
/* 获得文本消息, 打印 源actor地址 目的actor地址 消息内容 */
65-
std::cout << *msg << ": " << msg->GetData() << std::endl;
66-
}
67-
};
68-
69-
/* 框架根据描述文件创建actor实例函数 */
70-
extern "C" std::shared_ptr<Actor> actor_create(const std::string& actor_name) {
71-
if (actor_name == "demo") {
72-
return std::make_shared<Demo>();
73-
}
74-
return nullptr;
75-
}
76-
77-
```
78-
79-
### Hello,World 配置文件
80-
```json
81-
{
82-
"type":"library",
83-
"lib":"demo",
84-
"actor":{
85-
"demo":[
86-
{
87-
"instance_name":"echo_hello_world",
88-
"instance_params":""
89-
}
90-
]
91-
}
92-
}
93-
```
94-
- type: [ library | class ]
95-
- lib: 库名称
96-
- actor: 需要创建的actor列表
97-
- demo: actor名
98-
- instance_name:实例名称
99-
- instance_params:实例参数
43+
### Hello,World 组件示例
44+
- [组件代码示例](examples/example_actor_helloworld.cpp)
45+
- [组件配置示例](examples/example_actor_helloworld.json)
10046

10147
## 程序接口
102-
- [Example](https://github.com/lkpworkspace/myframe/tree/master/examples)
103-
- [Actor模块](https://github.com/lkpworkspace/myframe/blob/master/myframe/actor.h)
104-
- [Worker模块](https://github.com/lkpworkspace/myframe/blob/master/myframe/worker.h)
105-
- [Msg模块](https://github.com/lkpworkspace/myframe/blob/master/myframe/msg.h)
48+
- [Example](examples)
49+
- [Actor模块](myframe/actor.h)
50+
- [Worker模块](myframe/worker.h)
51+
- [Msg模块](myframe/msg.h)
10652

10753
## 文档
10854
- [开发手册](doc/development_guide.md)

cpplint.bash

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function main() {
1818
-name "*.hxx" -or \
1919
-name "*.cxx" -or \
2020
-name "*.cuh" \
21-
')' -and -not -path "./build/*" \
22-
-and -not -path "./output/*" \
21+
')' -and -not -path "./build*" \
22+
-and -not -path "./output*" \
2323
-and -not -path "./3rd/*" \
2424
-and -not -path "./myframe/export.h" \
2525
| xargs python3 ./cpplint.py

launcher/conf/sys.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2-
"default_pending_queue_size":-1,
3-
"default_run_queue_size":-1,
42
"thread_poll_size":4,
53
"conn_event_size":2,
6-
"warning_msg_size":10
4+
"warning_msg_size":10,
5+
"log_dir":"log",
6+
"lib_dir":"",
7+
"service_dir":"service",
8+
"default_pending_queue_size":-1,
9+
"default_run_queue_size":2,
10+
"log_max_size_mb":100
711
}

launcher/launcher.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ int main(int argc, char** argv) {
4242
log_dir = module_args.GetLogDir();
4343
}
4444
log_dir = myframe::Common::GetAbsolutePath(log_dir.string());
45-
myframe::InitLog(log_dir, module_args.GetProcessName());
45+
myframe::InitLog(
46+
log_dir,
47+
module_args.GetProcessName(),
48+
module_args.GetLogMaxSizeMB());
4649
LOG(INFO) << "launch command: " << module_args.GetCmd();
4750

4851
if (module_args.GetLibDir().empty()) {

launcher/module_argument.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void ModuleArgument::ParseArgument(
6161
process_name_ = process_name;
6262
}
6363

64+
// 命令行参数优先级大于配置文件优先级
6465
auto sys_conf = parser_.get<std::string>("sys_conf");
6566
if (!sys_conf.empty()) {
6667
if (!ParseSysConf(sys_conf)) {
@@ -74,10 +75,6 @@ void ModuleArgument::ParseArgument(
7475
conf_dir_ = dir;
7576
}
7677

77-
for (size_t i = 0; i < parser_.rest().size(); i++) {
78-
conf_list_.emplace_back(parser_.rest()[i]);
79-
}
80-
8178
auto log_dir = parser_.get<std::string>("log_dir");
8279
if (!log_dir.empty()) {
8380
log_dir_ = log_dir;
@@ -87,6 +84,10 @@ void ModuleArgument::ParseArgument(
8784
if (!lib_dir.empty()) {
8885
lib_dir_ = lib_dir;
8986
}
87+
88+
for (size_t i = 0; i < parser_.rest().size(); i++) {
89+
conf_list_.emplace_back(parser_.rest()[i]);
90+
}
9091
}
9192

9293
bool ModuleArgument::ParseSysConf(const std::string& sys_conf) {
@@ -139,6 +140,10 @@ bool ModuleArgument::ParseSysConf(const std::string& sys_conf) {
139140
&& root["default_run_queue_size"].isInt()) {
140141
default_run_queue_size_ = root["default_run_queue_size"].asInt();
141142
}
143+
if (root.isMember("log_max_size_mb")
144+
&& root["log_max_size_mb"].isInt()) {
145+
log_max_size_mb_ = root["log_max_size_mb"].asInt();
146+
}
142147
return true;
143148
}
144149

launcher/module_argument.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ModuleArgument final {
3434
inline int GetDefaultRunQueueSize() const {
3535
return default_run_queue_size_;
3636
}
37+
inline int GetLogMaxSizeMB() const { return log_max_size_mb_; }
3738

3839
private:
3940
bool ParseSysConf(const std::string&);
@@ -42,7 +43,8 @@ class ModuleArgument final {
4243
int conn_event_size_{2};
4344
int warning_msg_size_{10};
4445
int default_pending_queue_size_{-1};
45-
int default_run_queue_size_{-1};
46+
int default_run_queue_size_{2};
47+
int log_max_size_mb_{100};
4648
std::string log_dir_;
4749
std::string lib_dir_;
4850
std::string conf_dir_;

myframe/app.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: 李柯鹏 <[email protected]>
88
#include "myframe/app.h"
99

1010
#include <regex>
11+
#include <utility>
1112

1213
#include "myframe/log.h"
1314
#include "myframe/platform.h"
@@ -509,9 +510,6 @@ void App::DispatchMsg(std::shared_ptr<Msg> msg) {
509510
}
510511

511512
void App::DispatchMsg(std::list<std::shared_ptr<Msg>>* msg_list) {
512-
LOG_IF(WARNING,
513-
msg_list->size() > warning_msg_size_.load())
514-
<< " dispatch msg too many";
515513
for (auto& msg : (*msg_list)) {
516514
DispatchMsg(std::move(msg));
517515
}

myframe/app.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@ Author: 李柯鹏 <[email protected]>
66
****************************************************************************/
77
#pragma once
88
#include <atomic>
9-
#include <list>
109
#include <memory>
1110
#include <mutex>
12-
#include <unordered_map>
13-
#include <vector>
11+
#include <list>
1412
#include <string>
15-
#include <filesystem>
16-
#include <utility>
13+
#include <vector>
1714

1815
#include <json/json.h>
1916

2017
#include "myframe/macros.h"
2118
#include "myframe/event.h"
2219
#include "myframe/export.h"
23-
24-
namespace stdfs = std::filesystem;
20+
#include "myframe/common.h"
2521

2622
namespace myframe {
2723

@@ -61,7 +57,7 @@ class MYFRAME_EXPORT App final : public std::enable_shared_from_this<App> {
6157
int event_conn_size = 2,
6258
int warning_msg_size = 10,
6359
int default_pending_queue_size = -1,
64-
int default_run_queue_size = -1);
60+
int default_run_queue_size = 2);
6561

6662
int LoadServiceFromDir(const std::string& path);
6763

@@ -145,7 +141,7 @@ class MYFRAME_EXPORT App final : public std::enable_shared_from_this<App> {
145141
std::string node_addr_;
146142
///
147143
int default_pending_queue_size_{-1};
148-
int default_run_queue_size_{-1};
144+
int default_run_queue_size_{2};
149145
std::atomic<std::size_t> warning_msg_size_{10};
150146
std::atomic<State> state_{kUninitialized};
151147
std::recursive_mutex local_mtx_;

myframe/log.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ static void signal_handler(const char *data, size_t size) {
1818

1919
namespace myframe {
2020

21-
void InitLog(const stdfs::path& log_dir, const std::string& bin_name) {
21+
void InitLog(
22+
const stdfs::path& log_dir,
23+
const std::string& bin_name,
24+
int max_size_mb) {
2225
google::InitGoogleLogging(bin_name.c_str());
2326

2427
FLAGS_logbufsecs = 0;
25-
FLAGS_max_log_size = 100;
28+
FLAGS_max_log_size = max_size_mb;
2629
FLAGS_stop_logging_if_full_disk = true;
2730

2831
std::string dst_str = (log_dir / bin_name).string();

myframe/log.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ Author: 李柯鹏 <[email protected]>
66
****************************************************************************/
77
#pragma once
88
#include <string>
9-
#include <filesystem>
109

1110
#include <glog/logging.h>
1211

1312
#include "myframe/export.h"
14-
15-
namespace stdfs = std::filesystem;
13+
#include "myframe/common.h"
1614

1715
namespace myframe {
1816

1917
MYFRAME_EXPORT void InitLog(
2018
const stdfs::path& log_dir,
21-
const std::string& bin_name);
19+
const std::string& bin_name,
20+
int max_size_mb = 100);
2221

2322
MYFRAME_EXPORT void ShutdownLog();
2423

myframe/mailbox.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: 李柯鹏 <[email protected]>
88
#include <utility>
99

1010
#include "myframe/msg.h"
11+
#include "myframe/log.h"
1112

1213
namespace myframe {
1314

@@ -74,6 +75,8 @@ void Mailbox::RecvClear() {
7475
void Mailbox::Recv(std::shared_ptr<Msg> msg) {
7576
if (pending_queue_size_ > 0) {
7677
for (; recv_.size() >= static_cast<std::size_t>(pending_queue_size_);) {
78+
LOG(WARNING) << Addr() << " pending queue overflow "
79+
<< recv_.size() << "/" << pending_queue_size_;
7780
recv_.pop_front();
7881
}
7982
}

myframe/mod_manager.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ Author: 李柯鹏 <[email protected]>
66
****************************************************************************/
77

88
#include "myframe/mod_manager.h"
9-
#include <filesystem>
109

10+
#include "myframe/common.h"
1111
#include "myframe/log.h"
1212
#include "myframe/shared_library.h"
1313
#include "myframe/actor.h"
1414
#include "myframe/worker.h"
1515

16-
namespace stdfs = std::filesystem;
17-
1816
namespace myframe {
1917

2018
ModManager::ModManager() {

test/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ cmake_minimum_required(VERSION 3.10)
44
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/performance_test_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/performance_test_config.h @ONLY)
55

66
### test bin
7+
add_executable(hello_test hello_test.cpp)
8+
target_link_libraries(hello_test
9+
myframe
10+
)
11+
712
add_executable(common_test common_test.cpp)
813
target_link_libraries(common_test
914
myframe
@@ -51,6 +56,7 @@ target_link_libraries(performance_trans100_fullspeed_test
5156

5257
### install
5358
INSTALL(TARGETS
59+
hello_test
5460
common_test
5561
actor_run_queue_test
5662
app_send_test

test/hello_test.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/****************************************************************************
2+
Copyright (c) 2019, 李柯鹏
3+
All rights reserved.
4+
5+
Author: 李柯鹏 <[email protected]>
6+
****************************************************************************/
7+
#include <iostream>
8+
#include "myframe/msg.h"
9+
#include "myframe/actor.h"
10+
#include "myframe/mod_manager.h"
11+
#include "myframe/app.h"
12+
13+
#include "performance_test_config.h"
14+
15+
class Hello : public myframe::Actor {
16+
public:
17+
/* actor模块加载完毕后调用 */
18+
int Init(const char* param) override {
19+
(void)param;
20+
/* 构造 hello,world 消息发送给自己 */
21+
auto mailbox = GetMailbox();
22+
mailbox->Send(
23+
mailbox->Addr(),
24+
std::make_shared<myframe::Msg>("hello,world"));
25+
return 0;
26+
}
27+
28+
void Proc(const std::shared_ptr<const myframe::Msg>& msg) override {
29+
/* 获得文本消息, 打印 源actor地址 目的actor地址 消息内容 */
30+
std::cout << *msg << ": " << msg->GetData() << std::endl;
31+
}
32+
};
33+
34+
int main() {
35+
auto lib_dir =
36+
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string();
37+
38+
auto app = std::make_shared<myframe::App>();
39+
if (false == app->Init(lib_dir, 1)) {
40+
std::cout << "Init failed\n";
41+
return -1;
42+
}
43+
44+
auto& mod = app->GetModManager();
45+
46+
mod->RegActor("Hello", [](const std::string&) {
47+
return std::make_shared<Hello>();
48+
});
49+
auto actor = mod->CreateActorInst("class", "Hello");
50+
app->AddActor("1", "", actor);
51+
52+
return app->Exec();
53+
}

0 commit comments

Comments
 (0)