-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
145 lines (115 loc) · 3.01 KB
/
events.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
package protoevents
import (
"context"
"strings"
"sync"
evtpb "github.com/srikrsna/protoevents/events"
"github.com/taskcluster/slugid-go/slugid"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
//go:generate mockgen -package mocks -destination mocks/$GOFILE . Dispatcher,ErrorLogger
type Event struct {
Id string
Type string
Request protoreflect.ProtoMessage
Response protoreflect.ProtoMessage
}
type Dispatcher interface {
Dispatch(context.Context, *Event) error
}
type ErrorLogger interface {
Log(context.Context, *Event, error)
}
type Options struct {
Dispatcher Dispatcher
ErrorLogger ErrorLogger
Preprocess bool
}
func NewInterceptor(opt Options) grpc.UnaryServerInterceptor {
var shouldFire func(method string) (bool, error)
if !opt.Preprocess {
shouldFire = lazy
} else {
preprocess()
shouldFire = eager
}
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
resp, err = handler(ctx, req)
if err != nil {
return
}
v, err := shouldFire(info.FullMethod)
if err != nil {
opt.ErrorLogger.Log(ctx, &Event{
Id: "evt_" + slugid.Nice(),
Type: info.FullMethod,
Request: req.(protoreflect.ProtoMessage),
Response: resp.(protoreflect.ProtoMessage),
}, err)
}
if !v {
return
}
evt := evtBuf.Get().(*Event)
defer evtBuf.Put(evt)
*evt = Event{
Id: "evt_" + slugid.Nice(),
Type: info.FullMethod,
Request: req.(protoreflect.ProtoMessage),
Response: resp.(protoreflect.ProtoMessage),
}
if err := opt.Dispatcher.Dispatch(ctx, evt); err != nil {
opt.ErrorLogger.Log(ctx, evt, err)
}
return
}
}
func lazy(method string) (bool, error) {
mutex.RLock()
v, ok := cache[method]
mutex.RUnlock()
if !ok {
parts := strings.Split(method, "/")
serviceName := protoreflect.FullName(parts[1])
methodName := protoreflect.Name(parts[2])
descriptor, err := protoregistry.GlobalFiles.FindDescriptorByName(serviceName)
if err != nil {
return false, err
}
v = getFromMethod(descriptor.(protoreflect.ServiceDescriptor).Methods().ByName(methodName))
mutex.Lock()
cache[method] = v
mutex.Unlock()
}
return v, nil
}
func preprocess() {
protoregistry.GlobalFiles.RangeFiles(func(fd protoreflect.FileDescriptor) bool {
services := fd.Services()
for i := 0; i < services.Len(); i++ {
s := services.Get(i)
methods := s.Methods()
for j := 0; j < methods.Len(); j++ {
m := methods.Get(j)
cache["/"+string(s.FullName())+"/"+string(m.Name())] = getFromMethod(m)
}
}
return true
})
}
func getFromMethod(md protoreflect.MethodDescriptor) bool {
return proto.GetExtension(md.Options(), evtpb.E_Fire).(bool)
}
func eager(method string) (bool, error) {
return cache[method], nil
}
var cache = map[string]bool{}
var mutex sync.RWMutex
var evtBuf = sync.Pool{
New: func() interface{} {
return new(Event)
},
}