-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
84 lines (73 loc) · 2.61 KB
/
service.go
File metadata and controls
84 lines (73 loc) · 2.61 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
82
83
84
package zrpc
import (
"context"
"fmt"
"reflect"
"go.uber.org/zap"
)
// MethodHandler is a function type that processes a unary RPC method call.
type MethodHandler func(srv any, ctx context.Context, dec func(any) error, middleware ServerMiddleware) (any, error)
// MethodDesc represents an RPC service's method specification.
type MethodDesc struct {
MethodName string
Handler MethodHandler
}
// ServiceDesc represents an RPC service's specification.
type ServiceDesc struct {
ServiceName string
// The pointer to the service interface. Used to check whether the user
// provided implementation satisfies the interface requirements.
HandlerType any
Methods []MethodDesc
Metadata any
}
type service struct {
serviceImpl any
methods map[string]*MethodDesc
mdata any
}
// ServiceRegistrar wraps a single method that supports service registration. It
// enables users to pass concrete types other than grpc.Server to the service
// registration methods exported by the IDL generated code.
type ServiceRegistrar interface {
// RegisterService registers a service and its implementation to the
// concrete type implementing this interface. It may not be called
// once the server has started serving.
// desc describes the service and its methods and handlers. impl is the
// service implementation which is passed to the method handlers.
RegisterService(desc *ServiceDesc, impl any)
}
// RegisterService registers a service and its implementation to the gRPC
// server. It is called from the IDL generated code. This must be called before
// invoking Serve. If ss is non-nil (for legacy code), its type is checked to
// ensure it implements sd.HandlerType.
func (s *Server) RegisterService(sd *ServiceDesc, srv any) {
if srv != nil {
ht := reflect.TypeOf(sd.HandlerType).Elem()
st := reflect.TypeOf(srv)
if !st.Implements(ht) {
zap.L().Fatal(fmt.Sprintf("zrpc: Server.RegisterService found the handler of type %v that does not satisfy %v", st, ht))
}
}
s.register(sd, srv)
}
func (s *Server) register(sd *ServiceDesc, srv any) {
s.mu.Lock()
defer s.mu.Unlock()
if s.serve {
zap.L().Fatal("zrpc: Server.RegisterService after Server.Serve for ", zap.String("name", sd.ServiceName))
}
if _, ok := s.serviceMap.Load(sd.ServiceName); ok {
zap.L().Fatal("zrpc: Server.RegisterService found duplicate service registration for %q", zap.String("name", sd.ServiceName))
}
svc := &service{
serviceImpl: srv,
methods: make(map[string]*MethodDesc),
mdata: sd.Metadata,
}
for i := range sd.Methods {
d := &sd.Methods[i]
svc.methods[d.MethodName] = d
}
s.serviceMap.Store(sd.ServiceName, svc)
}