-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
206 lines (179 loc) · 4.94 KB
/
Copy pathserver.go
File metadata and controls
206 lines (179 loc) · 4.94 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
package simplestdioplugin
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"sync"
"time"
"github.com/google/uuid"
)
type PluginRunning struct {
Name string
Path string
log_func func(message string)
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{}
cmd *exec.Cmd
pipe_in *os.File
pipe_out *os.File
pipe_err *os.File
}
// Command call function from plugin and return its result
func (plugin *PluginRunning) Command(input MessageInput, timeout ...time.Duration) ([]byte, error) {
if plugin.cmd.ProcessState != nil {
return nil, fmt.Errorf("process is already exited")
}
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.pipe_in); 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 *PluginRunning) runner(ctx context.Context) error {
max_concurrent := 1000
mut := sync.RWMutex{}
concurrent := 0
for {
select {
case <-ctx.Done():
return nil
case data := <-plugin.req_c:
mut.RLock()
if concurrent >= max_concurrent {
_ = writeAll(data.uuid, []byte("error: MAX CONCURRENT command reached"), COMMAND_ERROR, plugin.pipe_in)
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.pipe_in)
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(fmt.Sprintf("%v", err)), COMMAND_ERROR, plugin.pipe_in)
} else {
if err := writeAll(data.uuid, result, COMMAND_RESULT, plugin.pipe_in); err != nil {
_ = writeAll(data.uuid, []byte(fmt.Sprintf("error write: %v", err)), COMMAND_ERROR, plugin.pipe_in)
}
}
} else {
_ = writeAll(data.uuid, []byte("error func: "+input.Function+" not found"), COMMAND_ERROR, plugin.pipe_in)
}
}()
case <-plugin.heartbeat_c:
if err := writeHeartbeat(plugin.pipe_in); err != nil {
return err
}
}
}
}
func (plugin *PluginRunning) reader(ctx context.Context) error {
result := make(map[string][]byte)
for {
select {
case <-ctx.Done():
return nil
default:
read, err := readChunk(plugin.pipe_out)
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))
default:
return fmt.Errorf("invalid command")
}
}
}
}
func (plugin *PluginRunning) stderr(ctx context.Context) error {
buffer := make([]byte, 10)
result := []byte{}
for {
select {
case <-ctx.Done():
return nil
default:
n, err := plugin.pipe_err.Read(buffer)
if err != nil {
if err == io.EOF {
return fmt.Errorf("plugin %s stderr: \n%s", plugin.Name, string(result))
}
return fmt.Errorf("plugin %s failed to read stderr: %s", plugin.Name, err.Error())
}
result = append(result, buffer[0:n]...)
if n < 10 {
return fmt.Errorf("plugin %s stderr: \n%s", plugin.Name, string(result))
}
}
}
}