-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
222 lines (191 loc) · 5.4 KB
/
Copy pathclient.go
File metadata and controls
222 lines (191 loc) · 5.4 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package simplestdioplugin
import (
"context"
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/google/uuid"
)
type PluginData struct {
router map[string]func(data []byte) ([]byte, error)
resp_mutex sync.RWMutex
resp_map map[string]chan ReadResult
req_c chan ReadResult
heartbeat_c chan struct{}
stdin *os.File
stdout *os.File
}
type PluginClientConfig struct {
// Router is functions that can be called from host side
Router map[string]func(data []byte) ([]byte, error)
// Stdin is file descriptor for plugin input, defaults to os.Stdin
Stdin *os.File
// Stdout is file descriptor for plugin output, defaults to os.Stdout
Stdout *os.File
}
// Command call function from host and return its result
func (plugin *PluginData) Command(input MessageInput, timeout ...time.Duration) ([]byte, error) {
bytes, err := encodeMessage(input)
if err != nil {
return nil, fmt.Errorf("failed to encode MessageInput: %v", err)
}
id := uuid.New().String()
resp_c := make(chan ReadResult)
plugin.resp_mutex.Lock()
plugin.resp_map[id] = resp_c
plugin.resp_mutex.Unlock()
defer func() {
plugin.resp_mutex.Lock()
defer plugin.resp_mutex.Unlock()
close(plugin.resp_map[id])
delete(plugin.resp_map, id)
}()
if err := writeAll([]byte(id), bytes, COMMAND_REQUEST, plugin.stdout); err != nil {
return nil, fmt.Errorf("failed to write request: %v", err)
}
if len(timeout) > 0 {
timer := time.NewTimer(timeout[0])
defer timer.Stop()
select {
case <-timer.C:
return nil, fmt.Errorf("command timed out")
case result := <-resp_c:
if result.command == COMMAND_ERROR {
return nil, fmt.Errorf("%s", string(result.data))
}
return result.data, nil
}
} else {
result := <-resp_c
if result.command == COMMAND_ERROR {
return nil, fmt.Errorf("%s", string(result.data))
}
return result.data, nil
}
}
func (plugin *PluginData) reader(ctx context.Context) error {
result := make(map[string][]byte)
for {
select {
case <-ctx.Done():
return nil
default:
read, err := readChunk(plugin.stdin)
if err != nil {
return err
}
switch read.command {
case COMMAND_DATA:
result[string(read.uuid)] = append(result[string(read.uuid)], read.data...)
case COMMAND_REQUEST:
plugin.req_c <- ReadResult{uuid: read.uuid, command: read.command, data: result[string(read.uuid)]}
delete(result, string(read.uuid))
case COMMAND_ERROR:
plugin.resp_mutex.RLock()
resp_c, ok := plugin.resp_map[string(read.uuid)]
plugin.resp_mutex.RUnlock()
if ok && resp_c != nil {
resp_c <- ReadResult{uuid: read.uuid, command: read.command, data: result[string(read.uuid)]}
}
delete(result, string(read.uuid))
case COMMAND_RESULT:
plugin.resp_mutex.RLock()
resp_c, ok := plugin.resp_map[string(read.uuid)]
plugin.resp_mutex.RUnlock()
if ok && resp_c != nil {
resp_c <- ReadResult{uuid: read.uuid, command: read.command, data: result[string(read.uuid)]}
}
delete(result, string(read.uuid))
case COMMAND_HEARTBEAT:
plugin.heartbeat_c <- struct{}{}
default:
return errors.New("invalid command")
}
}
}
}
func NewPluginClient(config PluginClientConfig) *PluginData {
stdin := config.Stdin
stdout := config.Stdout
if stdin == nil {
stdin = os.Stdin
}
if stdout == nil {
stdout = os.Stdout
}
return &PluginData{
router: config.Router,
resp_mutex: sync.RWMutex{},
resp_map: make(map[string]chan ReadResult),
req_c: make(chan ReadResult),
heartbeat_c: make(chan struct{}),
stdin: stdin,
stdout: stdout,
}
}
func PluginServe(plugin *PluginData, max_conn ...int) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
max_concurrent := 1000
if len(max_conn) > 0 {
max_concurrent = max_conn[0]
}
mut := sync.RWMutex{}
concurrent := 0
// reader
go func() {
if err := plugin.reader(ctx); err != nil {
_ = writeAll([]byte(uuid.New().String()), []byte("reader error: "+err.Error()), COMMAND_ERROR, plugin.stdout)
}
cancel()
}()
var timer = time.NewTimer(5 * time.Second)
for {
select {
case <-ctx.Done():
return fmt.Errorf("plugin closed (reader errored)")
case <-timer.C:
cancel()
return fmt.Errorf("plugin closed (heartbeat timeout)")
case <-plugin.heartbeat_c:
timer.Reset(5 * time.Second)
case data := <-plugin.req_c:
mut.RLock()
if concurrent >= max_concurrent {
_ = writeAll(data.uuid, []byte("error concurrent: MAX CONCURRENT command reached"), COMMAND_ERROR, plugin.stdout)
mut.RUnlock()
continue
}
mut.RUnlock()
input, err := decodeMessage(data.data)
if err != nil {
_ = writeAll(data.uuid, []byte(fmt.Sprintf("error decode message: %v", err)), COMMAND_ERROR, plugin.stdout)
continue
}
mut.Lock()
concurrent += 1
mut.Unlock()
go func() {
defer func() {
mut.Lock()
concurrent -= 1
mut.Unlock()
}()
if function := plugin.router[input.Function]; function != nil {
result, err := function(input.Data)
if err != nil {
_ = writeAll(data.uuid, []byte("error func: "+err.Error()), COMMAND_ERROR, plugin.stdout)
} else {
if err := writeAll(data.uuid, result, COMMAND_RESULT, plugin.stdout); err != nil {
_ = writeAll(data.uuid, []byte("error write: "+err.Error()), COMMAND_ERROR, plugin.stdout)
}
}
} else {
_ = writeAll(data.uuid, []byte("error func: "+input.Function+" not found"), COMMAND_ERROR, plugin.stdout)
}
}()
}
}
}