feat(stability): perPage 封顶 + statement_timeout + 连接池 5→20

防 perPage=999999 一次物化全表致 OOM/连接耗尽:
- Pagination.clampPerPage(上限 5000,安全护栏非产品限),接入 FlowController/PickerController 两 REST 入口(page 不封)
- application.yml 加 hikari.connection-init-sql=SET statement_timeout='${EIP4_DB_STATEMENT_TIMEOUT:120s}'(dev/local/prod 继承 base,可覆盖/设0关闭)
- maximum-pool-size 5→20(prod 无 profile 继承 base 受益)
验证:68/68 绿(PaginationTest+经真实 executeGet 集成+无回归);statement_timeout 经真实 PG SHOW=2min。见总览 §10.2。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-24 10:36:41 +08:00
parent ffe89e62bf
commit 0106088084
6 changed files with 91 additions and 4 deletions

View File

@@ -10,9 +10,15 @@ spring:
username: ceshi username: ceshi
password: ceshi password: ceshi
hikari: hikari:
maximum-pool-size: 5 # 基础默认池(总览 §10.2)。default/prod profile 无独立 yml 时继承本值;
# dev/local 各自覆盖为更小值。生产高负载可经独立 prod profile 或环境进一步上调。
maximum-pool-size: 20
# PoC 阶段对共享测试库默认只读,对拍写操作需显式改 false # PoC 阶段对共享测试库默认只读,对拍写操作需显式改 false
read-only: false read-only: false
# 慢查询熔断(总览 §10.2):杀死单条运行超时的 SQL,防失控/恶意查询(如超大 perPage 全表扫)
# 长期占用连接、与小池叠加拖垮实例。会话级,随连接复用持续生效;dev/local 继承本项。
# 默认 120s 对 OLTP 足够宽,重报表/批量场景经环境变量放宽,设 0 关闭。
connection-init-sql: "SET statement_timeout = '${EIP4_DB_STATEMENT_TIMEOUT:120s}'"
server: server:
port: 8084 port: 8084

View File

@@ -300,7 +300,8 @@ public class FlowController {
return; return;
} }
int page = parsePositiveInt(map.remove("page"), 1); int page = parsePositiveInt(map.remove("page"), 1);
int perPage = parsePositiveInt(map.remove("perPage"), DEFAULT_PER_PAGE); // perPage 封顶(总览 §10.2):防 perPage=999999 一次物化全表致 OOM/连接耗尽;page 不封(深翻页合法)
int perPage = Pagination.clampPerPage(parsePositiveInt(map.remove("perPage"), DEFAULT_PER_PAGE));
map.put(POJOCRUDService.PAGE_START, (page - 1) * perPage); map.put(POJOCRUDService.PAGE_START, (page - 1) * perPage);
map.put(POJOCRUDService.PAGE_SIZE, perPage); map.put(POJOCRUDService.PAGE_SIZE, perPage);
} }

View File

@@ -0,0 +1,25 @@
package com.eip4.platform.metadata.web;
/**
* 分页参数安全护栏(总览 §10.2 稳定性)。
*
* <p>仅作「安全上限」用途——防止客户端传 {@code perPage=999999} 让引擎一次把全表物化进内存,
* 与小连接池叠加即可<em>单请求</em>耗尽连接 / OOM。这<strong>不是</strong>产品级分页上限:
* 交互式大数据集应走服务端搜索({@code q});超大导出应走流式通道(未来工作)。
*/
final class Pagination {
/** 每页条数硬上限:远超任何真实页大小(amis 默认 10 / picker 20),远低于 OOM 阈值。 */
static final int MAX_PER_PAGE = 5000;
private Pagination() {
}
/** 把每页条数夹到 {@code [1, MAX_PER_PAGE]};用于所有 REST 分页入口。 */
static int clampPerPage(int perPage) {
if (perPage > MAX_PER_PAGE) {
return MAX_PER_PAGE;
}
return Math.max(perPage, 1);
}
}

View File

@@ -174,8 +174,10 @@ public class PickerController {
: buildCtx(asUser, roles, asDepart, asOrg); : buildCtx(asUser, roles, asDepart, asOrg);
Map<String, Object> cond = new HashMap<>(); Map<String, Object> cond = new HashMap<>();
cond.put(POJOCRUDService.PAGE_START, (page - 1) * perPage); // perPage 封顶(总览 §10.2):防客户端传超大 perPage 一次拉全表致 OOM/连接耗尽
cond.put(POJOCRUDService.PAGE_SIZE, perPage); int safePerPage = Pagination.clampPerPage(perPage);
cond.put(POJOCRUDService.PAGE_START, (page - 1) * safePerPage);
cond.put(POJOCRUDService.PAGE_SIZE, safePerPage);
// 模糊搜索:仅当有关键字且存在可读标识列时下推 LIKE。q 经转义后进引擎(引擎对 String 条件裸拼 // 模糊搜索:仅当有关键字且存在可读标识列时下推 LIKE。q 经转义后进引擎(引擎对 String 条件裸拼
// col like '%值%',转义单引号与 LIKE 通配符,杜绝注入与通配符逃逸)。标识列名本身来自元数据,天然合法。 // col like '%值%',转义单引号与 LIKE 通配符,杜绝注入与通配符逃逸)。标识列名本身来自元数据,天然合法。
if (q != null && !q.isBlank() && labelColumn != null) { if (q != null && !q.isBlank() && labelColumn != null) {

View File

@@ -616,6 +616,26 @@ class FlowControllerTest {
assertEquals("放假", sent.get("TITLE"), "其它 query 参数仍作过滤条件保留"); assertEquals("放假", sent.get("TITLE"), "其它 query 参数仍作过滤条件保留");
} }
@Test
@DisplayName("14. perPage 超大封顶: 防一次拉全表致 OOM/连接耗尽(总览 §10.2)")
void clampsExcessivePerPage() {
String flowId = "PM_PROJECT_SEARCH";
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
FlowController controller = new FlowController(engine, resolverReturning(flowId, synthesized(flowId)));
Map<String, String> params = new HashMap<>();
params.put("perPage", "999999"); // 恶意/失误的超大每页
controller.executeGet(flowId, params);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId));
Map<String, Object> sent = mapCap.getValue();
assertEquals(Pagination.MAX_PER_PAGE, sent.get(POJOCRUDService.PAGE_SIZE),
"perPage 超上限应被夹到 MAX_PER_PAGE,不得原样下传引擎");
}
@Test @Test
@DisplayName("13. 分页缺省: 只给 page,perPage 回落 amis 默认 10;page 缺省 1") @DisplayName("13. 分页缺省: 只给 page,perPage 回落 amis 默认 10;page 缺省 1")
void pagingDefaults() { void pagingDefaults() {

View File

@@ -0,0 +1,33 @@
package com.eip4.platform.metadata.web;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** 分页护栏单测(总览 §10.2):封顶 perPage,挡住一次拉全表的 OOM/DoS 向量。 */
class PaginationTest {
@Test
@DisplayName("超大 perPage 夹到上限")
void clampsExcessive() {
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(999999));
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(Integer.MAX_VALUE));
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(Pagination.MAX_PER_PAGE + 1));
}
@Test
@DisplayName("正常与边界值原样保留")
void keepsNormal() {
assertEquals(10, Pagination.clampPerPage(10));
assertEquals(20, Pagination.clampPerPage(20));
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(Pagination.MAX_PER_PAGE));
}
@Test
@DisplayName("非正数夹到 1,避免 PAGE_SIZE<=0 的退化语义")
void clampsNonPositive() {
assertEquals(1, Pagination.clampPerPage(0));
assertEquals(1, Pagination.clampPerPage(-1));
}
}