-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmsg.js
More file actions
108 lines (92 loc) · 2.6 KB
/
msg.js
File metadata and controls
108 lines (92 loc) · 2.6 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
const Koa = require('koa')
const koaBody = require('koa-body')
const Router = require('@koa/router')
const dtmcli = require('dtmcli')
const { getDB, initModel, getUserAccount } = require('./db')
const args = process.argv.slice(2)
const localTx = args.includes('local')
const dtm = "http://localhost:36789/api/dtmsvr" // dtm服务地址
const svc = "http://localhost:4005/api" // 本地服务前缀
const transOutUid = '1'
const transInUid = '2'
async function FireMsg() {
let req = { amount: 30 } // 子事务需要的负荷
const gid = await dtmcli.mustGenGid(dtm)
const msg = new dtmcli.Msg(dtm, gid).add(`${svc }/TransOut`, req).add(`${svc }/TransIn`, req)
await msg.prepare(`${svc }/query`)
await msg.submit()
}
async function FireMsgWithLocalTransaction() {
const req = { amount: 30 }
const gid = await dtmcli.mustGenGid(dtm)
const seuqelize = await getDB()
const msg = new dtmcli.Msg(dtm, gid).add(`${svc }/TransIn`, req)
await msg.doAndSubmitDB(`${svc }/query`, seuqelize, async (tx) => {
await transUserBalance(tx, transOutUid, -req.amount)
})
}
async function transUserBalance(tx, uid, amount) {
const account = await getUserAccount().findOne(
{
where: {
userId: uid,
},
transaction: tx,
}
)
if (!account) {
throw new Error('not found')
}
await account.increment({
balance: amount,
})
}
const app = new Koa()
const router = new Router()
router
.get('/api/query', (ctx, next) => {
console.log('query prepare')
ctx.body = { dtm_result: 'SUCCESS' }
})
.post('/api/TransOut', async (ctx, next) => {
console.log('TransOut', ctx.request.body.amount)
await transUserBalance(undefined, transOutUid, -ctx.request.body.amount)
ctx.body = { dtm_result: 'SUCCESS' }
})
.post('/api/TransIn', async (ctx, next) => {
console.log('TransIn', ctx.request.body.amount)
await transUserBalance(undefined, transInUid, ctx.request.body.amount)
ctx.body = { dtm_result: 'SUCCESS' }
})
.get('/api/FireMsg', async (ctx, next) => {
await FireMsg()
ctx.body = { dtm_result: 'SUCCESS' }
})
.get('/api/FireLocalMsg', async (ctx, next) => {
await FireMsgWithLocalTransaction()
ctx.body = { dtm_result: 'SUCCESS' }
})
app
.use(
koaBody({
jsonLimit: '100kb',
})
)
.use(router.routes())
.use(router.allowedMethods())
async function startup() {
const sequelize = await getDB()
initModel(sequelize)
dtmcli.init({
sequelize,
})
}
startup().then(() => {
app.listen(4005, async () => {
if (localTx) {
await FireMsgWithLocalTransaction()
} else {
await FireMsg()
}
})
})