This repository was archived by the owner on Jan 4, 2025. It is now read-only.
forked from kurrent-io/EventStore-Client-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
796 lines (682 loc) · 27.7 KB
/
client.go
File metadata and controls
796 lines (682 loc) · 27.7 KB
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
// Package esdb EventStoreDB gRPC client.
package esdb
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"github.com/EventStore/EventStore-Client-Go/v4/protos/gossip"
persistentProto "github.com/EventStore/EventStore-Client-Go/v4/protos/persistent"
"github.com/EventStore/EventStore-Client-Go/v4/protos/shared"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
api "github.com/EventStore/EventStore-Client-Go/v4/protos/streams"
)
// Client Represents a client to a single node. A client instance maintains a full duplex communication to EventStoreDB.
// Many threads can use an EventStoreDB client at the same time or a single thread can make many asynchronous requests.
type Client struct {
grpcClient *grpcClient
config *Configuration
}
// NewClient Creates a gRPC client to an EventStoreDB database.
func NewClient(configuration *Configuration) (*Client, error) {
if err := configuration.Validate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
grpcClient := newGrpcClient(*configuration)
return &Client{
grpcClient: grpcClient,
config: configuration,
}, nil
}
// Close Closes a connection and cleans all its allocated resources.
func (client *Client) Close() error {
client.grpcClient.close()
return nil
}
// AppendToStream Appends events to a given stream.
func (client *Client) AppendToStream(
context context.Context,
streamID string,
opts AppendToStreamOptions,
events ...EventData,
) (*WriteResult, error) {
opts.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
streamsClient := api.NewStreamsClient(handle.Connection())
var headers, trailers metadata.MD
callOptions := []grpc.CallOption{grpc.Header(&headers), grpc.Trailer(&trailers)}
callOptions, ctx, cancel := configureGrpcCall(context, client.config, &opts, callOptions, client.grpcClient.perRPCCredentials)
defer cancel()
appendOperation, err := streamsClient.Append(ctx, callOptions...)
if err != nil {
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("could not construct append operation. Reason: %w", err)
}
header := toAppendHeader(streamID, opts.ExpectedRevision)
if err := appendOperation.Send(header); err != nil {
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("could not send append request header. Reason: %w", err)
}
for _, event := range events {
appendRequest := &api.AppendReq{
Content: &api.AppendReq_ProposedMessage_{
ProposedMessage: toProposedMessage(event),
},
}
if err = appendOperation.Send(appendRequest); err != nil {
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("could not send append request. Reason: %w", err)
}
}
response, err := appendOperation.CloseAndRecv()
if err != nil {
return nil, client.grpcClient.handleError(handle, trailers, err)
}
result := response.GetResult()
switch result.(type) {
case *api.AppendResp_Success_:
{
success := result.(*api.AppendResp_Success_)
var streamRevision uint64
if _, ok := success.Success.GetCurrentRevisionOption().(*api.AppendResp_Success_NoStream); ok {
streamRevision = 1
} else {
streamRevision = success.Success.GetCurrentRevision()
}
var commitPosition uint64
var preparePosition uint64
if position, ok := success.Success.GetPositionOption().(*api.AppendResp_Success_Position); ok {
commitPosition = position.Position.CommitPosition
preparePosition = position.Position.PreparePosition
} else {
streamRevision = success.Success.GetCurrentRevision()
}
return &WriteResult{
CommitPosition: commitPosition,
PreparePosition: preparePosition,
NextExpectedVersion: streamRevision,
}, nil
}
case *api.AppendResp_WrongExpectedVersion_:
{
wrong := result.(*api.AppendResp_WrongExpectedVersion_).WrongExpectedVersion
expected := ""
current := ""
if wrong.GetExpectedAny() != nil {
expected = "any"
} else if wrong.GetExpectedNoStream() != nil {
expected = "no_stream"
} else if wrong.GetExpectedStreamExists() != nil {
expected = "stream_exists"
} else {
expected = strconv.Itoa(int(wrong.GetExpectedRevision()))
}
if wrong.GetCurrentNoStream() != nil {
current = "no_stream"
} else {
current = strconv.Itoa(int(wrong.GetCurrentRevision()))
}
return nil, &Error{code: ErrorCodeWrongExpectedVersion, err: fmt.Errorf("wrong expected version: expecting '%s' but got '%s'", expected, current)}
}
}
return &WriteResult{
CommitPosition: 0,
PreparePosition: 0,
NextExpectedVersion: 1,
}, nil
}
// SetStreamMetadata Sets the metadata for a stream.
func (client *Client) SetStreamMetadata(
context context.Context,
streamID string,
opts AppendToStreamOptions,
metadata StreamMetadata,
) (*WriteResult, error) {
streamName := fmt.Sprintf("$$%v", streamID)
props, err := metadata.toMap()
if err != nil {
return nil, fmt.Errorf("error when serializing stream metadata: %w", err)
}
data, err := json.Marshal(props)
if err != nil {
return nil, fmt.Errorf("error when serializing stream metadata: %w", err)
}
result, err := client.AppendToStream(context, streamName, opts, EventData{
ContentType: ContentTypeJson,
EventType: "$metadata",
Data: data,
})
if err != nil {
return nil, err
}
return result, nil
}
// GetStreamMetadata Reads the metadata for a stream.
func (client *Client) GetStreamMetadata(
context context.Context,
streamID string,
opts ReadStreamOptions,
) (*StreamMetadata, error) {
streamName := fmt.Sprintf("$$%v", streamID)
stream, err := client.ReadStream(context, streamName, opts, 1)
if esdbErr, ok := FromError(err); !ok {
return nil, esdbErr
}
defer stream.Close()
event, err := stream.Recv()
if errors.Is(err, io.EOF) {
return &StreamMetadata{}, nil
}
if err != nil {
return nil, fmt.Errorf("unexpected error when reading stream metadata: %w", err)
}
var props map[string]interface{}
err = json.Unmarshal(event.OriginalEvent().Data, &props)
if err != nil {
return nil, &Error{code: ErrorCodeParsing, err: fmt.Errorf("error when deserializing stream metadata json: %w", err)}
}
meta, err := streamMetadataFromMap(props)
if err != nil {
return nil, &Error{code: ErrorCodeParsing, err: fmt.Errorf("error when parsing stream metadata json: %w", err)}
}
return meta, nil
}
// DeleteStream Deletes a given stream.
//
// Makes use of "Truncate Before". When a stream is deleted, it's "Truncate Before" is set to the stream's current last
// event number. When a deleted stream is read, the read will return a stream not found error. After deleting
// the stream, you are able to write to it again, continuing from where it left off.
func (client *Client) DeleteStream(
parent context.Context,
streamID string,
opts DeleteStreamOptions,
) (*DeleteResult, error) {
opts.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
streamsClient := api.NewStreamsClient(handle.Connection())
var headers, trailers metadata.MD
callOptions := []grpc.CallOption{grpc.Header(&headers), grpc.Trailer(&trailers)}
callOptions, ctx, cancel := configureGrpcCall(parent, client.config, &opts, callOptions, client.grpcClient.perRPCCredentials)
defer cancel()
deleteRequest := toDeleteRequest(streamID, opts.ExpectedRevision)
deleteResponse, err := streamsClient.Delete(ctx, deleteRequest, callOptions...)
if err != nil {
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("failed to perform delete, details: %w", err)
}
return &DeleteResult{Position: deletePositionFromProto(deleteResponse)}, nil
}
// TombstoneStream Permanently deletes a given stream by writing a Tombstone event to the end of the stream.
//
// A Tombstone event is written to the end of the stream, permanently deleting it. The stream cannot be recreated or
// written to again. Tombstone events are written with the event's type "$streamDeleted". When a tombstoned stream
// is read, the read will return a stream deleted error.
func (client *Client) TombstoneStream(
parent context.Context,
streamID string,
opts TombstoneStreamOptions,
) (*DeleteResult, error) {
opts.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
streamsClient := api.NewStreamsClient(handle.Connection())
var headers, trailers metadata.MD
callOptions := []grpc.CallOption{grpc.Header(&headers), grpc.Trailer(&trailers)}
callOptions, ctx, cancel := configureGrpcCall(parent, client.config, &opts, callOptions, client.grpcClient.perRPCCredentials)
defer cancel()
tombstoneRequest := toTombstoneRequest(streamID, opts.ExpectedRevision)
tombstoneResponse, err := streamsClient.Tombstone(ctx, tombstoneRequest, callOptions...)
if err != nil {
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("failed to perform delete, details: %w", err)
}
return &DeleteResult{Position: tombstonePositionFromProto(tombstoneResponse)}, nil
}
// ReadStream Reads events from a given stream. The reading can be done forward and backward.
func (client *Client) ReadStream(
context context.Context,
streamID string,
opts ReadStreamOptions,
count uint64,
) (*ReadStream, error) {
opts.setDefaults()
readRequest := toReadStreamRequest(streamID, opts.Direction, opts.From, count, opts.ResolveLinkTos)
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
streamsClient := api.NewStreamsClient(handle.Connection())
return readInternal(context, client, &opts, handle, streamsClient, readRequest)
}
// ReadAll Reads events from the $all stream. The reading can be done forward and backward.
func (client *Client) ReadAll(
context context.Context,
opts ReadAllOptions,
count uint64,
) (*ReadStream, error) {
opts.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
streamsClient := api.NewStreamsClient(handle.Connection())
readRequest := toReadAllRequest(opts.Direction, opts.From, count, opts.ResolveLinkTos)
return readInternal(context, client, &opts, handle, streamsClient, readRequest)
}
// SubscribeToStream allows you to subscribe to a stream and receive notifications about new events added to the stream.
// The subscription will notify event from the starting point onward. If events already exist, the handler will be
// called for each event one by one until it reaches the end of the stream. From there, the server will notify the
// handler whenever a new event appears.
func (client *Client) SubscribeToStream(
parent context.Context,
streamID string,
opts SubscribeToStreamOptions,
) (*Subscription, error) {
opts.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
var headers, trailers metadata.MD
callOptions := []grpc.CallOption{grpc.Header(&headers), grpc.Trailer(&trailers)}
callOptions, ctx, cancel := configureGrpcCall(parent, client.config, &opts, callOptions, client.grpcClient.perRPCCredentials)
streamsClient := api.NewStreamsClient(handle.Connection())
subscriptionRequest, err := toStreamSubscriptionRequest(streamID, opts.From, opts.ResolveLinkTos, nil)
if err != nil {
return nil, fmt.Errorf("failed to construct subscription. Reason: %w", err)
}
readClient, err := streamsClient.Read(ctx, subscriptionRequest, callOptions...)
if err != nil {
defer cancel()
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("failed to construct subscription. Reason: %w", err)
}
readResult, err := readClient.Recv()
if err != nil {
defer cancel()
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("failed to perform read. Reason: %w", err)
}
switch readResult.Content.(type) {
case *api.ReadResp_Confirmation:
{
confirmation := readResult.GetConfirmation()
return newSubscription(client, cancel, readClient, confirmation.SubscriptionId), nil
}
}
defer cancel()
return nil, fmt.Errorf("failed to initiate subscription")
}
// SubscribeToAll allows you to subscribe to $all stream and receive notifications about new events added to the stream.
// The subscription will notify event from the starting point onward. If events already exist, the handler will be
// called for each event one by one until it reaches the end of the stream. From there, the server will notify the
// handler whenever a new event appears.
func (client *Client) SubscribeToAll(
parent context.Context,
opts SubscribeToAllOptions,
) (*Subscription, error) {
opts.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
streamsClient := api.NewStreamsClient(handle.Connection())
var headers, trailers metadata.MD
callOptions := []grpc.CallOption{grpc.Header(&headers), grpc.Trailer(&trailers)}
callOptions, ctx, cancel := configureGrpcCall(parent, client.config, &opts, callOptions, client.grpcClient.perRPCCredentials)
var filterOptions *SubscriptionFilterOptions = nil
if opts.Filter != nil {
filterOptions = &SubscriptionFilterOptions{
MaxSearchWindow: opts.MaxSearchWindow,
CheckpointInterval: opts.CheckpointInterval,
SubscriptionFilter: opts.Filter,
}
}
subscriptionRequest, err := toAllSubscriptionRequest(opts.From, opts.ResolveLinkTos, filterOptions)
if err != nil {
return nil, fmt.Errorf("failed to construct subscription. Reason: %w", err)
}
readClient, err := streamsClient.Read(ctx, subscriptionRequest, callOptions...)
if err != nil {
defer cancel()
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("failed to construct subscription. Reason: %w", err)
}
readResult, err := readClient.Recv()
if err != nil {
defer cancel()
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("failed to perform read. Reason: %w", err)
}
switch readResult.Content.(type) {
case *api.ReadResp_Confirmation:
{
confirmation := readResult.GetConfirmation()
return newSubscription(client, cancel, readClient, confirmation.SubscriptionId), nil
}
}
defer cancel()
return nil, fmt.Errorf("failed to initiate subscription")
}
// SubscribeToPersistentSubscription Connects to a persistent subscription group on a stream.
func (client *Client) SubscribeToPersistentSubscription(
ctx context.Context,
streamName string,
groupName string,
options SubscribeToPersistentSubscriptionOptions,
) (*PersistentSubscription, error) {
options.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.ConnectToPersistentSubscription(
ctx,
client.config,
&options,
handle,
int32(options.BufferSize),
streamName,
groupName,
)
}
// SubscribeToPersistentSubscriptionToAll Connects to a persistent subscription group to the $all stream.
func (client *Client) SubscribeToPersistentSubscriptionToAll(
ctx context.Context,
groupName string,
options SubscribeToPersistentSubscriptionOptions,
) (*PersistentSubscription, error) {
options.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
if !handle.SupportsFeature(featurePersistentSubscriptionToAll) {
return nil, unsupportedFeatureError()
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.ConnectToPersistentSubscription(
ctx,
client.config,
&options,
handle,
int32(options.BufferSize),
"",
groupName,
)
}
// CreatePersistentSubscription Creates a persistent subscription gorup on a stream.
//
// Persistent subscriptions are special kind of subscription where the server remembers the state of the subscription.
// This allows for many modes of operations compared to a regular or catcup subscription where the client
// holds the subscription state.
func (client *Client) CreatePersistentSubscription(
ctx context.Context,
streamName string,
groupName string,
options PersistentStreamSubscriptionOptions,
) error {
options.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
if options.Settings == nil {
setts := SubscriptionSettingsDefault()
options.Settings = &setts
}
return persistentSubscriptionClient.CreateStreamSubscription(ctx, client.config, &options, handle, streamName, groupName, options.StartFrom, *options.Settings)
}
// CreatePersistentSubscriptionToAll Creates a persistent subscription gorup on the $all stream.
//
// Persistent subscriptions are special kind of subscription where the server remembers the state of the subscription.
// This allows for many modes of operations compared to a regular or catcup subscription where the client
// holds the subscription state.
func (client *Client) CreatePersistentSubscriptionToAll(
ctx context.Context,
groupName string,
options PersistentAllSubscriptionOptions,
) error {
options.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
if !handle.SupportsFeature(featurePersistentSubscriptionToAll) {
return unsupportedFeatureError()
}
var filterOptions *SubscriptionFilterOptions = nil
if options.Filter != nil {
filterOptions = &SubscriptionFilterOptions{
MaxSearchWindow: options.MaxSearchWindow,
SubscriptionFilter: options.Filter,
}
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
if options.Settings == nil {
setts := SubscriptionSettingsDefault()
options.Settings = &setts
}
return persistentSubscriptionClient.CreateAllSubscription(
ctx,
client.config,
&options,
handle,
groupName,
options.StartFrom,
*options.Settings,
filterOptions,
)
}
// UpdatePersistentSubscription Updates a persistent subscription group on a stream.
func (client *Client) UpdatePersistentSubscription(
ctx context.Context,
streamName string,
groupName string,
options PersistentStreamSubscriptionOptions,
) error {
options.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
if options.Settings == nil {
setts := SubscriptionSettingsDefault()
options.Settings = &setts
}
return persistentSubscriptionClient.UpdateStreamSubscription(ctx, client.config, &options, handle, streamName, groupName, options.StartFrom, *options.Settings)
}
// UpdatePersistentSubscriptionToAll Updates a persistent subscription group on the $all stream.
func (client *Client) UpdatePersistentSubscriptionToAll(
ctx context.Context,
groupName string,
options PersistentAllSubscriptionOptions,
) error {
options.setDefaults()
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
if !handle.SupportsFeature(featurePersistentSubscriptionToAll) {
return unsupportedFeatureError()
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.UpdateAllSubscription(ctx, client.config, &options, handle, groupName, options.StartFrom, *options.Settings)
}
// DeletePersistentSubscription Deletes a persistent subscription group on a stream.
func (client *Client) DeletePersistentSubscription(
ctx context.Context,
streamName string,
groupName string,
options DeletePersistentSubscriptionOptions,
) error {
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.DeleteStreamSubscription(ctx, client.config, &options, handle, streamName, groupName)
}
// DeletePersistentSubscriptionToAll Deletes a persistent subscription group on the $all stream.
func (client *Client) DeletePersistentSubscriptionToAll(
ctx context.Context,
groupName string,
options DeletePersistentSubscriptionOptions,
) error {
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
if !handle.SupportsFeature(featurePersistentSubscriptionToAll) {
return unsupportedFeatureError()
}
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.DeleteAllSubscription(ctx, client.config, &options, handle, groupName)
}
// ReplayParkedMessages Replays the parked messages of a persistent subscription to a stream.
func (client *Client) ReplayParkedMessages(ctx context.Context, streamName string, groupName string, options ReplayParkedMessagesOptions) error {
return client.replayParkedMessages(ctx, streamName, groupName, options)
}
// ReplayParkedMessagesToAll Replays the parked messages of a persistent subscription to $all.
func (client *Client) ReplayParkedMessagesToAll(ctx context.Context, groupName string, options ReplayParkedMessagesOptions) error {
return client.replayParkedMessages(ctx, "$all", groupName, options)
}
func (client *Client) replayParkedMessages(ctx context.Context, streamName string, groupName string, options ReplayParkedMessagesOptions) error {
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
var finalStreamName *string
if streamName != "$all" {
finalStreamName = &streamName
}
if finalStreamName == nil && !handle.SupportsFeature(featurePersistentSubscriptionToAll) {
return unsupportedFeatureError()
}
if handle.SupportsFeature(featurePersistentSubscriptionManagement) {
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.replayParkedMessages(ctx, client.config, handle, finalStreamName, groupName, &options)
}
return client.httpReplayParkedMessages(streamName, groupName, options)
}
// ListAllPersistentSubscriptions Lists all persistent subscriptions regardless of which stream they are on.
func (client *Client) ListAllPersistentSubscriptions(ctx context.Context, options ListPersistentSubscriptionsOptions) ([]PersistentSubscriptionInfo, error) {
return client.listPersistentSubscriptionsInternal(ctx, nil, options)
}
// ListPersistentSubscriptionsForStream Lists all persistent subscriptions of a specific stream.
func (client *Client) ListPersistentSubscriptionsForStream(ctx context.Context, streamName string, options ListPersistentSubscriptionsOptions) ([]PersistentSubscriptionInfo, error) {
return client.listPersistentSubscriptionsInternal(ctx, &streamName, options)
}
// ListPersistentSubscriptionsToAll Lists all persistent subscriptions specific to the $all stream.
func (client *Client) ListPersistentSubscriptionsToAll(ctx context.Context, options ListPersistentSubscriptionsOptions) ([]PersistentSubscriptionInfo, error) {
streamName := "$all"
return client.listPersistentSubscriptionsInternal(ctx, &streamName, options)
}
func (client *Client) listPersistentSubscriptionsInternal(ctx context.Context, streamName *string, options ListPersistentSubscriptionsOptions) ([]PersistentSubscriptionInfo, error) {
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
if streamName != nil && *streamName == "$all" && !handle.SupportsFeature(featurePersistentSubscriptionToAll) {
return nil, unsupportedFeatureError()
}
if handle.SupportsFeature(featurePersistentSubscriptionManagement) {
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.listPersistentSubscriptions(ctx, client.config, handle, streamName, &options)
}
if streamName != nil {
return client.httpListPersistentSubscriptionsForStream(*streamName, options)
}
return client.httpListAllPersistentSubscriptions(options)
}
// GetPersistentSubscriptionInfo Gets the info for a specific persistent subscription to a stream
func (client *Client) GetPersistentSubscriptionInfo(ctx context.Context, streamName string, groupName string, options GetPersistentSubscriptionOptions) (*PersistentSubscriptionInfo, error) {
return client.getPersistentSubscriptionInfoInternal(ctx, &streamName, groupName, options)
}
// GetPersistentSubscriptionInfoToAll Gets the info for a specific persistent subscription to the $all stream.
func (client *Client) GetPersistentSubscriptionInfoToAll(ctx context.Context, groupName string, options GetPersistentSubscriptionOptions) (*PersistentSubscriptionInfo, error) {
return client.getPersistentSubscriptionInfoInternal(ctx, nil, groupName, options)
}
func (client *Client) getPersistentSubscriptionInfoInternal(ctx context.Context, streamName *string, groupName string, options GetPersistentSubscriptionOptions) (*PersistentSubscriptionInfo, error) {
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
if streamName != nil && *streamName == "$all" && !handle.SupportsFeature(featurePersistentSubscriptionToAll) {
return nil, unsupportedFeatureError()
}
if handle.SupportsFeature(featurePersistentSubscriptionManagement) {
persistentSubscriptionClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentSubscriptionClient.getPersistentSubscriptionInfo(ctx, client.config, handle, streamName, groupName, &options)
}
if streamName == nil {
streamName = new(string)
*streamName = "$all"
}
return client.httpGetPersistentSubscriptionInfo(*streamName, groupName, options)
}
// RestartPersistentSubscriptionSubsystem Restarts the persistent subscription subsystem on the server.
func (client *Client) RestartPersistentSubscriptionSubsystem(ctx context.Context, options RestartPersistentSubscriptionSubsystemOptions) error {
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return err
}
if handle.SupportsFeature(featurePersistentSubscriptionManagement) {
persistentClient := newPersistentClient(client.grpcClient, persistentProto.NewPersistentSubscriptionsClient(handle.Connection()))
return persistentClient.restartSubsystem(ctx, client.config, handle, &options)
}
return client.httpRestartSubsystem(options)
}
func readInternal(
parent context.Context,
client *Client,
options options,
handle *connectionHandle,
streamsClient api.StreamsClient,
readRequest *api.ReadReq,
) (*ReadStream, error) {
var headers, trailers metadata.MD
callOptions := []grpc.CallOption{grpc.Header(&headers), grpc.Trailer(&trailers)}
callOptions, ctx, cancel := configureGrpcCall(parent, client.config, options, callOptions, client.grpcClient.perRPCCredentials)
result, err := streamsClient.Read(ctx, readRequest, callOptions...)
if err != nil {
defer cancel()
err = client.grpcClient.handleError(handle, trailers, err)
return nil, fmt.Errorf("could not construct read operation. Reason: %w", err)
}
params := readStreamParams{
client: client.grpcClient,
handle: handle,
cancel: cancel,
inner: result,
headers: &headers,
trailers: &trailers,
}
return newReadStream(params), nil
}
func (client *Client) Gossip(ctx context.Context) ([]*gossip.MemberInfo, error) {
handle, err := client.grpcClient.getConnectionHandle()
if err != nil {
return nil, err
}
gossipClient := gossip.NewGossipClient(handle.Connection())
clusterInfo, err := gossipClient.Read(ctx, &shared.Empty{})
if err != nil {
return nil, err
}
return clusterInfo.Members, nil
}