Skip to content

Commit

Permalink
fix(sys-server): fix memory overflow #33
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jan 13, 2022
1 parent 298239c commit 0e1b321
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 22 deletions.
5 changes: 2 additions & 3 deletions packages/system-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"keywords": [
"BaaS",
"cloud",
"mysql",
"acl",
"laf.js",
"serverless",
"less-api",
"laf",
"devops"
],
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/system-server/src/api/application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-28 22:00:45
* @LastEditTime: 2021-12-09 08:21:51
* @LastEditTime: 2022-01-13 13:48:28
* @Description: Application APIs
*/

Expand Down Expand Up @@ -222,5 +222,6 @@ export async function publishApplicationPackages(appid: string) {
logger.error(error)
} finally {
await session.endSession()
await app_accessor.close()
}
}
3 changes: 2 additions & 1 deletion packages/system-server/src/api/function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-12-28 20:37:17
* @LastEditTime: 2022-01-13 13:47:55
* @Description:
*/

Expand Down Expand Up @@ -107,6 +107,7 @@ export async function publishFunctions(app: ApplicationStruct) {
logger.error(error)
} finally {
await session.endSession()
await app_accessor.close()
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/system-server/src/api/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-12-28 20:36:07
* @LastEditTime: 2022-01-13 13:48:05
* @Description:
*/

Expand Down Expand Up @@ -70,6 +70,7 @@ export async function publishAccessPolicies(app: ApplicationStruct) {
})
} finally {
await session.endSession()
await app_accessor.conn.close()
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/system-server/src/lib/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export class ApplicationExporter {

this.zip.addFile(`collections/${co.name}.json`, this.json2buffer(data))
}

await accessor.close()
}

private json2buffer(data: Object) {
Expand Down
2 changes: 2 additions & 0 deletions packages/system-server/src/lib/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,7 @@ export class ApplicationImporter {

await db.createCollection(coll.name, coll.options)
await db.collection(coll.name).createIndexes(coll.indexes)

await accessor.close()
}
}
7 changes: 4 additions & 3 deletions packages/system-server/src/router/dbm/add-index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:26:26
* @LastEditTime: 2021-09-04 00:15:15
* @LastEditTime: 2022-01-13 13:55:07
* @Description:
*/

Expand Down Expand Up @@ -36,17 +36,18 @@ export async function handleCreateIndex(req: Request, res: Response) {
return res.status(422).send('invalid index spec')
}

const accessor = await getApplicationDbAccessor(app)
try {
const accessor = await getApplicationDbAccessor(app)
const r = await accessor.db
.collection(collectionName as string)
.createIndex(spec, {
background: true,
unique: unique as boolean
})

await accessor.close()
return res.send(r)
} catch (error) {
await accessor.close()
return res.status(400).send(error)
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/system-server/src/router/dbm/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:26:26
* @LastEditTime: 2021-10-14 14:03:32
* @LastEditTime: 2022-01-13 13:50:37
* @Description:
*/

Expand Down Expand Up @@ -34,8 +34,10 @@ export async function handleCreateCollection(req: Request, res: Response) {
const db = accessor.db
try {
await db.createCollection(collectionName)
await accessor.close()
return res.send({ code: 0, data: 'ok' })
} catch (error) {
await accessor.close()
return res.status(400).send({
error: error.message,
code: error.codeName
Expand Down
6 changes: 4 additions & 2 deletions packages/system-server/src/router/dbm/delete-index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:26:26
* @LastEditTime: 2021-09-04 00:14:35
* @LastEditTime: 2022-01-13 13:55:26
* @Description:
*/

Expand Down Expand Up @@ -34,14 +34,16 @@ export async function handleDeleteIndex(req: Request, res: Response) {
return res.status(422).send('invalid index name')
}

const accessor = await getApplicationDbAccessor(app)
try {
const accessor = await getApplicationDbAccessor(app)
const r = await accessor.db
.collection(collectionName as string)
.dropIndex(indexName as string)

await accessor.close()
return res.send(r)
} catch (error) {
await accessor.close()
return res.status(400).send(error)
}
}
4 changes: 3 additions & 1 deletion packages/system-server/src/router/dbm/get-indexes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:26:26
* @LastEditTime: 2021-09-04 00:14:53
* @LastEditTime: 2022-01-13 13:51:32
* @Description:
*/

Expand Down Expand Up @@ -32,5 +32,7 @@ export async function handleGetIndexesOfCollection(req: Request, res: Response)
const accessor = await getApplicationDbAccessor(app)

const r = await accessor.db.collection(collectionName as string).indexes()

await accessor.close()
return res.send(r)
}
4 changes: 3 additions & 1 deletion packages/system-server/src/router/dbm/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:26:26
* @LastEditTime: 2021-10-14 12:25:19
* @LastEditTime: 2022-01-13 13:51:41
* @Description:
*/

Expand All @@ -28,5 +28,7 @@ export async function handleCollectionList(req: Request, res: Response) {

const collections = await accessor.db.listCollections().toArray()
const result = collections.filter(coll => !coll.name.startsWith('__'))

await accessor.close()
return res.send(result)
}
4 changes: 3 additions & 1 deletion packages/system-server/src/router/dbm/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:26:26
* @LastEditTime: 2021-10-14 14:30:41
* @LastEditTime: 2022-01-13 13:52:09
* @Description:
*/

Expand Down Expand Up @@ -37,11 +37,13 @@ export async function handleDbProxy(req: Request, res: Response) {
try {
const data = await proxy.execute(params)

await accessor.close()
return res.send({
code: 0,
data
})
} catch (error) {
await accessor.close()
if (error.code === 121) {
const errs = error.errInfo?.details?.schemaRulesNotSatisfied
return res.send({
Expand Down
4 changes: 3 additions & 1 deletion packages/system-server/src/router/dbm/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:26:26
* @LastEditTime: 2021-10-14 14:27:34
* @LastEditTime: 2022-01-13 13:52:29
* @Description:
*/

Expand Down Expand Up @@ -52,8 +52,10 @@ export async function handleUpdateCollection(req: Request, res: Response) {

try {
await db.command(command)
await accessor.close()
return res.send({ code: 0, data: 'ok' })
} catch (error) {
await accessor.close()
return res.status(400).send({
error: error.message,
code: error.codeName
Expand Down
7 changes: 5 additions & 2 deletions packages/system-server/src/router/file/delete-file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 15:22:34
* @LastEditTime: 2021-09-03 20:34:59
* @LastEditTime: 2022-01-13 13:52:41
* @Description:
*/

Expand Down Expand Up @@ -29,19 +29,22 @@ export async function handleDeleteFile(req: Request, res: Response) {
return res.status(code).send()
}

const accessor = await getApplicationDbAccessor(app)

// delete file
try {
const accessor = await getApplicationDbAccessor(app)

const bucket = new GridFSBucket(accessor.db, { bucketName: bucket_name })
await bucket.delete(new ObjectId(file_id))

await accessor.close()
return res.send({
code: 0,
data: file_id
})
} catch (error) {
logger.error(requestId, `delete file ${file_id} in ${bucket_name} got error`, error)
await accessor.close()
return res.status(500).send('Internal Server Error')
}
}
6 changes: 4 additions & 2 deletions packages/system-server/src/router/file/get-files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 15:22:34
* @LastEditTime: 2021-09-03 20:35:20
* @LastEditTime: 2022-01-13 13:52:55
* @Description:
*/

Expand Down Expand Up @@ -31,8 +31,8 @@ export async function handleGetFiles(req: Request, res: Response) {
return res.status(code).send()
}

const accessor = await getApplicationDbAccessor(app)
try {
const accessor = await getApplicationDbAccessor(app)

const query = {}
if (keyword) {
Expand All @@ -52,6 +52,7 @@ export async function handleGetFiles(req: Request, res: Response) {
.sort('uploadDate', 'desc')
.toArray()

await accessor.close()
return res.send({
code: 0,
data: files,
Expand All @@ -61,6 +62,7 @@ export async function handleGetFiles(req: Request, res: Response) {
})
} catch (err) {
logger.error(requestId, `get files in ${bucket} got error`, err)
await accessor.close()
return res.status(500).send('Internal Server Error')
}
}
3 changes: 2 additions & 1 deletion packages/system-server/src/router/function/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-12-07 17:00:16
* @LastEditTime: 2022-01-13 13:53:49
* @Description:
*/

Expand Down Expand Up @@ -146,6 +146,7 @@ export async function handleGetPublishedFunctions(req: Request, res: Response) {
.find(query, {})
.toArray()

await accessor.close()
return res.send({
data: docs
})
Expand Down
4 changes: 3 additions & 1 deletion packages/system-server/src/router/function/logs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-10-06 23:42:50
* @LastEditTime: 2022-01-13 13:54:13
* @Description:
*/

Expand Down Expand Up @@ -58,6 +58,8 @@ export async function handleGetFunctionLogs(req: Request, res: Response) {
const total = await coll
.countDocuments(query)

await accessor.close()

return res.send({
data: docs,
total: total,
Expand Down

0 comments on commit 0e1b321

Please sign in to comment.