-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.go
226 lines (195 loc) · 4.49 KB
/
stream.go
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
package krpcgo
import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"sync"
"github.com/atburke/krpc-go/lib/utils"
"github.com/atburke/krpc-go/types"
"github.com/golang/protobuf/proto"
"github.com/ztrue/tracerr"
)
// StreamClient is a client for kRPC streams.
type StreamClient struct {
sync.RWMutex
conn net.Conn
streams map[uint64]*streamManager
}
// NewStreamClient creates a new stream client with an existing connection.
func NewStreamClient(conn net.Conn) *StreamClient {
return &StreamClient{
conn: conn,
streams: make(map[uint64]*streamManager),
}
}
// Close closes the stream client.
func (s *StreamClient) Close() error {
return tracerr.Wrap(s.conn.Close())
}
// Send sends protobuf-encoded data to a stream server.
func (s *StreamClient) Send(data []byte) error {
return tracerr.Wrap(send(s.conn, data))
}
// Receive receives protobuf-encoded data from a stream server.
func (s *StreamClient) Receive() ([]byte, error) {
data, err := receive(s.conn)
return data, tracerr.Wrap(err)
}
// Run starts the stream handler.
func (s *StreamClient) Run(ctx context.Context) {
for {
data, err := s.Receive()
if errors.Is(err, io.EOF) {
return
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading stream: %v\n", err)
}
var streamUpdate types.StreamUpdate
if err := proto.Unmarshal(data, &streamUpdate); err != nil {
fmt.Fprintf(os.Stderr, "Error unmarshaling stream result: %v\n", err)
}
for _, result := range streamUpdate.Results {
s.WriteToStream(result.Id, result.Result.Value)
}
select {
case <-ctx.Done():
s.Close()
return
default:
}
}
}
func (s *StreamClient) getStreamManager(id uint64) *streamManager {
s.RLock()
sm, ok := s.streams[id]
s.RUnlock()
if ok {
return sm
}
s.Lock()
defer s.Unlock()
// Check if the stream was created by another thread in between locks.
sm, ok = s.streams[id]
if ok {
return sm
}
sm = newStreamManager(id)
s.streams[id] = sm
return sm
}
// WriteToStream writes data to a particular stream.
func (s *StreamClient) WriteToStream(id uint64, b []byte) {
sm := s.getStreamManager(id)
sm.write(b)
}
// GetStream gets a byte stream for a particular stream ID.
func (s *StreamClient) GetStream(id uint64) *Stream[[]byte] {
return s.getStreamManager(id).newStream()
}
// DeleteStream removes a byte stream for a particular stream ID. Note that
// if the stream hasn't yet been closed on the kRPC server, a new local stream
// will eventually be recreated.
func (s *StreamClient) DeleteStream(id uint64) {
s.Lock()
defer s.Unlock()
delete(s.streams, id)
}
type streamManager struct {
id uint64
channels map[int]chan []byte
newID func() int
sync.RWMutex
}
func newStreamManager(id uint64) *streamManager {
return &streamManager{
id: id,
channels: make(map[int]chan []byte),
newID: utils.NewIDGenerator(),
}
}
func (sm *streamManager) newStream() *Stream[[]byte] {
sm.Lock()
defer sm.Unlock()
c := make(chan []byte)
idx := sm.newID()
sm.channels[idx] = c
s := &Stream[[]byte]{
C: c,
ID: sm.id,
clone: sm.newStream,
}
s.AddCloser(func() error {
sm.deleteStream(idx)
return nil
})
return s
}
func (sm *streamManager) deleteStream(idx int) {
sm.Lock()
defer sm.Unlock()
delete(sm.channels, idx)
}
func (sm *streamManager) write(b []byte) {
sm.RLock()
defer sm.RUnlock()
for _, ch := range sm.channels {
select {
case ch <- b:
// Don't update channel if no one is listening.
default:
}
}
}
// Stream is a struct for receiving stream data.
type Stream[T any] struct {
C chan T
ID uint64
clone func() *Stream[T]
closers []func() error
}
// Clone clones the stream for another thread to listen on.
func (s *Stream[T]) Clone() *Stream[T] {
return s.clone()
}
func (s *Stream[T]) AddCloser(close func() error) {
s.closers = append(s.closers, close)
}
// Close closes the stream.
func (s *Stream[T]) Close() error {
for _, close := range s.closers {
if err := close(); err != nil {
return tracerr.Wrap(err)
}
}
return nil
}
// MapStream converts a stream to another type.
func MapStream[S, T any](src *Stream[S], m func(S) T) *Stream[T] {
ctx, cancel := context.WithCancel(context.Background())
dst := &Stream[T]{
C: make(chan T),
ID: src.ID,
clone: func() *Stream[T] {
return MapStream(src.Clone(), m)
},
}
dst.AddCloser(func() error {
cancel()
return tracerr.Wrap(src.Close())
})
go func() {
for {
select {
case data := <-src.C:
dst.C <- m(data)
case <-ctx.Done():
return
}
}
}()
return dst
}