-
Notifications
You must be signed in to change notification settings - Fork 110
/
summationapi.go
193 lines (166 loc) · 5.06 KB
/
summationapi.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
// Package summation defines a simple number summing service API for demonstration purposes.
package summationapi
import (
"context"
"sync"
"github.com/edaniels/golog"
"github.com/pkg/errors"
pb "go.viam.com/rdk/examples/customresources/apis/proto/api/service/summation/v1"
"go.viam.com/rdk/robot"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/subtype"
"go.viam.com/rdk/utils"
goutils "go.viam.com/utils"
)
var Subtype = resource.NewSubtype(
resource.Namespace("acme"),
resource.ResourceTypeService,
resource.SubtypeName("summation"),
)
// Named is a helper for getting the named Summation's typed resource name.
func Named(name string) resource.Name {
return resource.NameFromSubtype(Subtype, name)
}
// FromRobot is a helper for getting the named Summation from the given Robot.
func FromRobot(r robot.Robot, name string) (Summation, error) {
return robot.ResourceFromRobot[Summation](r, Named(name))
}
func init() {
registry.RegisterResourceSubtype(Subtype, registry.ResourceSubtype{
Reconfigurable: wrapWithReconfigurable,
RegisterRPCService: func(ctx context.Context, rpcServer rpc.Server, subtypeSvc subtype.Service) error {
return rpcServer.RegisterServiceServer(
ctx,
&pb.SummationService_ServiceDesc,
NewServer(subtypeSvc),
pb.RegisterSummationServiceHandlerFromEndpoint,
)
},
RPCServiceDesc: &pb.SummationService_ServiceDesc,
RPCClient: func(ctx context.Context, conn rpc.ClientConn, name string, logger golog.Logger) interface{} {
return newClientFromConn(conn, name, logger)
},
})
}
// Summation defines the Go interface for the service (should match the protobuf methods.)
type Summation interface {
Sum(ctx context.Context, nums []float64) (float64, error)
}
func wrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) {
mc, ok := r.(Summation)
if !ok {
return nil, utils.NewUnimplementedInterfaceError((Summation)(nil), r)
}
if reconfigurable, ok := mc.(*reconfigurableSummation); ok {
return reconfigurable, nil
}
return &reconfigurableSummation{actual: mc, name: name}, nil
}
var (
_ = Summation(&reconfigurableSummation{})
_ = resource.Reconfigurable(&reconfigurableSummation{})
)
type reconfigurableSummation struct {
mu sync.RWMutex
name resource.Name
actual Summation
}
func (g *reconfigurableSummation) ProxyFor() interface{} {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual
}
func (g *reconfigurableSummation) Reconfigure(ctx context.Context, newSummation resource.Reconfigurable) error {
g.mu.Lock()
defer g.mu.Unlock()
actual, ok := newSummation.(*reconfigurableSummation)
if !ok {
return utils.NewUnexpectedTypeError(g, newSummation)
}
if err := goutils.TryClose(ctx, g.actual); err != nil {
golog.Global().Errorw("error closing old", "error", err)
}
g.actual = actual.actual
return nil
}
func (g *reconfigurableSummation) Sum(ctx context.Context, nums []float64) (float64, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.Sum(ctx, nums)
}
func (g *reconfigurableSummation) Name() resource.Name {
g.mu.RLock()
defer g.mu.RUnlock()
return g.name
}
// subtypeServer implements the Summation RPC service from summation.proto.
type subtypeServer struct {
pb.UnimplementedSummationServiceServer
s subtype.Service
}
func NewServer(s subtype.Service) pb.SummationServiceServer {
return &subtypeServer{s: s}
}
func (s *subtypeServer) getMyService(name string) (Summation, error) {
resource := s.s.Resource(name)
if resource == nil {
return nil, errors.Errorf("no summation service with name (%s)", name)
}
g, ok := resource.(Summation)
if !ok {
return nil, errors.Errorf("resource with name (%s) is not a Summation", name)
}
return g, nil
}
func (s *subtypeServer) Sum(ctx context.Context, req *pb.SumRequest) (*pb.SumResponse, error) {
g, err := s.getMyService(req.Name)
if err != nil {
return nil, err
}
resp, err := g.Sum(ctx, req.Numbers)
if err != nil {
return nil, err
}
return &pb.SumResponse{Sum: resp}, nil
}
func newClientFromConn(conn rpc.ClientConn, name string, logger golog.Logger) Summation {
sc := newSvcClientFromConn(conn, logger)
return clientFromSvcClient(sc, name)
}
func newSvcClientFromConn(conn rpc.ClientConn, logger golog.Logger) *serviceClient {
client := pb.NewSummationServiceClient(conn)
sc := &serviceClient{
conn: conn,
client: client,
logger: logger,
}
return sc
}
type serviceClient struct {
conn rpc.ClientConn
client pb.SummationServiceClient
logger golog.Logger
}
type client struct {
*serviceClient
name string
}
func clientFromSvcClient(sc *serviceClient, name string) Summation {
return &client{sc, name}
}
func (c *client) Sum(ctx context.Context, nums []float64) (float64, error) {
resp, err := c.client.Sum(ctx, &pb.SumRequest{
Name: c.name,
Numbers: nums,
})
if err != nil {
return 0, err
}
return resp.Sum, nil
}
// NewUnimplementedInterfaceError is used when there is a failed interface check.
func NewUnimplementedInterfaceError(actual interface{}) error {
return utils.NewUnimplementedInterfaceError((Summation)(nil), actual)
}