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 pathreads.go
More file actions
81 lines (66 loc) · 1.76 KB
/
reads.go
File metadata and controls
81 lines (66 loc) · 1.76 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
package esdb
import (
"context"
"errors"
"fmt"
"io"
"sync"
"sync/atomic"
api "github.com/EventStore/EventStore-Client-Go/v4/protos/streams"
"google.golang.org/grpc/metadata"
)
// ReadStream read stream iterator.
type ReadStream struct {
once *sync.Once
closed *int32
params readStreamParams
}
type readStreamParams struct {
client *grpcClient
handle *connectionHandle
cancel context.CancelFunc
inner api.Streams_ReadClient
headers *metadata.MD
trailers *metadata.MD
}
// Close closes the iterator and release allocated resources.
func (stream *ReadStream) Close() {
stream.once.Do(func() {
atomic.StoreInt32(stream.closed, 1)
stream.params.cancel()
})
}
// Recv awaits for the next incoming event.
func (stream *ReadStream) Recv() (*ResolvedEvent, error) {
if atomic.LoadInt32(stream.closed) != 0 {
return nil, io.EOF
}
msg, err := stream.params.inner.Recv()
if err != nil {
atomic.StoreInt32(stream.closed, 1)
if !errors.Is(err, io.EOF) {
err = stream.params.client.handleError(stream.params.handle, *stream.params.trailers, err)
}
return nil, err
}
switch msg.Content.(type) {
case *api.ReadResp_Event:
resolvedEvent := getResolvedEventFromProto(msg.GetEvent())
return &resolvedEvent, nil
case *api.ReadResp_StreamNotFound_:
atomic.StoreInt32(stream.closed, 1)
streamName := string(msg.Content.(*api.ReadResp_StreamNotFound_).StreamNotFound.StreamIdentifier.StreamName)
return nil, &Error{code: ErrorCodeResourceNotFound, err: fmt.Errorf("stream '%s' is not found", streamName)}
}
panic("unreachable code")
}
func newReadStream(params readStreamParams) *ReadStream {
once := new(sync.Once)
closed := new(int32)
atomic.StoreInt32(closed, 0)
return &ReadStream{
once: once,
closed: closed,
params: params,
}
}