jeremyczhen / fdbus

FDBus - Fast Distributed Bus
https://blog.csdn.net/jeremy_cz/article/details/89060291
161 stars 85 forks source link

How to use "void CFdbBaseObject::getSubscribeTable(...)" API conveniently without limitation "must be used within context" #22

Open lijinxing-source opened 3 years ago

lijinxing-source commented 3 years ago

I found the API "void CFdbBaseObject::getSubscribeTable(...)" cannot be called in worker(self-thread). because: 1.the API"void CFdbBaseObject::subscribe(CFdbSession session, FdbMsgCode_t msg, FdbObjectId_t obj_id, const char filter)" is only called in CONTEXT thread. 2.both getSubscribeTable and subscribe have no lock/unlock mechanism.

But, usually, the user like to create a CBaseClient or CBaseServer with a separate worker attached, which will easily cause the "getSubscribeTable" API called in worker thread. not very friendly.

So,in order to use getSubscribeTable without any limitation. i have an immature idea to add lock mechanism in below interfaces: 1.CFdbBaseObject::subscribe(CFdbSession session, FdbMsgCode_t msg, FdbObjectId_t obj_id, const char filter) 2.void CFdbBaseObject::unsubscribe(CFdbSession session, FdbMsgCode_t msg, FdbObjectId_t obj_id, const char filter) 3.void CFdbBaseObject::unsubscribe(CFdbSession session) 4.void CFdbBaseObject::unsubscribe(FdbObjectId_t obj_id) 5.void CFdbBaseObject::broadcast(CFdbMessage msg) 6.bool CFdbBaseObject::broadcast(CFdbMessage msg, CFdbSession session) 7.void CFdbBaseObject::getSubscribeTabl(...)

But, i am afraid it will take side effect for the whole design and sequence.
Wish your appropriate advices!!!

jeremyczhen commented 3 years ago

At this moment, since all data structures are handled in context thread, there is no need to protect them with mutex lock. On the other hand, there is not need for user to call CFdbBaseObject::getSubscribeTable(...). Could you let me know under which cast should you call this method?

Basically a server doesn't need to know who registered an event. What it need to do is to broadcast event to all clients who registered the event by calling 'broadcast()' method.

Anyway it is still possible for you to obtain the subscribe table in your worker: 1) don't assign worker to the server, so that the CBaseObject::onXXX() is called at context thread; 2) call CFdbBaseObject::getSubscribeTable(...) to get a list of subscribe table; 3) throw the table to your worker with a job

Currently only one context thread is allowed for each process. In the future, more than one context thread will be supported. For example, several endpoints can share context thread A, meanwhile other endpoints can share context thread B.