-
Notifications
You must be signed in to change notification settings - Fork 0
/
handleMessage.js
145 lines (116 loc) · 3.75 KB
/
handleMessage.js
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
import protobuf from 'protobufjs'
import murmurhash from 'murmurhash'
import matchOrder from './matchOrder'
export const messageTypes = {
0: 'order',
1: 'query',
2: 'view',
}
const createMessageHandler = (uid, redisClient) => async (message) => {
const [messageType, data] = message.split('|')
const proto = await protobuf.load('src/proto/order.proto')
const type = messageTypes[messageType]
if (process.env.LOG_ORDERS) console.log(`message:${uid}:${type}`)
try {
switch (type) {
case 'order':
return await handleOrder({ data, uid, proto, redisClient })
case 'query':
return await handleQuery({ data, uid, proto, redisClient })
case 'view':
return await handleView({ data, uid, proto, redisClient })
default:
return {
type: 'unknown',
error: `Message type “${messageType}” unknown`,
}
}
} catch (e) {
return {
type,
error: `Failed to handle message. Actual error: ${e.message}`,
}
}
}
const handleOrder = async ({ data, uid, proto, redisClient }) => {
const OrderMessage = proto.lookupType('orderbook.Order')
const buf = Buffer.from(data, 'base64')
const decoded = OrderMessage.decode(buf)
const message = OrderMessage.toObject(decoded)
if (uid !== message.uid) {
console.log('message rejected: uid mismatch')
return {
type: 'order',
error: 'Connected user ID does not match message user ID.',
}
}
await redisClient.INCR('TOTAL_ORDERS')
const { side, symbol, price } = message
const ts = Date.now()
const orderString = `${side}:${symbol}@${price}`
const hash = murmurhash.v3(`${orderString}-${uid}-${ts}`).toString(16)
if (process.env.LOG_ORDERS) console.log(`order:${uid}:${hash}:${orderString}`)
const matchedOrder = await matchOrder({ side, symbol, price, redisClient })
if (matchedOrder) {
await redisClient.INCRBY('TOTAL_MATCHED', 2)
return {
type: 'match',
message: 'Order matched',
data: {
matchedBy: matchedOrder.uid,
matchedAt: Date.now(),
yourOrder: { order: orderString, uid, ts, hash },
matchedOrder,
},
}
} else {
await redisClient.RPUSH(orderString, JSON.stringify({ uid, ts, hash }))
return {
type: 'order',
message: 'Order submitted to queue',
data: {
order: orderString,
uid,
ts,
hash,
},
}
}
}
const handleQuery = async ({ data, uid, proto, redisClient }) => {
const OrderMessage = proto.lookupType('orderbook.Query')
const buf = Buffer.from(data, 'base64')
const decoded = OrderMessage.decode(buf)
const message = OrderMessage.toObject(decoded)
if (uid !== message.uid) {
console.log('message rejected: uid mismatch')
return { type: 'order', error: 'User IDs do not match' }
}
const { side, symbol } = message
const keys = await redisClient.KEYS(`${side}:${symbol}*`)
const lists = {}
for (const key of keys) {
const [, price] = key.split('@')
lists[price] = await redisClient.LLEN(key)
}
return { type: 'query', data: lists }
}
const handleView = async ({ data, uid, proto, redisClient }) => {
const OrderMessage = proto.lookupType('orderbook.View')
const buf = Buffer.from(data, 'base64')
const decoded = OrderMessage.decode(buf)
const message = OrderMessage.toObject(decoded)
if (uid !== message.uid) {
console.log('message rejected: uid mismatch')
return { type: 'order', error: 'User IDs do not match' }
}
const { side, symbol, price, start, stop } = message
const orders = await redisClient.LRANGE(
`${side}:${symbol}@${price}`,
start,
stop
)
console.log('ORDERS:', orders)
return { type: 'query', data: orders.map((o) => JSON.parse(o)) }
}
export default createMessageHandler