forked from SkySoft-ATM/gorillaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_registry.go
180 lines (156 loc) · 4.6 KB
/
stream_registry.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
package gorillaz
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"github.com/skysoft-atm/gorillaz/stream"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/protobuf/proto"
)
const (
streamDefinitions = "streamDefinitions"
StreamProviderTag = "streamProvider"
)
type StreamDefinition struct {
Name string
DataType string
}
type provider interface {
close()
streamDefinition() *StreamDefinition
sendLoop(strm grpc.ServerStream, peer Peer, opts sendLoopOpts) error
streamType() stream.StreamType
sendHelloMessage(strm grpc.ServerStream, peer Peer) error
}
type sendLoopOpts struct {
disconnectOnBackpressure bool
}
type streamRegistry struct {
sync.RWMutex
g *Gaz
providers map[string]provider
}
func newStreamRegistry(g *Gaz) *streamRegistry {
sr := &streamRegistry{
g: g,
providers: make(map[string]provider),
}
return sr
}
func (sr *streamRegistry) find(streamName string) (provider, bool) {
sr.RLock()
p, ok := sr.providers[streamName]
sr.RUnlock()
return p, ok
}
func (g *Gaz) closeStream(p provider) error {
streamName := p.streamDefinition().Name
Log.Info("closing stream", zap.String("stream", streamName))
prov, ok := g.streamRegistry.find(streamName)
if !ok {
return fmt.Errorf("cannot find stream " + streamName)
}
g.streamRegistry.unregister(streamName)
prov.close()
return nil
}
func (sr *streamRegistry) register(p provider) {
streamName := p.streamDefinition().Name
sr.Lock()
defer sr.Unlock()
if _, found := sr.providers[streamName]; found {
panic("cannot register 2 providers with the same streamName: " + streamName)
}
sr.providers[streamName] = p
sd := stream.StreamDefinition{
Name: streamName,
DataType: p.streamDefinition().DataType,
StreamType: p.streamType(),
}
bytes, err := proto.Marshal(&sd)
if err != nil {
panic(err)
}
se := &stream.Event{Ctx: context.Background(), Key: []byte(streamName), Value: bytes}
if sr.g.streamDefinitions != nil {
sr.g.streamDefinitions.Submit(se)
}
}
func (sr *streamRegistry) unregister(streamName string) {
sr.Lock()
defer sr.Unlock()
_, ok := sr.providers[streamName]
if ok {
delete(sr.providers, streamName)
sr.g.streamDefinitions.Delete([]byte(streamName))
}
}
type StreamRequest interface {
GetName() string
GetRequesterName() string
GetExpectHello() bool
GetDisconnectOnBackpressure() bool
}
// Stream implements streaming.proto Stream.
// should not be called by the client
func (sr *streamRegistry) Stream(req *stream.StreamRequest, strm stream.Stream_StreamServer) error {
return sr.publishOnStream(req, strm)
}
func (sr *streamRegistry) publishOnStream(np StreamRequest, strm grpc.ServerStream) error {
peer := getPeer(strm, np)
streamName := np.GetName()
requester := np.GetRequesterName()
opts := sendLoopOpts{
disconnectOnBackpressure: np.GetDisconnectOnBackpressure(),
}
Log.Info("new stream consumer", zap.String("stream", streamName), zap.String("peer", peer.address), zap.String("requester", requester))
sr.RLock()
provider, ok := sr.providers[streamName]
sr.RUnlock()
if !ok {
Log.Warn("unknown stream", zap.String("stream", streamName), zap.String("peer", peer.address), zap.String("requester", requester))
return fmt.Errorf("unknown stream %s", streamName)
}
// we send some metadata for backward compatibility, it was previously used on the client side to check if the stream connection is really established
header := metadata.Pairs("name", streamName, "expectHello", strconv.FormatBool(np.GetExpectHello()))
err := strm.SendHeader(header)
if err != nil {
Log.Error("client might be disconnected %s", zap.Error(err), zap.String("peer", peer.address), zap.String("requester", requester))
return err
}
if np.GetExpectHello() {
err := provider.sendHelloMessage(strm, peer)
if err != nil {
return err
}
}
return provider.sendLoop(strm, peer, opts)
}
func getPeer(strm grpc.ServerStream, np StreamRequest) Peer {
return Peer{GetGrpcClientAddress(strm.Context()), np.GetRequesterName()}
}
func (sr *streamRegistry) GetAndWatch(req *stream.GetAndWatchRequest, strm stream.Stream_GetAndWatchServer) error {
return sr.publishOnStream(req, strm)
}
type Peer struct {
address string
serviceName string
}
func GetGrpcClientAddress(ctx context.Context) string {
pr, ok := peer.FromContext(ctx)
if !ok {
return "no peer in context"
}
if pr.Addr == net.Addr(nil) {
return "no address found"
}
return pr.Addr.String()
}
func (g *Gaz) DiscoverStreamDefinitions(serviceName string) (GetAndWatchStreamConsumer, error) {
return g.GetAndWatchStream(serviceName, streamDefinitions)
}