-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathnats.h
8893 lines (8158 loc) · 315 KB
/
nats.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef NATS_H_
#define NATS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
#include "status.h"
#include "version.h"
/** \def NATS_EXTERN
* \brief Needed for shared library.
*
* Based on the platform this is compiled on, it will resolve to
* the appropriate instruction so that objects are properly exported
* when building the shared library.
*/
#if defined(_WIN32)
#include <winsock2.h>
#if defined(nats_EXPORTS)
#define NATS_EXTERN __declspec(dllexport)
#elif defined(nats_IMPORTS)
#define NATS_EXTERN __declspec(dllimport)
#else
#define NATS_EXTERN
#endif
typedef SOCKET natsSock;
#else
#define NATS_EXTERN
typedef int natsSock;
#endif
/*! \mainpage %NATS C client.
*
* \section intro_sec Introduction
*
* The %NATS C Client is part of %NATS, an open-source cloud-native
* messaging system, and is supported by [Synadia Communications Inc.](http://www.synadia.com).
* This client, written in C, follows the go client closely, but
* diverges in some places.
*
* \section install_sec Installation
*
* Instructions to build and install the %NATS C Client can be
* found at the [NATS C Client GitHub page](https://github.com/nats-io/nats.c)
*
* \section faq_sec Frequently Asked Questions
*
* Some of the frequently asked questions can be found [here](https://github.com/nats-io/nats.c#faq)
*
* \section other_doc_section Other Documentation
*
* This documentation focuses on the %NATS C Client API; for additional
* information, refer to the following:
*
* - [General Documentation for nats.io](http://nats.io/documentation)
* - [NATS C Client found on GitHub](https://github.com/nats-io/nats.c)
* - [The NATS Server (nats-server) found on GitHub](https://github.com/nats-io/nats-server)
*/
/** \brief The default `NATS Server` URL.
*
* This is the default URL a `NATS Server`, running with default listen
* port, can be reached at.
*/
#define NATS_DEFAULT_URL "nats://localhost:4222"
/** \brief Message header for JetStream messages representing the message payload size
*
* When creating a JetStream consumer, if the `HeadersOnly` boolean is specified,
* the subscription will receive messages with headers only (no message payload),
* and a header of this name containing the size of the message payload that was
* omitted.
*
* @see jsConsumerConfig
*/
#define JSMsgSize "Nats-Msg-Size"
/** \brief Message header for JetStream message for rollup
*
* If message is sent to a stream's subject with this header set, and the stream
* is configured with `AllowRollup` option, then the server will insert this
* message and delete all previous messages in the stream.
*
* If the header is set to #JSMsgRollupSubject, then only messages on the
* specific subject this message is sent to are deleted.
*
* If the header is set to #JSMsgRollupAll, then all messages on all subjects
* are deleted.
*/
#define JSMsgRollup "Nats-Rollup"
/** \brief Message header value causing rollup per subject
*
* This is a possible value for the #JSMsgRollup header indicating that only
* messages for the subject the rollup message is sent will be removed.
*
* @see JSMsgRollup
*/
#define JSMsgRollupSubject "sub"
/** \brief Message header value causing rollup for all subjects
*
* This is a possible value for the #JSMsgRollup header indicating that all
* messages for all subjects will be removed.
*
* @see JSMsgRollup
*/
#define JSMsgRollupAll "all"
// Headers for republished messages and direct get.
#define JSStream "Nats-Stream"
#define JSSequence "Nats-Sequence"
#define JSLastSequence "Nats-Last-Sequence"
#define JSTimeStamp "Nats-Time-Stamp"
#define JSSubject "Nats-Subject"
//
// Types.
//
/** \defgroup typesGroup Types
*
* NATS Types.
* @{
*/
/** \brief A connection to a `NATS Server`.
*
* A #natsConnection represents a bare connection to a `NATS Server`. It will
* send and receive byte array payloads.
*/
typedef struct __natsConnection natsConnection;
/** \brief Statistics of a #natsConnection
*
* Tracks various statistics received and sent on a connection,
* including counts for messages and bytes.
*/
typedef struct __natsStatistics natsStatistics;
/** \brief Interest on a given subject.
*
* A #natsSubscription represents interest in a given subject.
*/
typedef struct __natsSubscription natsSubscription;
/** \brief A structure holding a subject, optional reply and payload.
*
* #natsMsg is a structure used by Subscribers and
* #natsConnection_PublishMsg().
*/
typedef struct __natsMsg natsMsg;
/** \brief Way to configure a #natsConnection.
*
* Options can be used to create a customized #natsConnection.
*/
typedef struct __natsOptions natsOptions;
/** \brief Unique subject often used for point-to-point communication.
*
* This can be used as the reply for a request. Inboxes are meant to be
* unique so that replies can be sent to a specific subscriber. That
* being said, inboxes can be shared across multiple subscribers if
* desired.
*/
typedef char natsInbox;
/** \brief An initial configuration for NATS client. Provides control over the
* threading model, and sets many default option values.
*
* @see natsOptions_Create
*/
typedef struct __natsClientConfig
{
int64_t DefaultWriteDeadline;
int64_t LockSpinCount;
// Subscription message delivery thread control
bool DefaultToThreadPool;
int ThreadPoolMax;
// Reply message delivery thread control
bool DefaultRepliesToThreadPool;
bool UseSeparatePoolForReplies;
int ReplyThreadPoolMax;
} natsClientConfig;
/** \brief A list of NATS messages.
*
* Used by some APIs which return a list of #natsMsg objects.
*
* Those APIs will not create the object, but instead initialize
* the object to which a pointer to that object will be passed to it.
* Typically, the user will define the object on the stack and
* pass a pointer to this object to APIs that require a pointer
* to a #natsMsgList object.
*
* Similarly, calling #natsMsgList_Destroy will call #natsMsg_Destroy
* on any message still in the list, free the array containing pointers
* to the messages, but not free the #natsMsgList object itself.
*
* \note If the user wants to keep some of the messages from the
* list, the pointers of those messages in the `Msgs` array should
* be set to `NULL`. The value `Count` MUST not be changed. The
* function #natsMsgList_Destroy will iterate through all
* pointers in the list and only destroy the ones that have not
* been set to `NULL`.
*
* @see natsMsgList_Destroy
*/
typedef struct natsMsgList
{
natsMsg **Msgs;
int Count;
} natsMsgList;
/** \brief A type to represent user-provided metadata, a list of k=v pairs.
*
* Used in JetStream, microservice configuration.
*/
typedef struct natsMetadata
{
// User-provided metadata for the stream, encoded as an array of {"key", "value",...}
const char **List;
// Number of key/value pairs in Metadata, 1/2 of the length of the array.
int Count;
} natsMetadata;
/**
* The JetStream context. Use for JetStream assets management and communication.
*
* \warning A context MUST not be destroyed concurrently with #jsCtx API calls
* (for instance #js_Publish or #js_PublishAsync, etc...). However, it
* is safe to destroy the context while a #jsPubAckErrHandler callback is
* running or while inside #js_PublishAsyncComplete.
*/
typedef struct __jsCtx jsCtx;
/**
* JetStream publish options.
*
* These are options that you can provide to JetStream publish APIs.
*
* The common usage will be to initialize a structure on the stack by
* calling #jsPubOptions_Init. Note that strings are owned by
* the application and need to be valid for the duration of the API
* call this object is passed to.
*
* \note It is the user responsibility to free the strings if they
* have been allocated.
*
* @see jsPubOptions_Init
*/
typedef struct jsPubOptions
{
int64_t MaxWait; ///< Amount of time (in milliseconds) to wait for a publish response, default will the context's Wait value.
const char *MsgId; ///< Message ID used for de-duplication.
const char *ExpectStream; ///< Expected stream to respond from the publish call.
const char *ExpectLastMsgId; ///< Expected last message ID in the stream.
uint64_t ExpectLastSeq; ///< Expected last message sequence in the stream.
uint64_t ExpectLastSubjectSeq; ///< Expected last message sequence for the subject in the stream.
bool ExpectNoMessage; ///< Expected no message (that is, sequence == 0) for the subject in the stream.
} jsPubOptions;
/**
* Determines how messages in a set are retained.
*/
typedef enum
{
js_LimitsPolicy = 0, ///< Specifies that messages are retained until any given limit is reached, which could be one of MaxMsgs, MaxBytes, or MaxAge. This is the default.
js_InterestPolicy, ///< Specifies that when all known observables have acknowledged a message it can be removed.
js_WorkQueuePolicy, ///< Specifies that when the first worker or subscriber acknowledges the message it can be removed.
} jsRetentionPolicy;
/**
* Determines how to proceed when limits of messages or bytes are reached.
*/
typedef enum
{
js_DiscardOld = 0, ///< Will remove older messages to return to the limits. This is the default.
js_DiscardNew, ///< Will fail to store new messages.
} jsDiscardPolicy;
/**
* Determines how messages are stored for retention.
*/
typedef enum
{
js_FileStorage = 0, ///< Specifies on disk storage. It's the default.
js_MemoryStorage, ///< Specifies in memory only.
} jsStorageType;
/**
* Determines how messages are compressed when stored for retention.
*/
typedef enum
{
js_StorageCompressionNone = 0, ///< Specifies no compression. It's the default.
js_StorageCompressionS2, ///< Specifies S2.
} jsStorageCompression;
/**
* Determines how the consumer should select the first message to deliver.
*/
typedef enum
{
js_DeliverAll = 0, ///< Starts from the very beginning of a stream. This is the default.
js_DeliverLast, ///< Starts with the last sequence received.
js_DeliverNew, ///< Starts with messages sent after the consumer is created.
js_DeliverByStartSequence, ///< Starts from a given sequence.
js_DeliverByStartTime, ///< Starts from a given UTC time (number of nanoseconds since epoch)
js_DeliverLastPerSubject, ///< Starts with the last message for all subjects received.
} jsDeliverPolicy;
/**
* Determines how the consumer should acknowledge delivered messages.
*/
typedef enum
{
js_AckExplicit = 0, ///< Requires ack or nack for all messages.
js_AckNone, ///< Requires no acks for delivered messages.
js_AckAll, ///< When acking a sequence number, this implicitly acks all sequences below this one as well.
} jsAckPolicy;
/**
* Determines how the consumer should replay messages it already has queued in the stream.
*/
typedef enum
{
js_ReplayInstant = 0, ///< Replays messages as fast as possible.
js_ReplayOriginal, ///< Maintains the same timing as the messages were received.
} jsReplayPolicy;
/**
* Used to guide placement of streams in clustered JetStream.
*
* Initialize the object with #jsPlacement_Init.
*
* \note The strings are applications owned and will not be freed by the library.
*
* See #jsStreamConfig for information on how to configure a stream.
*
* @see jsPlacement_Init
*/
typedef struct jsPlacement
{
const char *Cluster;
const char **Tags;
int TagsLen;
} jsPlacement;
/**
* Allows you to qualify access to a stream source in another account.
*
* Initialize the object with #jsExternalStream_Init.
*
* \note The strings are applications owned and will not be freed by the library.
*
* See #jsStreamConfig for information on how to configure a stream.
*/
typedef struct jsExternalStream
{
const char *APIPrefix;
const char *DeliverPrefix;
} jsExternalStream;
/**
* Dictates how streams can source from other streams.
*
* Initialize the object with #jsStreamSource_Init.
*
* \note The strings are applications owned and will not be freed by the library.
*
* \note The `OptStartTime` needs to be expressed as the number of nanoseconds
* passed since 00:00:00 UTC Thursday, 1 January 1970.
*
* See #jsStreamConfig for information on how to configure a stream.
*/
typedef struct jsStreamSource
{
const char *Name;
uint64_t OptStartSeq;
int64_t OptStartTime; ///< UTC time expressed as number of nanoseconds since epoch.
const char *FilterSubject;
jsExternalStream *External;
// Domain and External are mutually exclusive.
// If Domain is set, an External value will be created with
// the APIPrefix constructed based on the Domain value.
const char *Domain;
} jsStreamSource;
/**
* Allows a source subject to be mapped to a destination subject for republishing.
*/
typedef struct jsRePublish
{
const char *Source;
const char *Destination;
bool HeadersOnly;
} jsRePublish;
/**
* SubjectTransformConfig is for applying a subject transform (to matching
* messages) before doing anything else when a new message is received
*/
typedef struct jsSubjectTransformConfig
{
const char *Source;
const char *Destination;
} jsSubjectTransformConfig;
/**
* SubjectTransformConfig is for applying a subject transform (to matching
* messages) before doing anything else when a new message is received
*/
typedef struct jsStreamConsumerLimits
{
int64_t InactiveThreshold;
int MaxAckPending;
} jsStreamConsumerLimits;
/**
* Configuration of a JetStream stream.
*
* There are sensible defaults for most. If no subjects are
* given the name will be used as the only subject.
*
* In order to add/update a stream, a configuration needs to be set.
* The typical usage would be to initialize all required objects on the stack
* and configure them, then pass the pointer to the configuration to
* #js_AddStream or #js_UpdateStream.
*
* \note The strings are applications owned and will not be freed by the library.
*
* \note NATS server 2.10 added user-provided Metadata, storage Compression
* type, FirstSeq to specify the starting sequence number, and SubjectTransform.
*
* @see jsStreamConfig_Init
*
* \code{.unparsed}
* jsStreamConfig sc;
* jsPlacement p;
* jsStreamSource m;
* jsExternalStream esm;
* jsStreamSource s1;
* jsStreamSource s2;
* jsExternalStream esmS2;
* const char *subjects[] = {"foo", "bar"};
* const char *tags[] = {"tag1", "tag2"};
* jsStreamSource *sources[] = {&s1, &s2};
* jsRePublish rp;
*
* jsStreamConfig_Init(&sc);
*
* jsPlacement_Init(&p);
* p.Cluster = "MyCluster";
* p.Tags = tags;
* p.TagsLen = 2;
*
* jsStreamSource_Init(&m);
* m.Name = "AStream";
* m.OptStartSeq = 100;
* m.FilterSubject = "foo";
* jsExternalStream_Init(&esm);
* esm.APIPrefix = "mirror.prefix.";
* esm.DeliverPrefix = "deliver.prefix.";
* m.External = &esm;
*
* jsStreamSource_Init(&s1);
* s1.Name = "StreamOne";
* s1.OptStartSeq = 10;
* s1.FilterSubject = "stream.one";
*
* jsStreamSource_Init(&s2);
* s2.Name = "StreamTwo";
* s2.FilterSubject = "stream.two";
* jsExternalStream_Init(&esmS2);
* esmS2.APIPrefix = "mirror.prefix.";
* esmS2.DeliverPrefix = "deliver.prefix.";
* s2.External = &esmS2;
*
* sc.Name = "MyStream";
* sc.Subjects = subjects;
* sc.SubjectsLen = 2;
* sc.Retention = js_InterestPolicy;
* sc.Replicas = 3;
* sc.Placement = &p;
* sc.Mirror = &m;
* sc.Sources = sources;
* sc.SourcesLen = 2;
*
* // For RePublish subject:
* jsRePublish_Init(&rp);
* rp.Source = ">";
* rp.Destination = "RP.>";
* sc.RePublish = &rp;
*
* s = js_AddStream(&si, js, &sc, NULL, &jerr);
* \endcode
*/
typedef struct jsStreamConfig {
const char *Name;
const char *Description;
const char **Subjects;
int SubjectsLen;
jsRetentionPolicy Retention;
int64_t MaxConsumers;
int64_t MaxMsgs;
int64_t MaxBytes;
int64_t MaxAge;
int64_t MaxMsgsPerSubject;
int32_t MaxMsgSize;
jsDiscardPolicy Discard;
jsStorageType Storage;
int64_t Replicas;
bool NoAck;
const char *Template;
int64_t Duplicates;
jsPlacement *Placement;
jsStreamSource *Mirror;
jsStreamSource **Sources;
int SourcesLen;
bool Sealed; ///< Seal a stream so no messages can get our or in.
bool DenyDelete; ///< Restrict the ability to delete messages.
bool DenyPurge; ///< Restrict the ability to purge messages.
/// @brief Allow messages to be placed into the system and purge all
/// older messages using a special message header.
bool AllowRollup;
/// @brief Allow republish of the message after being sequenced and
/// stored.
jsRePublish *RePublish;
/// @brief Allow higher performance, direct access to get individual
/// messages. E.g. KeyValue
bool AllowDirect;
/// @brief Allow higher performance and unified direct access for
/// mirrors as well.
bool MirrorDirect;
/// @brief Allow KV like semantics to also discard new on a per subject
/// basis
bool DiscardNewPerSubject;
/// @brief A user-provided array of key/value pairs, encoded as a string
/// array [n1, v1, n2, v2, ...] representing key/value pairs {n1:v1,
/// n2:v2, ...}.
natsMetadata Metadata;
/// @brief js_StorageCompressionNone (default) or
/// js_StorageCompressionS2.
jsStorageCompression Compression;
/// @brief the starting sequence number for the stream.
uint64_t FirstSeq;
/// @brief Applies a subject transform (to matching messages) before
/// doing anything else when a new message is received.
jsSubjectTransformConfig SubjectTransform;
/// @brief Sets the limits on certain options on all consumers of the
/// stream.
jsStreamConsumerLimits ConsumerLimits;
} jsStreamConfig;
/**
* Information about messages that have been lost
*/
typedef struct jsLostStreamData
{
uint64_t *Msgs;
int MsgsLen;
uint64_t Bytes;
} jsLostStreamData;
/**
* This indicate that the given `Subject` in a stream contains `Msgs` messages.
*
* @see jsStreamStateSubjects
*/
typedef struct jsStreamStateSubject
{
const char *Subject;
uint64_t Msgs;
} jsStreamStateSubject;
/**
* List of subjects optionally returned in the stream information request.
*
* This structure indicates the number of elements in the list, that is,
* the list contains `Count` #jsStreamStateSubject elements.
*
* To get this list in #jsStreamState, you have to ask for it through #jsOptions.
*
* \code{.unparsed}
* jsStreamInfo *si = NULL;
* jsOptions o;
*
* jsOptions_Init(&o);
* o.Stream.Info.SubjectsFilter = "foo.>";
* s = js_GetStreamInfo(&si, js, "MY_STREAM", &o, &jerr);
*
* // handle errors and assume si->State.Subjects is not NULL
*
* for (i=0; i<si->State.Subjects->Count; i++)
* {
* jsStreamStateSubject *subj = &(si->State.Subjects->List[i]);
* printf("Subject=%s Messages count=%d\n", subj->Subject, (int) subj->Msgs);
* }
* \endcode
*
* @see jsStreamStateSubject
* @see js_GetStreamInfo
* @see jsOptions.Stream.Info.SubjectsFilter
*/
typedef struct jsStreamStateSubjects
{
jsStreamStateSubject *List;
int Count;
} jsStreamStateSubjects;
/**
* Information about the given stream
*
* \note `FirstTime` and `LastTime` are message timestamps expressed as the number
* of nanoseconds passed since 00:00:00 UTC Thursday, 1 January 1970.
*/
typedef struct jsStreamState
{
uint64_t Msgs;
uint64_t Bytes;
uint64_t FirstSeq;
int64_t FirstTime; ///< UTC time expressed as number of nanoseconds since epoch.
uint64_t LastSeq;
int64_t LastTime; ///< UTC time expressed as number of nanoseconds since epoch.
int64_t NumSubjects;
jsStreamStateSubjects *Subjects;
uint64_t NumDeleted;
uint64_t *Deleted;
int DeletedLen;
jsLostStreamData *Lost;
int64_t Consumers;
} jsStreamState;
/**
* Information about all the peers in the cluster that
* are supporting the stream or consumer.
*/
typedef struct jsPeerInfo
{
char *Name;
bool Current;
bool Offline;
int64_t Active;
uint64_t Lag;
} jsPeerInfo;
/**
* Information about the underlying set of servers
* that make up the stream or consumer.
*/
typedef struct jsClusterInfo
{
char *Name;
char *Leader;
jsPeerInfo **Replicas;
int ReplicasLen;
} jsClusterInfo;
/**
* Information about an upstream stream source.
*/
typedef struct jsStreamSourceInfo
{
char *Name;
jsExternalStream *External;
uint64_t Lag;
int64_t Active;
const char * FilterSubject;
jsSubjectTransformConfig *SubjectTransforms;
int SubjectTransformsLen;
} jsStreamSourceInfo;
/**
* Information about an alternate stream represented by a mirror.
*/
typedef struct jsStreamAlternate
{
const char *Name;
const char *Domain;
const char *Cluster;
} jsStreamAlternate;
/**
* Configuration and current state for this stream.
*
* \note `Created` is the timestamp when the stream was created, expressed as
* the number of nanoseconds passed since 00:00:00 UTC Thursday, 1 January 1970.
*/
typedef struct jsStreamInfo
{
jsStreamConfig *Config;
int64_t Created; ///< UTC time expressed as number of nanoseconds since epoch.
jsStreamState State;
jsClusterInfo *Cluster;
jsStreamSourceInfo *Mirror;
jsStreamSourceInfo **Sources;
int SourcesLen;
jsStreamAlternate **Alternates;
int AlternatesLen;
} jsStreamInfo;
/**
* List of stream information objects returned by #js_Streams
*
* \note Once done, the list should be destroyed calling #jsStreamInfoList_Destroy
*
* @see jsStreamInfoList_Destroy
*/
typedef struct jsStreamInfoList
{
jsStreamInfo **List;
int Count;
} jsStreamInfoList;
/**
* List of stream names returned by #js_StreamNames
*
* \note Once done, the list should be destroyed calling #jsStreamNamesList_Destroy
*
* @see jsStreamNamesList_Destroy
*/
typedef struct jsStreamNamesList
{
char **List;
int Count;
} jsStreamNamesList;
/**
* Configuration of a JetStream consumer.
*
* In order to add a consumer, a configuration needs to be set.
* The typical usage would be to initialize all required objects on the stack
* and configure them, then pass the pointer to the configuration to
* #js_AddConsumer.
*
* \note `OptStartTime` needs to be expressed as the number of nanoseconds
* passed since 00:00:00 UTC Thursday, 1 January 1970.
*
* \note The strings are applications owned and will not be freed by the library.
*
* \note `SampleFrequency` is a sampling value, represented as a string such as "50"
* for 50%, that causes the server to produce advisories for consumer ack metrics.
*
* \note `Durable` cannot contain the character ".".
*
* \note `HeadersOnly` means that the subscription will not receive any message payload,
* instead, it will receive only messages headers (if present) with the addition of
* the header #JSMsgSize ("Nats-Msg-Size"), whose value is the payload size.
*
* \note NATS server 2.10 added FilterSubjects, an array of multiple filter
* subjects. It is mutually exclusive with the previously available single
* FilterSubject.
*
* \note NATS server 2.10 added consumer Metadata which contains user-provided
* string name/value pairs.
*
* @see jsConsumerConfig_Init
*
* \code{.unparsed}
* jsConsumerInfo *ci = NULL;
* jsConsumerConfig cc;
*
* jsConsumerConfig_Init(&cc);
* cc.Durable = "MY_DURABLE";
* cc.DeliverSubject = "foo";
* cc.DeliverPolicy = js_DeliverNew;
*
* s = js_AddConsumer(&ci, js, &cc, NULL, &jerr);
* \endcode
*/
typedef struct jsConsumerConfig
{
const char *Name;
const char *Durable;
const char *Description;
jsDeliverPolicy DeliverPolicy;
uint64_t OptStartSeq;
int64_t OptStartTime; ///< UTC time expressed as number of nanoseconds since epoch.
jsAckPolicy AckPolicy;
int64_t AckWait;
int64_t MaxDeliver;
int64_t *BackOff; ///< Redelivery durations expressed in nanoseconds
int BackOffLen;
const char *FilterSubject;
jsReplayPolicy ReplayPolicy;
uint64_t RateLimit;
const char *SampleFrequency;
int64_t MaxWaiting;
int64_t MaxAckPending;
bool FlowControl;
int64_t Heartbeat; ///< Heartbeat interval expressed in number of nanoseconds.
bool HeadersOnly;
// Pull based options.
int64_t MaxRequestBatch; ///< Maximum Pull Consumer request batch size.
int64_t MaxRequestExpires; ///< Maximum Pull Consumer request expiration, expressed in number of nanoseconds.
int64_t MaxRequestMaxBytes; ///< Maximum Pull Consumer request maximum bytes.
// Push based options.
const char *DeliverSubject;
const char *DeliverGroup;
// Ephemeral inactivity threshold.
int64_t InactiveThreshold; ///< How long the server keeps an ephemeral after detecting loss of interest, expressed in number of nanoseconds.
// Generally inherited by parent stream and other markers, now can be configured directly.
int64_t Replicas;
// Force memory storage.
bool MemoryStorage;
// Configuration options introduced in 2.10
const char **FilterSubjects; ///< Multiple filter subjects
int FilterSubjectsLen;
natsMetadata Metadata; ///< User-provided metadata for the consumer, encoded as an array of {"key", "value",...}
// Configuration options introduced in 2.11
int64_t PauseUntil; ///< Suspends the consumer until this deadline, represented as number of nanoseconds since epoch.
} jsConsumerConfig;
/**
* This represents a consumer sequence mismatch between the server and client
* views.
*
* This can help applications find out if messages have been missed. Without
* this and during a disconnect, it would be possible that a subscription
* is not aware that it missed messages from the server. When acknowledgment
* mode is other than #js_AckNone, messages would ultimately be redelivered,
* but for #js_AckNone, they would not. But even with an acknowledgment mode
* this may help finding sooner that something went wrong and let the application
* decide if it wants to recreate the subscription starting at a given
* sequence.
*
* The gap of missing messages could be calculated as `ConsumerServer-ConsumerClient`.
*
* @see natsSubscription_GetSequenceMismatch
*/
typedef struct jsConsumerSequenceMismatch
{
uint64_t Stream; ///< This is the stream sequence that the application should resume from.
uint64_t ConsumerClient; ///< This is the consumer sequence that was last received by the library.
uint64_t ConsumerServer; ///< This is the consumer sequence last sent by the server.
} jsConsumerSequenceMismatch;
/**
* JetStream subscribe options.
*
* These are options that you can provide to JetStream subscribe APIs.
*
* The common usage will be to initialize a structure on the stack by
* calling #jsSubOptions_Init. Note that strings are owned by
* the application and need to be valid for the duration of the API
* call this object is passed to.
*
* \note It is the user responsibility to free the strings if they
* have been allocated.
*
* @see jsSubOptions_Init
*/
typedef struct jsSubOptions
{
/**
* If specified, the library will only bind to this stream,
* otherwise, the library communicates with the server to
* get the stream name that has the matching subject given
* to the #js_Subscribe family calls.
*/
const char *Stream; ///< If specified, the consumer will be bound to this stream name.
/**
* If specified, the #js_Subscribe family calls will only
* attempt to create a subscription for this matching consumer.
*
* That is, the consumer should exist prior to the call,
* either created by the application calling #js_AddConsumer
* or it should have been created with some other tools
* such as the NATS cli.
*/
const char *Consumer; ///< If specified, the subscription will be bound to an existing consumer from the `Stream` without attempting to create.
/**
* If specified, the low level NATS subscription will be a
* queue subscription, which means that the load on the
* delivery subject will be balanced across multiple members
* of the same queue group.
*
* This makes sense only if the delivery subject in the
* `Config` field of #jsSubOptions is the same for the
* members of the same group.
*
* When no `Durable` name is specified in the `Config` block, then the
* queue name will be used as the consumer's durable name. In this case,
* the queue name cannot contain the character ".".
*/
const char *Queue; ///< Queue name for queue subscriptions.
/**
* This has meaning only for asynchronous subscriptions,
* and only if the consumer's acknowledgment mode is
* other than #js_AckNone.
*
* For asynchronous subscriptions, the default behavior
* is for the library to acknowledge the message once
* the user callback returns.
*
* This option allows you to take control of when the
* message should be acknowledged.
*/
bool ManualAck; ///< If true, the user will have to acknowledge the messages.
/**
* This allows the user to fully configure the JetStream
* consumer.
*/
jsConsumerConfig Config; ///< Consumer configuration.
/**
* This will create a fifo ephemeral consumer for in order delivery of
* messages. There are no redeliveries and no acks.
* Flow control and heartbeats are required and set by default, but
* the heartbeats value can be overridden in the consumer configuration.
*/
bool Ordered; ///< If true, this will be an ordered consumer.
} jsSubOptions;
/**
* Includes the consumer and stream sequence info from a JetStream consumer.
*/
typedef struct jsSequencePair
{
uint64_t Consumer;
uint64_t Stream;
} jsSequencePair;
/**
* Has both the consumer and the stream sequence and last activity.
*/
typedef struct jsSequenceInfo
{