Skip to content

Commit

Permalink
Merge pull request #78 from cnlkl/issue_77
Browse files Browse the repository at this point in the history
feat: 支持限制扫描器可用内存 #77
  • Loading branch information
owenlxu committed Nov 28, 2022
2 parents 3ac8c0e + 486d09d commit d3399b3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DockerScanHelper(
val maxScanDuration = task.scanner.maxScanDuration(scannerInputFile.length())
// 创建容器
val maxFileSize = maxFileSize(scannerInputFile.length())
val hostConfig = DockerUtils.dockerHostConfig(binds, maxFileSize)
val hostConfig = DockerUtils.dockerHostConfig(binds, maxFileSize, task.scanner.memory)
val containerId = dockerClient.createContainer(image, hostConfig, args)

taskContainerIdMap[task.taskId] = containerId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ object DockerUtils {
fun dockerHostConfig(
binds: Binds,
maxSize: Long,
mem: Long,
withPrivileged: Boolean = false
): HostConfig {
return HostConfig().apply {
Expand All @@ -114,6 +115,7 @@ object DockerUtils {
// 降低容器CPU优先级,限制可用的核心,避免调用DockerDaemon获其他系统服务时超时
withCpuShares(CONTAINER_CPU_SHARES)
withPrivileged(withPrivileged)
withMemory(mem)
val processorCount = Runtime.getRuntime().availableProcessors()
if (processorCount > 2) {
withCpusetCpus("0-${processorCount - 2}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ open class Scanner(
@ApiModelProperty("支持扫描的包类型")
val supportPackageTypes: List<String> = emptyList(),
@ApiModelProperty("支持扫描的类型")
val supportScanTypes: List<String> = emptyList()
val supportScanTypes: List<String> = emptyList(),
@ApiModelProperty("执行扫描所需要的内存大小")
val memory: Long = DEFAULT_MEM
) {
/**
* 获取待扫描文件最大允许扫描时长
Expand Down Expand Up @@ -101,5 +103,9 @@ open class Scanner(
* 默认至少允许扫描的时间
*/
private const val DEFAULT_MIN_SCAN_DURATION = 3 * 60L * 1000L
/**
* 默认内存大小
*/
private const val DEFAULT_MEM = 32L * 1024L * 1024L * 1024L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<el-form-item label="扫描结束后是否清理" prop="cleanWorkDir" required>
<el-switch v-model="scanner.cleanWorkDir" />
</el-form-item>
<el-form-item label="执行扫描所需内存" prop="memory" required>
<el-input v-model.number="scanner.memory" type="number">
<template slot="append">Byte</template>
</el-input>
</el-form-item>
<el-form-item label="1MB最大允许扫描时间" prop="maxScanDurationPerMb" required>
<el-input v-model.number="scanner.maxScanDurationPerMb" type="number">
<template slot="append">ms</template>
Expand Down Expand Up @@ -297,6 +302,7 @@ export default {
description: '',
rootPath: type,
cleanWorkDir: true,
memory: 32 * 1024 * 1024 * 1024,
maxScanDurationPerMb: 6000,
supportFileNameExt: [],
supportPackageTypes: [],
Expand Down

0 comments on commit d3399b3

Please sign in to comment.