Skip to content

Commit

Permalink
Merge pull request #9 from lannoy0523/issue_2655
Browse files Browse the repository at this point in the history
feat: 后台增加限流配置页面 #2655
  • Loading branch information
zacYL authored Oct 24, 2024
2 parents 46ee8da + 3bff4fd commit bea9449
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ class RateLimiterAutoConfiguration {
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
): UrlRepoRateLimiterService {
return UrlRepoRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService)
return UrlRepoRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
)
}

@Bean(URL_RATELIMITER_SERVICE)
Expand All @@ -90,7 +96,13 @@ class RateLimiterAutoConfiguration {
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
): UrlRateLimiterService {
return UrlRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService)
return UrlRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
)
}

@Bean(USER_URL_REPO_RATELIMITER_SERVICE)
Expand All @@ -101,7 +113,13 @@ class RateLimiterAutoConfiguration {
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
): UserUrlRepoRateLimiterService {
return UserUrlRepoRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService)
return UserUrlRepoRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
)
}

@Bean(UPLOAD_USAGE_RATELIMITER_SERVICE)
Expand All @@ -112,7 +130,13 @@ class RateLimiterAutoConfiguration {
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
): UploadUsageRateLimiterService {
return UploadUsageRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService)
return UploadUsageRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
)
}

@Bean(DOWNLOAD_USAGE_RATELIMITER_SERVICE)
Expand All @@ -123,7 +147,13 @@ class RateLimiterAutoConfiguration {
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
): DownloadUsageRateLimiterService {
return DownloadUsageRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService)
return DownloadUsageRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
)
}

@Bean(USER_DOWNLOAD_USAGE_RATELIMITER_SERVICE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ class RateLimitRepository : SimpleMongoDao<TRateLimit>() {
)
}

fun findByModuleName(moduleName: String): List<TRateLimit> {
fun findByModuleNameAndLimitDimension(moduleName: String, limitDimension: String): List<TRateLimit> {
return find(
Query(
Criteria.where(TRateLimit::moduleName.name).regex("$moduleName")
.and(TRateLimit::limitDimension.name).isEqualTo(limitDimension)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ abstract class AbstractBandwidthRateLimiterService(
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterProperties: RateLimiterProperties,
rateLimiterConfigService: RateLimiterConfigService,
) : AbstractRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : AbstractRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {

override fun limit(request: HttpServletRequest, applyPermits: Long?) {
throw UnsupportedOperationException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import com.tencent.bkrepo.common.ratelimiter.interceptor.RateLimiterInterceptor
import com.tencent.bkrepo.common.ratelimiter.interceptor.RateLimiterInterceptorChain
import com.tencent.bkrepo.common.ratelimiter.interceptor.TargetRateLimiterInterceptorAdaptor
import com.tencent.bkrepo.common.ratelimiter.metrics.RateLimiterMetrics
import com.tencent.bkrepo.common.ratelimiter.model.TRateLimit
import com.tencent.bkrepo.common.ratelimiter.rule.RateLimitRule
import com.tencent.bkrepo.common.ratelimiter.rule.bandwidth.DownloadBandwidthRateLimitRule
import com.tencent.bkrepo.common.ratelimiter.rule.bandwidth.UploadBandwidthRateLimitRule
Expand Down Expand Up @@ -315,12 +314,13 @@ abstract class AbstractRateLimiterService(
val usageRuleConfigs = rateLimiterProperties.rules.filter {
it.limitDimension in getLimitDimensions()
}
val databaseConfig =
try {
rateLimiterConfigService.findByModuleName(moduleName)
} catch (ex: Exception) {
rateLimiterConfigService.list()
}
val databaseConfig = try {
rateLimiterConfigService.findByModuleNameAndLimitDimension(
moduleName, getLimitDimensions().first())
} catch (e: Exception) {
logger.error("system error: $e")
listOf()
}
val configs = usageRuleConfigs.plus(databaseConfig.map { tRateLimit -> ResourceLimit(
algo = tRateLimit.algo,
resource = tRateLimit.resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ open class UploadBandwidthRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
) : AbstractBandwidthRateLimiterService(taskScheduler, rateLimiterMetrics, redisTemplate, rateLimiterProperties, rateLimiterConfigService) {
) : AbstractBandwidthRateLimiterService(
taskScheduler,
rateLimiterMetrics,
redisTemplate,
rateLimiterProperties,
rateLimiterConfigService
) {

override fun buildResource(request: HttpServletRequest): String {
val (projectId, repoName) = getRepoInfoFromAttribute(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ class UrlRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
) : AbstractRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : AbstractRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {


override fun buildResource(request: HttpServletRequest): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ class UrlRepoRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
) : AbstractRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : AbstractRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {

override fun ignoreRequest(request: HttpServletRequest): Boolean {
if (rateLimiterProperties.specialUrls.contains(StringPool.POUND)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ class UserUrlRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService,
) : AbstractRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : AbstractRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {

override fun buildResource(request: HttpServletRequest): String {
val userId = HttpContextHolder.getRequestOrNull()?.getAttribute(USER_KEY) as? String ?: ANONYMOUS_USER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ class UserUrlRepoRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
private val rateLimiterConfigService: RateLimiterConfigService
) : AbstractRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : AbstractRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {

override fun ignoreRequest(request: HttpServletRequest): Boolean {
if (rateLimiterProperties.specialUrls.contains(StringPool.POUND)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ open class UploadUsageRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService,
) : AbstractRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : AbstractRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {

override fun buildResource(request: HttpServletRequest): String {
val (projectId, repoName) = getRepoInfoFromAttribute(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ class UserDownloadUsageRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
) : UserUploadUsageRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : UserUploadUsageRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {

override fun getApplyPermits(request: HttpServletRequest, applyPermits: Long?): Long {
if (applyPermits == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ open class UserUploadUsageRateLimiterService(
rateLimiterMetrics: RateLimiterMetrics,
redisTemplate: RedisTemplate<String, String>? = null,
rateLimiterConfigService: RateLimiterConfigService
) : AbstractRateLimiterService(taskScheduler, rateLimiterProperties, rateLimiterMetrics, redisTemplate, rateLimiterConfigService) {
) : AbstractRateLimiterService(
taskScheduler,
rateLimiterProperties,
rateLimiterMetrics,
redisTemplate,
rateLimiterConfigService
) {

override fun buildResource(request: HttpServletRequest): String {
val (projectId, repoName) = getRepoInfoFromAttribute(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class RateLimiterConfigService(
}
}

fun findByModuleName(moduleName: String): List<TRateLimit> {
return rateLimitRepository.findByModuleName(moduleName)
fun findByModuleNameAndLimitDimension(moduleName: String, limitDimension: String): List<TRateLimit> {
return rateLimitRepository.findByModuleNameAndLimitDimension(moduleName, limitDimension)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.tencent.bkrepo.common.ratelimiter.enums.LimitDimension
import com.tencent.bkrepo.common.ratelimiter.enums.WorkScope
import com.tencent.bkrepo.common.ratelimiter.exception.InvalidResourceException
import com.tencent.bkrepo.common.ratelimiter.metrics.RateLimiterMetrics
import com.tencent.bkrepo.common.ratelimiter.repository.RateLimitRepository
import com.tencent.bkrepo.common.ratelimiter.rule.common.ResInfo
import com.tencent.bkrepo.common.ratelimiter.rule.common.ResourceLimit
import com.tencent.bkrepo.common.ratelimiter.service.user.RateLimiterConfigService
Expand All @@ -68,13 +69,17 @@ open class AbstractRateLimiterServiceTest : DistributedTest() {
@MockBean
private lateinit var meterRegistry: MeterRegistry

@MockBean
private lateinit var rateLimitRepository: RateLimitRepository

fun init() {
request = MockHttpServletRequest()
val scheduler = ThreadPoolTaskScheduler()
scheduler.initialize()
taskScheduler = scheduler
rateLimiterProperties = RateLimiterProperties()
rateLimiterMetrics = RateLimiterMetrics(meterRegistry)
rateLimiterConfigService = RateLimiterConfigService(rateLimitRepository)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.tencent.bkrepo.common.api.util.toJsonString
import com.tencent.bkrepo.common.ratelimiter.config.RateLimiterProperties
import com.tencent.bkrepo.common.ratelimiter.model.RateLimitCreatOrUpdateRequest
import com.tencent.bkrepo.common.ratelimiter.model.TRateLimit
import com.tencent.bkrepo.common.ratelimiter.rule.common.ResourceLimit
import com.tencent.bkrepo.common.ratelimiter.service.user.RateLimiterConfigService
import com.tencent.bkrepo.common.security.permission.Principal
import com.tencent.bkrepo.common.security.permission.PrincipalType
Expand All @@ -20,7 +19,6 @@ import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.PathVariable
import java.time.Duration

@RestController
@RequestMapping("/api/rateLimit")
Expand All @@ -37,27 +35,25 @@ class RateLimitController(

@PostMapping("/update")
fun update(@RequestBody request: RateLimitCreatOrUpdateRequest): Response<Void> {
with(request) {
if (id.isNullOrBlank()) {
throw NotFoundException(CommonMessageCode.PARAMETER_EMPTY, "ID")
}
if (!rateLimiterConfigService.checkExist(id!!)) {
throw NotFoundException(CommonMessageCode.RESOURCE_NOT_FOUND, id!!)
}
rateLimiterConfigService.getById(id!!).let {
if (it != null &&
(!it.resource.equals(resource) || !it.limitDimension.equals(limitDimension)) &&
rateLimiterConfigService.checkExist(request)
) {
throw ErrorCodeException(
CommonMessageCode.RESOURCE_EXISTED,
"resource:$resource,limitDimension:$limitDimension"
)
}
if (request.id.isNullOrBlank()) {
throw NotFoundException(CommonMessageCode.PARAMETER_EMPTY, "ID")
}
if (!rateLimiterConfigService.checkExist(request.id!!)) {
throw NotFoundException(CommonMessageCode.RESOURCE_NOT_FOUND, request.id!!)
}
rateLimiterConfigService.getById(request.id!!).let {
if (it != null &&
(!it.resource.equals(request.resource) || !it.limitDimension.equals(request.limitDimension)) &&
rateLimiterConfigService.checkExist(request)
) {
throw ErrorCodeException(
CommonMessageCode.RESOURCE_EXISTED,
"resource:${request.resource},limitDimension:${request.limitDimension})"
)
}
rateLimiterConfigService.update(request)
return ResponseBuilder.success()
}
rateLimiterConfigService.update(request)
return ResponseBuilder.success()
}

// 新增
Expand Down
18 changes: 12 additions & 6 deletions src/frontend/devops-op/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,6 @@ export const asyncRoutes = [
meta: { title: '缓存管理', icon: 'file' },
component: () => import('@/views/node/FileCache')
},
{
path: 'rateLimiter',
name: ROUTER_NAME_RATE_LIMITER_CONFIG,
meta: { title: '文件限流配置', icon: 'file' },
component: () => import('@/views/node/RateLimiter')
},
{
path: 'fileSystem',
name: ROUTER_NAME_FILE_SYSTEM,
Expand Down Expand Up @@ -320,6 +314,18 @@ export const asyncRoutes = [
}
]
},
{
path: '/rateLimiter',
component: Layout,
children: [
{
path: '/',
name: ROUTER_NAME_RATE_LIMITER_CONFIG,
meta: { title: '限流管理', icon: 'permission' },
component: () => import('@/views/rateLimitConfg/RateLimiter')
}
]
},
// 404 page must be placed at the end !!!
{ path: '*', redirect: '/404', hidden: true }
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
</template>
<script>
import CreateOrUpdateRateLimitDialog from '@/views/node/components/CreateOrUpdateRateLimitDialog'
import CreateOrUpdateRateLimitDialog from '@/views/rateLimitConfg/components/CreateOrUpdateRateLimitDialog'
import { getRateLimitConfig, queryRateLimits, deleteRateLimit } from '@/api/rateLimit'
export default {
Expand Down
Loading

0 comments on commit bea9449

Please sign in to comment.