multithreading - Nodejs synchronicity between requests -
i understand asynchronous nature of node , async programming concepts, have been doing long time. in situation need simultaneous requests check "in sync" if will. let me explain:
- usera requests resource doesn't exist.
- set flag "isprocessing" let others know resource being created.
- userb requests same resource, sees it's processing, , waits ready (using polling - don't focus on this, part works way want)
- [a bunch of heavy async stuff happens generate resource]
- resource ready, polling notified (like said, part works)
the part has me in pickle "step 2" set "isprocessing" flag. asynchronous database operation (mongo) not yet complete when 2+ requests come in at same time. result, multiple requests attempting create same resource. problem goes away if requests separated enough time database write happen (~5ms), not "simultaneous" requests.
can offer advice solve this. seems need set "isprocessing" flag in synchronous (thread-blocking) way. know file system has synchronous operations - way go? using node on multi-core machine scale cloud day. using pm2 distribute our app across multiple cores. advice appreciated.
you need database access right. when say
update mylocktable set isprocessing = 1 isprocessing = 0
you 0 rows modified if isprocessing set 1. otherwise, 1 row. how can tell 2 cases apart , start heavy work once.
Comments
Post a Comment