-
Notifications
You must be signed in to change notification settings - Fork 0
/
geek.go
294 lines (271 loc) · 5.93 KB
/
geek.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package geek
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"os"
"syscall"
"time"
)
var DefaultLogWriter io.Writer = os.Stdout
type QProcess struct {
Host string
Port int
User string
Password string
Timeout time.Duration
TLSConfig *tls.Config
conn net.Conn
writer *bufio.Writer
reader *bufio.Reader
}
var (
ErrAuth = errors.New("geek: wrong credential?")
ErrVersion = errors.New("geek: require version 3+ kdb+")
ErrNotConnected = errors.New("geek: not connected")
ErrMaxRetryTimesReached = errors.New("geek: maximum retry times reached")
ErrNotAllowedAPI = errors.New("geek: not allowed API")
ErrInvalidQuery = errors.New("geek: first item is not a symbol or null symbol")
)
// var names used on the q processes
const (
GeekUser string = ".geek.user"
GeekIP string = ".geek.ip"
)
type GeekErr struct {
Msg string
}
func (ge GeekErr) Error() string {
return ge.Msg
}
func (q *QProcess) Close() {
if q.conn != nil {
q.conn.Close()
q.conn = nil
}
}
func (q QProcess) IsConnected() bool {
return q.conn != nil
}
func (q *QProcess) Sync(k interface{}, args interface{}) error {
if q.IsConnected() {
err := writeIPC(q.writer, SYNC, args)
if err != nil {
if errors.Is(err, syscall.EPIPE) {
q.Close()
}
return err
}
// not from a localhost, the message could be compressed
if len(q.Host) > 0 {
size, peekErr := q.peekMsgLength()
if peekErr != nil {
q.Close()
return peekErr
}
// for retrying
input := make([]byte, size)
io.ReadFull(q.reader, input)
// decompress the message and create a bufio reader
err = readIPC(bufio.NewReader(bytes.NewReader(Decompress(input))), k)
} else {
err = readIPC(q.reader, k)
}
if err != nil {
// close when connection reset by peer or EOF error
if errors.Is(err, syscall.ECONNRESET) || errors.Is(err, io.EOF) {
q.Close()
}
return err
}
return nil
} else {
return ErrNotConnected
}
}
func (q *QProcess) Async(args interface{}) error {
if q.IsConnected() {
err := writeIPC(q.writer, ASYNC, args)
if err != nil {
if errors.Is(err, syscall.EPIPE) {
q.Close()
}
return err
}
return nil
} else {
return ErrNotConnected
}
}
func (q *QProcess) Discard() error {
msgLength, err := q.peekMsgLength()
if err != nil {
return err
}
_, err = q.reader.Discard(int(msgLength))
return err
}
func (q *QProcess) Err(err error) error {
if q.IsConnected() {
err := writeErr(q.writer, err)
if err != nil {
if errors.Is(err, syscall.EPIPE) {
q.Close()
}
return err
}
return nil
} else {
return ErrNotConnected
}
}
func (q *QProcess) auth() error {
buf := bytes.NewBufferString(q.User + ":" + q.Password)
// test with v3 capability (compression, timestamp, timespan, uuid)
buf.WriteByte(3)
buf.WriteByte(0)
_, err := q.conn.Write(buf.Bytes())
if err != nil {
q.conn.Close()
return err
}
ipcVersion := make([]byte, 1)
_, err = q.conn.Read(ipcVersion)
if err != nil {
q.conn.Close()
return ErrAuth
}
if ipcVersion[0] < 3 {
return ErrVersion
}
return nil
}
func (q *QProcess) Dial() error {
socket := fmt.Sprintf("%s:%d", q.Host, q.Port)
var conn net.Conn
var err error
if q.TLSConfig != nil {
conn, err = tls.Dial("tcp", socket, q.TLSConfig)
} else {
conn, err = net.DialTimeout("tcp", socket, q.Timeout)
}
if err != nil {
return err
}
q.conn = conn
err = q.auth()
if err != nil {
q.conn = nil
return err
}
conn.(*net.TCPConn).SetKeepAlive(true)
q.reader = bufio.NewReader(conn)
q.writer = bufio.NewWriter(conn)
return nil
}
type Authenticator func(string, string) error
type HandlerFunc func(*ConnPool, *QProcess)
type Engine struct {
Port int
Auth Authenticator
Pool *ConnPool
Handler HandlerFunc
}
func DefaultEngine(pool *ConnPool) *Engine {
noAuth := func(user string, password string) error {
return nil
}
return &Engine{
Port: 8101,
Auth: noAuth,
Pool: pool,
Handler: LoggerWithConfig(LoggerConfig{}),
}
}
func (e *Engine) Run() error {
if e.Pool == nil {
return &GeekErr{"QEngine:Run nil Pool"}
}
if e.Handler == nil {
e.Handler = LoggerWithConfig(LoggerConfig{})
}
socket := fmt.Sprintf(":%d", e.Port)
listener, err := net.Listen("tcp", socket)
if err != nil {
return err
}
for {
conn, err := listener.Accept()
if err != nil {
return &GeekErr{fmt.Sprintf("QEngine:Run Failed to accept connection %s", err)}
}
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
credentials, _ := reader.ReadBytes(0x00)
var colonPos int
for i, c := range credentials {
if c == ':' {
colonPos = i
break
}
}
var user, password string
if colonPos == 0 {
user = string(credentials[:len(credentials)-2])
} else {
user = string(credentials[:colonPos-1])
password = string(credentials[colonPos+1 : len(credentials)-2])
}
err = e.Auth(user, password)
if err != nil {
conn.Close()
} else {
conn.Write([]byte{0x03})
qClient := &QProcess{conn: conn, reader: reader, writer: writer, User: user}
go e.Handler(e.Pool, qClient)
}
}
}
// peek the first symbol(API) in the incoming message
func PeekAPI(msg []byte) string {
var startIndex int
if len(msg) > 9 && msg[8] == 245 {
// a symbol
startIndex = 9
} else if len(msg) > 14 && msg[8] == 11 {
// symbol list
startIndex = 14
} else if len(msg) > 15 && msg[14] == 245 {
// mixed list
startIndex = 15
} else {
return ""
}
var endIndex int
for i := startIndex; i < len(msg); i++ {
if msg[i] == 0 {
endIndex = i
break
}
}
if endIndex == startIndex {
return ""
}
return string(msg[startIndex:endIndex])
}
// int64 is for io.copyN
func (q *QProcess) peekMsgLength() (int64, error) {
header, err := q.reader.Peek(8)
if err != nil {
if errors.Is(err, syscall.ECONNRESET) || errors.Is(err, io.EOF) {
q.Close()
}
return 0, err
}
return int64(binary.LittleEndian.Uint32(header[4:8])), nil
}