fdbus icon indicating copy to clipboard operation
fdbus copied to clipboard

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

Open lijinxing-source opened this issue 4 years ago • 1 comments

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!!!

lijinxing-source avatar Dec 04 '20 08:12 lijinxing-source

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.

jeremyczhen avatar Dec 16 '20 15:12 jeremyczhen