NATS C Client with JetStream and Streaming support
3.9.1
The nats.io C Client, Supported by Synadia Communications Inc.
|
Functions | |
natsStatus | nats_OpenWithConfig (natsClientConfig *config) |
Initializes the library. More... | |
NATS_EXTERN natsStatus | nats_Open (int64_t lockSpinCount) |
Initializes the library. More... | |
NATS_EXTERN const char * | nats_GetVersion (void) |
Returns the Library's version. More... | |
NATS_EXTERN uint32_t | nats_GetVersionNumber (void) |
Returns the Library's version as a number. More... | |
NATS_EXTERN bool | nats_CheckCompatibility (void) |
Check that the header is compatible with the library. More... | |
NATS_EXTERN int64_t | nats_Now (void) |
Gives the current time in milliseconds. More... | |
NATS_EXTERN int64_t | nats_NowInNanoSeconds (void) |
Gives the current time in nanoseconds. More... | |
NATS_EXTERN void | nats_Sleep (int64_t sleepTime) |
Sleeps for a given number of milliseconds. More... | |
NATS_EXTERN const char * | nats_GetLastError (natsStatus *status) |
Returns the calling thread's last known error. More... | |
NATS_EXTERN natsStatus | nats_GetLastErrorStack (char *buffer, size_t bufLen) |
Returns the calling thread's last known error stack. More... | |
NATS_EXTERN void | nats_PrintLastErrorStack (FILE *file) |
Prints the calling thread's last known error stack into the file. More... | |
NATS_EXTERN natsStatus | nats_SetMessageDeliveryPoolSize (int max) |
Sets the maximum size of the global message delivery thread pool. More... | |
NATS_EXTERN void | nats_ReleaseThreadMemory (void) |
Release thread-local memory possibly allocated by the library. More... | |
NATS_EXTERN natsStatus | nats_Sign (const char *encodedSeed, const char *input, unsigned char **signature, int *signatureLength) |
Signs a given text using the provided private key. More... | |
NATS_EXTERN void | nats_Close (void) |
Tear down the library. More... | |
NATS_EXTERN natsStatus | nats_CloseAndWait (int64_t timeout) |
Tear down the library and wait for all resources to be released. More... | |
Library and helper functions.
natsStatus nats_OpenWithConfig | ( | natsClientConfig * | config | ) |
This initializes the library, with more control over how threads and/or thread pools are used to deliver messages to the application.
It is invoked automatically when creating a connection, with the default settings (same as nats_Open(-1)).
config | points to a natsClientConfig. A copy of the settings is made, so the config can be freed after initializing the NATS client. |
NATS_EXTERN natsStatus nats_Open | ( | int64_t | lockSpinCount | ) |
This initializes the library.
It is invoked automatically when creating a connection, using a default spin count. However, you can call this explicitly before creating the very first connection in order for your chosen spin count to take effect.
lockSpinCount | The number of times the library will spin trying to lock a mutex object. |
NATS_EXTERN const char* nats_GetVersion | ( | void | ) |
Returns the version of the library your application is linked with.
NATS_EXTERN uint32_t nats_GetVersionNumber | ( | void | ) |
The version is returned as an hexadecimal number. For instance, if the string version is "1.2.3", the value returned will be:
0x010203
NATS_EXTERN bool nats_CheckCompatibility | ( | void | ) |
The version of the header you used to compile your application may be incompatible with the library the application is linked with.
This function will check that the two are compatibles. If they are not, a message is printed and the application will exit.
true
if the header and library are compatibles, otherwise the application exits.NATS_EXTERN int64_t nats_Now | ( | void | ) |
Gives the current time in milliseconds.
NATS_EXTERN int64_t nats_NowInNanoSeconds | ( | void | ) |
Gives the current time in nanoseconds. When such granularity is not available, the time returned is still expressed in nanoseconds.
NATS_EXTERN void nats_Sleep | ( | int64_t | sleepTime | ) |
Causes the current thread to be suspended for at least the number of milliseconds.
sleepTime | the number of milliseconds. |
NATS_EXTERN const char* nats_GetLastError | ( | natsStatus * | status | ) |
Returns the calling thread's last known error. This can be useful when natsConnection_Connect fails. Since no connection object is returned, you would not be able to call natsConnection_GetLastError.
status | if not NULL , this function will store the last error status in there. |
NATS_EXTERN natsStatus nats_GetLastErrorStack | ( | char * | buffer, |
size_t | bufLen | ||
) |
Copies the calling thread's last known error stack into the provided buffer. If the buffer is not big enough, NATS_INSUFFICIENT_BUFFER is returned.
buffer | the buffer into the stack is copied. |
bufLen | the size of the buffer |
NATS_EXTERN void nats_PrintLastErrorStack | ( | FILE * | file | ) |
This call prints the calling thread's last known error stack into the file file
. It first prints the error status and the error string, then the stack.
Here is an example for a call:
file | the file the stack is printed to. |
NATS_EXTERN natsStatus nats_SetMessageDeliveryPoolSize | ( | int | max | ) |
Normally, each asynchronous subscriber that is created has its own message delivery thread. The advantage is that it reduces lock contentions, therefore improving performance.
However, if an application creates many subscribers, this is not scaling well since the process would use too many threads.
The library has a thread pool that can perform message delivery. If a connection is created with the proper option set (natsOptions_UseGlobalMessageDelivery), then this thread pool will be responsible for delivering the messages. The thread pool is lazily initialized, that is, no thread is used as long as no subscriber (requiring global message delivery) is created.
Each subscriber will be attached to a given worker on the pool so that message delivery order is guaranteed.
This call allows you to set the maximum size of the pool.
max | the maximum size of the pool. |
NATS_EXTERN void nats_ReleaseThreadMemory | ( | void | ) |
This needs to be called on user-created threads where NATS calls are performed. This does not need to be called in threads created by the library. For instance, do not call this function in the message handler that you specify when creating a subscription.
Also, you do not need to call this in an user thread (or the main) if you are calling nats_Close() there.
NATS_EXTERN natsStatus nats_Sign | ( | const char * | encodedSeed, |
const char * | input, | ||
unsigned char ** | signature, | ||
int * | signatureLength | ||
) |
The key is the encoded string representation of the private key, or seed. This is what you get when generating an NKey using NATS tooling.
The input is a string, generally the nonce sent by the server when accepting a connection.
This function signs the input and returns the signature through the output arguments. This call allocates memory necessary to hold the signature. If this is used as part of the signature callback passed to natsOptions_SetNKey(), then the memory will be automatically freed by the library after the signature has been inserted in the CONNECT protocol. If this function is used outside of this context, it is the user responsibility to free the allocated memory when no longer needed.
encodedSeed | the string encoded private key, also known as seed. |
input | the input to be signed. |
signature | the memory location of allocated memory containing the signed input. |
signatureLength | the size of the allocated signature. |
NATS_EXTERN void nats_Close | ( | void | ) |
Releases memory used by the library.
For this to take effect, all NATS objects that you have created must first be destroyed.
This call does not block and it is possible that the library is not unloaded right away if there are still internal threads referencing it, so calling nats_Open() right away may fail. If you want to ensure that the library is fully unloaded, call nats_CloseAndWait() instead.
NATS_EXTERN natsStatus nats_CloseAndWait | ( | int64_t | timeout | ) |
Similar to nats_Close() except that this call will make sure that all references to the library are decremented before returning (up to the given timeout). Internal threads (such as subscriptions dispatchers, etc..) hold a reference to the library. Only when all references have been released that this call will return. It means that you must call all the "destroy" calls before calling this function, otherwise it will block forever (or up to given timeout).
For instance, this code would "deadlock":
But this would work as expected:
The library and other objects (such as connections, subscriptions, etc) use internal threads. After the destroy call, it is possible or even likely that some threads are still running, holding references to the library. Unlike nats_Close(), which will simply ensure that the library is ultimately releasing memory, the nats_CloseAndWait() API will ensure that all those internal threads have unrolled and that the memory used by the library is released before returning.
timeout | the maximum time to wait for the library to be closed. If negative or 0, waits for as long as needed. |