Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mars2] mars multi-instance. #1128

Open
wants to merge 1 commit into
base: rb/2023-05
Choose a base branch
from
Open

Conversation

panchangpeng
Copy link
Collaborator

No description provided.

@tencent-adm
Copy link

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

#include "mars/comm/thread/thread.h"
#include "mars/comm/time_utils.h"

using namespace mars::comm;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

头文件中不要使用全局的 using namespace, 会污染命名空间


#define S_SCOPED_LOCK() std::unique_lock<std::recursive_mutex> s_lock(s_mutex_)
#define S_SCOPED_UNLOCK() s_lock.unlock()
static int sg_context_instance_counter = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个变量好像没啥用?

std::string clazz_name = typeid(T).name();
// when context delete, then manager map will be null.
if (!manager_map_.empty()) {
return (T*)manager_map_[clazz_name];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里一定会存在相应的 manager 吗


void SdtManager::OnDestroy() {
delete sdt_core_;
if (callback_) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的判断应该是没必要的

#include "mars/boot/context.h"
#include "sdt.h"

using namespace mars::boot;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

头文件中不应该有 using namespace

@@ -184,7 +184,7 @@ int SendHttpQuery(const std::string& _url, int& _status_code, std::string& _errm
}

// fix SplitHttpHeadAndBody crash
if (0 == recv_autobuf.Length()) {
if ( 0 == recv_autobuf.Length()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

格式化的问题

@@ -31,6 +31,9 @@
#include "mars/sdt/constants.h"
#include "mars/sdt/sdt.h"
#include "mars/sdt/netchecker_profile.h"
#include "mars/boot/context.h"

using namespace mars::boot;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace

xinfo_function();
weak_network_logic_ = new WeakNetworkLogic();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放在构造函数的初始化列表中?

@@ -313,6 +319,9 @@ void ShortLinkTaskManager::__RunOnStartTask() {

first->task.shortlink_host_list = hosts;

if (hosts.empty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么会存在 empty 的情况?之前不存在吗

#include "mars/boot/context.h"
#include "mars/stn/stn.h"

using namespace mars::boot;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using

}

void RemoveManagerWithName(std::string name, BaseManager* mgr) {
std::string clazz_name = mgr->GetName();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mgr 参数是否必要?看起来只用 name 就足够了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是给Android的接口,因为name对应的是Java对象,mgr取的是c++对象。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果name能唯一标识一个Java/C++对象,删除C++对象直接这样就行了吧?

std::string clazz_name = java_clazz_name_c_clazz_name_map_[name];
manager_map_.erase(clazz_name);

public:
static void JniCreateContext(JNIEnv* env, jobject instance, jstring context_id) {
auto context_cpp = boot::Context::CreateContext(j2c_cast(context_id));
auto contextWrapper = new jnicat::JniObjectWrapper<Context>(context_cpp);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原先 JniObjectWrapper 的实现很冗余,其实没必要 new JniObjectWrapper 对象出来,本质是调用 setHandle,getHandle,可以考虑简化。

}

void* GetManagerByName(std::string name) {
std::string clazz_name = java_clazz_name_c_clazz_name_map_[name];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里先find一下,不存在返回nullptr比较合理,否则两个map中会存在很多无用数据

}
} else {
auto context = new Context();
auto tmp_context_id = std::to_string(reinterpret_cast<uint64_t>(context));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对象地址很容易复用,直接用对象地址作为context_id并不能保证唯一性。如果context_id需要全局唯一,比如log中要能区分出来,最好拼上一个自增id

const AutoBuffer& _extend);
// 底层获取task要发送的数据
bool Req2Buf(uint32_t taskid,
void* const user_context,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无意义的const

AutoBuffer& outbuffer,
AutoBuffer& extend,
int& error_code,
const int channel_select,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无意义的const

@@ -108,16 +122,28 @@ NO_DESTROY static Thread sg_slproxyThread;
lock.lock();
sg_proxyInfo.ip = ips.front();
sg_gotProxy = true;

*/
AppManager* app_manager = Context::CreateContext("default")->GetManager<AppManager>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

冗余代码太多了,考虑抽成一个函数,用的时候:
if (auto app_manager = GetDefaultAppManager()) {
app_manager->xxx();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

或者直接 GetDefaultAppManager()->xxx() 也行

xinfo2(TSF"sdt oncreate");
SdtCore::Singleton::Instance();
*/
SdtManager* sdt_manager = Context::CreateContext("default")->GetManager<SdtManager>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大量冗余代码,同之前,建议抽成函数

xassert2(NULL != sdt_manager, "mars2 sdt_manager is empty.");
if (sdt_manager) {
sdt_manager->OnDestroy();
}
}

static void __initbind_baseprjevent() {

GetSignalOnCreate().connect(&onCreate);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多实例之后,旧的onCreate,onDestroy机制是不是可以干掉了?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants