Comments (2)
@hidai It does not make sense to me to be calling nats_Open
and/or nats_Close
concurrently.
Think about it. You would have code running trying to open the library, do some NATS operations, while another part of your code would be closing it? What does that say about the application?
Adding a lock in the library would mean that the lock would need to survive the close, which then would be reported as memory in use (which some will report as memory leak, which is not). So if need be I will then document that these should not be called concurrently.
from nats.c.
Now I think that my problem should be addressed not by the library side but by the user side.
But let me explain what I wanted to do here since this might be useful for other users who have similar use-case with me.
The problem I was trying to deal with is Case 3 in the figure below.
Case 1
client A A--------->|
client B B----------->|
opened? |<---------------->|
^ ^
| |
nats_Open nats_Close
Case 2
client A A--------->|
client B B----------->|
opened? |<-------->| |<---------->|
^ ^ ^ ^
| | | |
nats_Open | nats_Open |
nats_Close nats_Close
Case 3
client A A--------->|
client B B----------->|
opened? |<-------->|<---------->|
^ ^
| |
nats_Open !!race condition!!
nats_Close and nats_Open are called concurrently
The race condition can be properly handled by user side using an integer counter and a mutex like below.
class Nats {
public:
Nats() {
std::lock_guard<std::mutex> lock(m);
nats_Open(-1);
++c;
}
~Nats() {
std::lock_guard<std::mutex> lock(m);
if (c == 1) {
nats_Close();
}
--c;
}
private:
static int c = 0;
static std::mutex m;
};
from nats.c.
Related Issues (20)
- kvStore_Purge is not removing keys HOT 7
- natsConnection_Connect can get stuck in a very long, unproductive loop HOT 1
- undefined reference to `pthread_mutexattr_init'
- single-threaded mode?
- Not all functions exported in the Windows DLL
- auth callout
- Implement Consumer Pause HOT 2
- Provide Conan recipe HOT 2
- natsOptions_SetTimeout() not being respected under certain conditions
- Example js-sub.c is missing stream parameter in usage HOT 1
- Documentation for Services HOT 1
- callbacks on natsConnection are called after natsConnection_destroy HOT 6
- Compile error on Linux HOT 1
- microEndpoint in microErrorHandler doesn't seem useful
- micro commands give parser errors on names HOT 1
- Request returns error status on callback, rather than from the method itself
- Please make the kvStore watcher async HOT 1
- natsConnection_SubscribeTimeout callback called twice having auto-unsubscribe after 1 message HOT 7
- nats c client hanging forever on natsConnection_PublishString
- Support URL (%-) encoding in server URL strings
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nats.c.