-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
180 lines (146 loc) · 4.57 KB
/
server.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 grpc
import (
"context"
"sync"
"time"
"github.com/dipdup-net/abi-indexer/internal/storage"
"github.com/dipdup-net/abi-indexer/pkg/modules/grpc/pb"
"github.com/dipdup-net/abi-indexer/pkg/modules/metadata"
"github.com/dipdup-net/indexer-sdk/pkg/modules"
"github.com/dipdup-net/indexer-sdk/pkg/modules/grpc"
generalPB "github.com/dipdup-net/indexer-sdk/pkg/modules/grpc/pb"
"github.com/pkg/errors"
)
// Server -
type Server struct {
*grpc.Server
pb.UnimplementedMetadataServiceServer
input *modules.Input
metadata storage.IMetadata
metadataSubscriptions *grpc.Subscriptions[*storage.Metadata, *pb.SubscriptionMetadata]
wg *sync.WaitGroup
}
// NewServer -
func NewServer(
cfg *grpc.ServerConfig,
metadataRepo storage.IMetadata,
) (*Server, error) {
if cfg == nil {
return nil, errors.New("configuration structure of gRPC server is nil")
}
server, err := grpc.NewServer(cfg)
if err != nil {
return nil, err
}
return &Server{
Server: server,
input: modules.NewInput(metadata.OutputMetadata),
metadataSubscriptions: grpc.NewSubscriptions[*storage.Metadata, *pb.SubscriptionMetadata](),
metadata: metadataRepo,
wg: new(sync.WaitGroup),
}, nil
}
// Name -
func (server *Server) Name() string {
return "metadata_grpc_server"
}
// Input -
func (server *Server) Input(name string) (*modules.Input, error) {
if name != metadata.OutputMetadata {
return nil, errors.Wrap(modules.ErrUnknownInput, name)
}
return server.input, nil
}
// Output -
func (server *Server) Output(name string) (*modules.Output, error) {
return nil, errors.Wrap(modules.ErrUnknownOutput, name)
}
// AttachTo -
func (server *Server) AttachTo(name string, input *modules.Input) error {
return errors.Wrap(modules.ErrUnknownOutput, name)
}
// Start -
func (server *Server) Start(ctx context.Context) {
pb.RegisterMetadataServiceServer(server.Server.Server(), server)
server.Server.Start(ctx)
server.wg.Add(1)
go server.listen(ctx)
}
func (server *Server) listen(ctx context.Context) {
defer server.wg.Done()
for {
select {
case <-ctx.Done():
return
case msg, ok := <-server.input.Listen():
if !ok {
return
}
model, ok := msg.(storage.Metadata)
if !ok {
continue
}
server.metadataSubscriptions.NotifyAll(&model, SubscriptionMetadata)
}
}
}
// Close -
func (server *Server) Close() error {
if err := server.input.Close(); err != nil {
return err
}
return server.Server.Close()
}
////////////////////////////////////////////////
////////////// HANDLERS //////////////////
////////////////////////////////////////////////
// SubscribeOnMetadata -
func (server *Server) SubscribeOnMetadata(req *generalPB.DefaultRequest, stream pb.MetadataService_SubscribeOnMetadataServer) error {
return grpc.DefaultSubscribeOn[*storage.Metadata, *pb.SubscriptionMetadata](
stream,
server.metadataSubscriptions,
NewMetadataSubscription(),
nil,
)
}
// UnsubscribeFromMetadata -
func (server *Server) UnsubscribeFromMetadata(ctx context.Context, req *generalPB.UnsubscribeRequest) (*generalPB.UnsubscribeResponse, error) {
return grpc.DefaultUnsubscribe(ctx, server.metadataSubscriptions, req.Id)
}
// GetMetadata -
func (server *Server) GetMetadata(ctx context.Context, req *pb.GetMetadataRequest) (*pb.Metadata, error) {
reqCtx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
metadata, err := server.metadata.GetByAddress(reqCtx, req.Address)
if err != nil {
return nil, err
}
return Metadata(metadata), nil
}
// ListMetadata -
func (server *Server) ListMetadata(ctx context.Context, req *pb.ListMetadataRequest) (*pb.ListMetadataResponse, error) {
p := newPage(req.GetPage())
metadata, err := server.metadata.List(ctx, p.limit, p.offset, p.order)
if err != nil {
return nil, err
}
return ListMetadataResponse(metadata), nil
}
// GetMetadataByMethodSinature -
func (server *Server) GetMetadataByMethodSinature(ctx context.Context, req *pb.GetMetadataByMethodSinatureRequest) (*pb.ListMetadataResponse, error) {
p := newPage(req.GetPage())
metadata, err := server.metadata.GetByMethod(ctx, req.Signature, p.limit, p.offset, p.order)
if err != nil {
return nil, err
}
return ListMetadataResponse(metadata), nil
}
// GetMetadataByTopic -
func (server *Server) GetMetadataByTopic(ctx context.Context, req *pb.GetMetadataByTopicRequest) (*pb.ListMetadataResponse, error) {
p := newPage(req.GetPage())
metadata, err := server.metadata.GetByTopic(ctx, req.Topic, p.limit, p.offset, p.order)
if err != nil {
return nil, err
}
return ListMetadataResponse(metadata), nil
}