-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnats.core.pas
6376 lines (5952 loc) · 260 KB
/
nats.core.pas
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
unit nats.core;
{$IFDEF FPC}
{$mode delphi}
{$ENDIF}
{$DEFINE USE_STATIC_LIB}
{$DEFINE NATS_HAS_STREAMING}
interface
// MIT License
//
// Copyright (c) 2022 Vahid Nasehi Oskouei
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
{$IFDEF USE_STATIC_LIB}
{$linklib 'libnats_static.a'}
{$linklib 'libc'}
{$linklib 'libcrypto'}
{$linklib 'libssl'}
{$linklib 'libpthread'}
{$IFDEF NATS_HAS_STREAMING}
{$linklib 'libprotobuf-c.a'}
{$ENDIF}
{$ENDIF}
uses
nats.status, nats.version;
const
{$IFDEF USE_STATIC_LIB}
LIB_NATS = '';
{$ELSE}
LIB_NATS = 'libnats.so';
{$ENDIF}
type
size_t = UInt64;
(** \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.
**)
natsSock = type Integer;
(*! \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)
*)
const
(** \brief The default `NATS Server` URL.
*
* This is the default URL a `NATS Server`, running with default listen
* port, can be reached at.
**)
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
**)
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.
**)
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
**)
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
**)
JSMsgRollupAll = 'all';
type
(** \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.
**)
PnatsConnection = ^natsConnection;
natsConnection = record
end;
(** \brief Statistics of a #natsConnection
*
* Tracks various statistics received and sent on a connection,
* including counts for messages and bytes.
**)
PnatsStatistics = ^natsStatistics;
natsStatistics = record
end;
(** \brief Interest on a given subject.
*
* A #natsSubscription represents interest in a given subject.
**)
PnatsSubscription = ^natsSubscription;
natsSubscription = record
end;
(** \brief A structure holding a subject, optional reply and payload.
*
* #natsMsg is a structure used by Subscribers and
* #natsConnection_PublishMsg().
**)
PPnatsMsg = ^PnatsMsg;
PnatsMsg = ^natsMsg;
natsMsg = record
end;
(** \brief Way to configure a #natsConnection.
*
* Options can be used to create a customized #natsConnection.
**)
PnatsOptions = ^natsOptions;
natsOptions = record
end;
(** \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.
**)
PnatsInbox = ^natsInbox;
natsInbox = type AnsiChar;
(** \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
**)
PnatsMsgList = ^natsMsgList;
natsMsgList = record
MsgsL: PPnatsMsg;
Count: Integer;
end;
(**
* 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.
**)
PjsCtx = ^jsCtx;
jsCtx = record
end;
(**
* 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
**)
PjsPubOptions = ^jsPubOptions;
jsPubOptions = record
MaxWait: Int64; ///< Amount of time (in milliseconds) to wait for a publish response, default will the context's Wait value.
MsgId: PAnsiChar; ///< Message ID used for de-duplication.
ExpectStream: PAnsiChar; ///< Expected stream to respond from the publish call.
ExpectLastMsgId: PAnsiChar; ///< Expected last message ID in the stream.
ExpectLastSeq: UInt64; ///< Expected last message sequence in the stream.
ExpectLastSubjectSeq: UInt64; ///< Expected last message sequence for the subject in the stream.
ExpectNoMessage: Boolean; ///< Expected no message (that is, sequence == 0) for the subject in the stream.
end;
(**
* Determines how messages in a set are retained.
**)
jsRetentionPolicy = (
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.
);
(**
* Determines how to proceed when limits of messages or bytes are reached.
**)
jsDiscardPolicy = (
js_DiscardOld = 0, ///< Will remove older messages to return to the limits. This is the default.
js_DiscardNew ///< Will fail to store new messages.
);
(**
* Determines how messages are stored for retention.
**)
jsStorageType = (
js_FileStorage = 0, ///< Specifies on disk storage. It's the default.
js_MemoryStorage ///< Specifies in memory only.
);
(**
* Determines how the consumer should select the first message to deliver.
**)
jsDeliverPolicy = (
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.
);
(**
* Determines how the consumer should acknowledge delivered messages.
**)
jsAckPolicy = (
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.
);
(**
* Determines how the consumer should replay messages it already has queued in the stream.
**)
jsReplayPolicy = (
js_ReplayInstant = 0, ///< Replays messages as fast as possible.
js_ReplayOriginal ///< Maintains the same timing as the messages were received.
);
(**
* 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
**)
PjsPlacement = ^jsPlacement;
jsPlacement = record
Cluster: PAnsiChar;
Tags: PPAnsiChar;
TagsLen: Integer;
end;
(**
* 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.
**)
PjsExternalStream = ^jsExternalStream;
jsExternalStream = record
APIPrefix: PAnsiChar;
DeliverPrefix: PAnsiChar;
end;
(**
* 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.
**)
PPjsStreamSource = ^PjsStreamSource;
PjsStreamSource = ^jsStreamSource;
jsStreamSource = record
Name: PAnsiChar;
OptStartSeq: UInt64;
OptStartTime: Int64; ///< UTC time expressed as number of nanoseconds since epoch.
FilterSubject: PAnsiChar;
_External: PjsExternalStream;
end;
(**
* 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.
*
* @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};
*
* 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;
*
* s = js_AddStream(&si, js, &sc, NULL, &jerr);
* \endcode
**)
PjsStreamConfig = ^jsStreamConfig;
jsStreamConfig = record
Name: PAnsiChar;
Description: PAnsiChar;
Subjects: PPAnsiChar;
SubjectsLen: Integer;
Retention: jsRetentionPolicy;
MaxConsumers: Int64;
MaxMsgs: Int64;
MaxBytes: Int64;
MaxAge: Int64;
MaxMsgsPerSubject: Int64;
MaxMsgSize: Integer;
Discard: jsDiscardPolicy;
Storage: jsStorageType;
Replicas: Int64;
NoAck: Boolean;
Template: PAnsiChar;
Duplicates: Int64;
Placement: PjsPlacement;
Mirror: PjsStreamSource;
Sources: PPjsStreamSource;
SourcesLen: Integer;
Sealed: Boolean; ///< Seal a stream so no messages can get our or in.
DenyDelete: Boolean; ///< Restrict the ability to delete messages.
DenyPurge: Boolean; ///< Restrict the ability to purge messages.
(**
* Allows messages to be placed into the system and purge
* all older messages using a special message header.
**)
AllowRollup: Boolean;
end;
(**
* Information about messages that have been lost
**)
PjsLostStreamData = ^jsLostStreamData;
jsLostStreamData = record
Msgs: PUInt64;
MsgsLen: Integer;
Bytes: UInt64;
end;
(**
* This indicate that the given `Subject` in a stream contains `Msgs` messages.
*
* @see jsStreamStateSubjects
**)
PjsStreamStateSubject = ^jsStreamStateSubject;
jsStreamStateSubject = record
Subject: PAnsiChar;
Msgs: UInt64;
end;
(**
* 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
**)
PjsStreamStateSubjects = ^jsStreamStateSubjects;
jsStreamStateSubjects = record
List: PjsStreamStateSubject;
Count: Integer;
end;
(**
* 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.
**)
jsStreamState = record
Msgs: UInt64;
Bytes: UInt64;
FirstSeq: UInt64;
FirstTime: Int64; ///< UTC time expressed as number of nanoseconds since epoch.
LastSeq: UInt64;
LastTime: Int64; ///< UTC time expressed as number of nanoseconds since epoch.
NumSubjects: Int64;
Subjects: PjsStreamStateSubjects;
NumDeleted: UInt64;
Deleted: PUInt64;
DeletedLen: Integer;
Lost: PjsLostStreamData;
Consumers: Int64;
end;
(**
* Information about all the peers in the cluster that
* are supporting the stream or consumer.
**)
PPjsPeerInfo = ^PjsPeerInfo;
PjsPeerInfo = ^jsPeerInfo;
jsPeerInfo = record
Name: PAnsiChar;
Current: Boolean;
Offline: Boolean;
Active: Int64;
Lag: UInt64;
end;
(**
* Information about the underlying set of servers
* that make up the stream or consumer.
**)
PjsClusterInfo = ^jsClusterInfo;
jsClusterInfo = record
Name: PAnsiChar;
Leader: PAnsiChar;
Replicas: PPjsPeerInfo;
ReplicasLen: Integer;
end;
(**
* Information about an upstream stream source.
**)
PPjsStreamSourceInfo = ^PjsStreamSourceInfo;
PjsStreamSourceInfo = ^jsStreamSourceInfo;
jsStreamSourceInfo = record
Name: PAnsiChar;
_External: PjsExternalStream;
Lag: UInt64;
Active: Int64;
end;
(**
* 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.
**)
PjsStreamInfo = ^jsStreamInfo;
jsStreamInfo = record
Config: PjsStreamConfig;
Created: Int64; ///< UTC time expressed as number of nanoseconds since epoch.
State: jsStreamState;
Cluster: PjsClusterInfo;
Mirror: PjsStreamSourceInfo;
Sources: PPjsStreamSourceInfo;
SourcesLen: Integer;
end;
(**
* 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.
*
* @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
**)
PjsConsumerConfig = ^jsConsumerConfig;
jsConsumerConfig = record
Durable: PAnsiChar;
Description: PAnsiChar;
DeliverSubject: PAnsiChar;
DeliverGroup: PAnsiChar;
DeliverPolicy: jsDeliverPolicy;
OptStartSeq: UInt64;
OptStartTime: Int64; ///< UTC time expressed as number of nanoseconds since epoch.
AckPolicy: jsAckPolicy;
AckWait: Int64;
MaxDeliver: Int64;
BackOff: PInt64; ///< Redelivery durations expressed in nanoseconds
BackOffLen: Integer;
FilterSubject: PAnsiChar;
ReplayPolicy: jsReplayPolicy;
RateLimit: UInt64;
SampleFrequency: PAnsiChar;
MaxWaiting: Int64;
MaxAckPending: Int64;
FlowControl: Boolean;
Heartbeat: Int64; ///< Heartbeat interval expressed in number of nanoseconds.
HeadersOnly: Boolean;
// Pull based options.
MaxRequestBatch: Int64;
MaxRequestExpires: Int64; ///< Maximum Pull Consumer request expiration, expressed in number of nanoseconds.
// Ephemeral inactivity threshold.
InactiveThreshold: Int64; ///< How long the server keeps an ephemeral after detecting loss of interest, expressed in number of nanoseconds.
end;
(**
* 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
**)
jsConsumerSequenceMismatch = record
Stream: UInt64; ///< This is the stream sequence that the application should resume from.
ConsumerClient: UInt64; ///< This is the consumer sequence that was last received by the library.
ConsumerServer: UInt64; ///< This is the consumer sequence last sent by the server.
end;
(**
* 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
**)
PjsSubOptions = ^jsSubOptions;
jsSubOptions = record
(**
* 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.
**)
Stream: PAnsiChar; ///< 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.
**)
Consumer: PAnsiChar; ///< 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 ".".
**)
Queue: PAnsiChar; ///< 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.
**)
ManualAck: Boolean; ///< If true, the user will have to acknowledge the messages.
(**
* This allows the user to fully configure the JetStream
* consumer.
**)
Config: jsConsumerConfig; ///< 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.
**)
Ordered: Boolean; ///< If true, this will be an ordered consumer.
end;
(**
* Includes the consumer and stream sequence info from a JetStream consumer.
**)
jsSequencePair = record
Consumer: UInt64;
Stream: UInt64;
end;
(**
* Has both the consumer and the stream sequence and last activity.
**)
jsSequenceInfo = record
Consumer: UInt64;
Stream: UInt64;
Last: Int64; ///< UTC time expressed as number of nanoseconds since epoch.
end;
(**
* Configuration and current state for this consumer.
*
* \note `Created` is the timestamp when the consumer was created, expressed as the number
* of nanoseconds passed since 00:00:00 UTC Thursday, 1 January 1970.
**)
PjsConsumerInfo = ^jsConsumerInfo;
jsConsumerInfo = record
Stream: PAnsiChar;
Name: PAnsiChar;
Created: Int64; ///< UTC time expressed as number of nanoseconds since epoch.
Config: PjsConsumerConfig;
Delivered: jsSequenceInfo;
AckFloor: jsSequenceInfo;
NumAckPending: Int64;
NumRedelivered: Int64;
NumWaiting: Int64;
NumPending: UInt64;
Cluster: PjsClusterInfo;
PushBound: Boolean;
end;
(**
* Reports on API calls to JetStream for this account.
**)
jsAPIStats = record
Total: UInt64;
Errors: UInt64;
end;
(**
* Includes the JetStream limits of the current account.
**)
jsAccountLimits = record
MaxMemory: Int64;
MaxStore: Int64;
MaxStreams: Int64;
MaxConsumers: Int64;
end;
(**
* Information about the JetStream usage from the current account.
**)
PjsAccountInfo = ^jsAccountInfo;
jsAccountInfo = record
Memory: UInt64;
Store: UInt64;
Streams: Int64;
Consumers: Int64;
Domain: PAnsiChar;
API: jsAPIStats;
Limits: jsAccountLimits;
end;
(**
* This represents the JetStream metadata associated with received messages.
*
* @see natsMsg_GetMetaData
* @see jsMsgMetaData_Destroy
*
**)
PjsMsgMetaData = ^jsMsgMetaData;
jsMsgMetaData = record
Sequence: jsSequencePair;
NumDelivered: UInt64;
NumPending: UInt64;
Timestamp: Int64;
Stream: PAnsiChar;
Consumer: PAnsiChar;
Domain: PAnsiChar;
end;
(**
* Ack received after successfully publishing a message.
**)
PjsPubAck = ^jsPubAck;
jsPubAck = record
Stream: PAnsiChar;
Sequence: UInt64;
Domain: PAnsiChar;
Duplicate: Boolean;
end;
(**
* Publish acknowledgment failure that will be passed to the optional
* #jsPubAckErrHandler callback.
**)
PjsPubAckErr = ^jsPubAckErr;
jsPubAckErr = record
Msg: PnatsMsg;
Err: natsStatus;
ErrCode: jsErrCode;
ErrText: PAnsiChar;
end;
(** \brief Callback used to process asynchronous publish errors from JetStream.
*
* Callback used to process asynchronous publish errors from JetStream #js_PublishAsync
* and #js_PublishMsgAsync calls. The provided #jsPubAckErr object gives the user
* access to the encountered error along with the original message sent to the server
* for possible restransmitting.
*
* \note If the message is resent, the library will not destroy the original
* message and once again take ownership of it. To resend the message, do the
* following so that the library knows not to destroy the message (since the
* call will clear the `Msg` field from the #jsPubAckErr object).
*
* \code{.unparsed}
* void myPAECallback(jsCtx *js, jsPubAckErr *pae, void* closure)
* {
* ...
* // Resend the message
* js_PublishMsgAsync(js, &(pae->Msg), NULL);
* }
* \endcode
*
* \warning The #jsPubAckErr object and its content will be invalid as
* soon as the callback returns.
*
* \warning Unlike a NATS message callback, the user does not have to destroy
* the original NATS message (present in the #jsPubAckErr object), the
* library will do it.
*
* @param js the pointer to the #jsCtx object.
* @param pae the pointer to the #jsPubAckErr object.
* @param closure an optional pointer to a user defined object that was specified when
* registering the callback.
**)
jsPubAckErrHandler = procedure(js: PjsCtx; pae: PjsPubAckErr; closure: Pointer); cdecl;
(**
* JetStream context options.
*
* Initialize the object with #jsOptions_Init.
**)
PjsOptions = ^jsOptions;
jsOptions = record
Prefix: PAnsiChar; ///< JetStream prefix, default is "$JS.API"
Domain: PAnsiChar; ///< Domain changes the domain part of JetSteam API prefix.
Wait: Int64; ///< Amount of time (in milliseconds) to wait for various JetStream API requests, default is 5000 ms (5 seconds).
(**
* Publish Async options
**)
type
jsOptionsPublishAsync = record
MaxPending: Int64; ///< Maximum outstanding asynchronous publishes that can be inflight at one time.
ErrHandler: jsPubAckErrHandler; ///< Callback invoked when error encountered publishing a given message.
ErrHandlerClosure: Pointer; ///< Closure (or user data) passed to #jsPubAckErrHandler callback.
StallWait: Int64; ///< Amount of time (in milliseconds) to wait in a PublishAsync call when there is MaxPending inflight messages, default is 200 ms.
end;
var
PublishAsync: jsOptionsPublishAsync;
(**
* Advanced stream options
*
* * `Purge` for advanced purge options.
* * `Info` for advanced information retrieval options.
**)
type
jsOptionsStream = record
(**
* Advanced stream purge options
*
* * `Subject` will filter the purge request to only messages that match the subject, which can have wildcards.<br>
* * `Sequence` will purge up to but not including this sequence and can be combined with subject filtering.<br>
* * `Keep` will specify how many messages to keep and can be combined with subject filtering.<br>
*
* \note `Sequence` and `Keep` are mutually exclusive, so both can not be set at the same time.
**)
type
jsOptionsStreamPurge = record
Subject: PAnsiChar; ///< This is the subject to match against messages for the purge command.
Sequence: UInt64; ///< Purge up to but not including sequence.
Keep: UInt64; ///< Number of messages to keep.
end;
var
Purge: jsOptionsStreamPurge; ///< Optional stream purge options.
(**
* Advance stream information retrieval options
**)
type
jsOptionsStreamInfo = record
DeletedDetails: Boolean; ///< Get the list of deleted message sequences.
SubjectsFilter: PAnsiChar; ///< Get the list of subjects in this stream.
end;
var
Info: jsOptionsStreamInfo; ///< Optional stream information retrieval options.
end;
var
Stream: jsOptionsStream; ///< Optional stream options.
end;
(**