-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.go
128 lines (108 loc) · 2.81 KB
/
chat.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
package example
import (
"context"
"fmt"
"time"
"github.com/dop251/goja"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib/netext/grpcext"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
)
func init() {
modules.Register("k6/x/chat", new(RootModule))
}
type (
// RootModule is the global module instance that will create module
// instances for each VU.
RootModule struct{}
// ModuleInstance represents an instance of the module.
ModuleInstance struct {
vu modules.VU
}
)
var (
_ modules.Module = &RootModule{}
_ modules.Instance = &ModuleInstance{}
)
// New returns a pointer to a new RootModule instance.
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance implements the modules.Module interface to return
// a new instance for each VU.
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{vu: vu}
}
// Exports returns the exports of the module.
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Named: map[string]interface{}{
"send": mi.OpenAndSend,
},
}
}
func (mi *ModuleInstance) OpenAndSend(addr string, method string, payload goja.Value) (*grpcext.Response, error) {
ctx, cancel := context.WithTimeout(mi.vu.Context(), 5*time.Second)
defer cancel()
notls := grpc.WithTransportCredentials(insecure.NewCredentials())
conn, err := grpcext.Dial(ctx, addr, notls)
if err != nil {
return nil, fmt.Errorf("dialing failed: %w", err)
}
defer conn.Close()
md, err := mi.methodDescriptor(ctx, conn, method)
if err != nil {
return nil, err
}
req := grpcext.Request{
MethodDescriptor: md,
Tags: make(map[string]string),
}
if payload != nil {
b, err := payload.ToObject(mi.vu.Runtime()).MarshalJSON()
if err != nil {
return nil, err
}
req.Message = b
}
return conn.Invoke(ctx, method, nil, req)
}
func (mi *ModuleInstance) methodDescriptor(ctx context.Context, c *grpcext.Conn, method string) (protoreflect.MethodDescriptor, error) {
rc, err := c.ReflectionClient()
if err != nil {
return nil, err
}
fdset, err := rc.Reflect(ctx)
if err != nil {
return nil, err
}
files, err := protodesc.NewFiles(fdset)
if err != nil {
return nil, err
}
var md protoreflect.MethodDescriptor
files.RangeFiles(func(fd protoreflect.FileDescriptor) bool {
sds := fd.Services()
for i := 0; i < sds.Len(); i++ {
sd := sds.Get(i)
sdname := sd.FullName()
mds := sd.Methods()
for j := 0; j < mds.Len(); j++ {
mdi := mds.Get(j)
methodName := fmt.Sprintf("/%s/%s", sdname, mdi.Name())
if methodName == method {
md = mdi
return false
}
}
}
return true
})
if md == nil {
return nil, fmt.Errorf("service method not found")
}
return md, nil
}