Skip to content

Neton HTTP 规范

定位:描述 Neton HTTP 抽象层的架构、生命周期与实现边界。与 Neton-Core-Spec 第六、七节一致,并落地 neton-core(抽象)与 neton-http(实现)的职责划分。

参考:历史 HTTP 抽象层与适配器设计文档(设计目标与优化方向);本文以当前实现为准,差异在文中注明。


一、概述与分层

1.1 设计目标

  • 服务器无关性:Core 只定义 HttpAdapter / HttpContext / Request·Response,实现可替换。
  • 类型安全:参数与返回值在 Core 层有明确接口(HandlerArgs、ParamConverter、RouteHandler 返回 Any?)。
  • 请求生命周期:每个请求对应一个 HttpContext,带 traceId,与 LogContext 一致,便于 APM/日志串联。
  • Native-first:抽象层无 JVM 专有依赖;neton-http 运行于 Native 目标。

1.2 四层关系

┌─────────────────────────────────────────────────────────┐
│  Neton 应用层(Neton.run、install、onStart)             │  ← neton-core
├─────────────────────────────────────────────────────────┤
│  HttpContext 抽象(request/response/session/attributes)  │  ← neton-core
├─────────────────────────────────────────────────────────┤
│  HttpAdapter(start/stop/port)+ RequestEngine 驱动路由   │  ← neton-core 接口,neton-http 实现
├─────────────────────────────────────────────────────────┤
│  服务器实现(HTTP 引擎 / 未来 Mock 等)                    │  ← neton-http
└─────────────────────────────────────────────────────────┘
  • Core:定义 HttpAdapter、HttpContext、HttpRequest、HttpResponse、HttpSession、HandlerArgs、RequestEngine、RouteDefinition、RouteHandler、ParamConverter 等。
  • neton-http:提供 HttpComponent(install 入口)、实现 HttpAdapter、将底层 HTTP 请求转为 HttpContext,并注册 RequestEngine 的路由。

1.3 统一抽象架构

mermaid
graph TD
    A[Neton Application] --> B[RequestEngine]
    B --> C[HttpComponent]
    C --> D[MockHttpAdapter]
    C --> E[HTTP 适配器]
    D --> F[Testing Environment]
    E --> G[Production Environment]
    
    E --> H[HTTP 服务器]
    H --> I[Real HTTP Requests]
    
    D --> J[Simulated Requests]
    J --> K[Unit Tests]

二、HttpAdapter 与生命周期

2.1 接口(neton-core)

kotlin
interface HttpAdapter {
    suspend fun start(ctx: NetonContext)
    suspend fun stop()
    fun port(): Int
}
  • start(ctx):从 ctx 获取 RequestEngine(及 LoggerFactory 等),在适配器内部启动 HTTP 服务器;port、timeout、maxConnections 等由适配器构造或配置注入,由 Core 接口规定。
  • stop():优雅关闭(gracePeriodMillis, timeoutMillis)。
  • port():供启动日志、onStart 回调等使用;由实现方在构造或配置中设定。
  • 废弃runCompat(port, args) 仅兼容旧路径,应使用 start(ctx)

2.2 实现边界(neton-http)

HTTP 适配器核心实现

kotlin
class KtorHttpAdapter(
    serverConfig: HttpServerConfig,
    converterRegistry: ParamConverterRegistry? = null
) : HttpAdapter {
    override suspend fun start(ctx: NetonContext, onStarted: ((coldStartMs: Long) -> Unit)? = null)
    override suspend fun stop()
    override fun port(): Int
    override fun adapterName(): String
}
  • HTTP 适配器实现:构造参数为 HttpServerConfig(port、timeout、maxConnections、enableCompression)与可选的 ParamConverterRegistry
  • 生命周期
    1. Neton.run(args) { http { port = 8080 }; routing { }; ... } → 各组件 init(ctx, config) → HttpComponent.init 绑定 HttpServerConfig、HttpAdapter 到 ctx。
    2. 应用 start 阶段:Core 调用 httpAdapter.start(ctx);适配器从 ctx 取 RequestEngine,注册路由(get/post/put/delete 等),然后启动 HTTP 服务器。
    3. 关闭:stop() 或进程退出时 gracefulShutdown(),再置空 embeddedServer。

HTTP 对象适配

完整的 HTTP 对象适配包括:

  • HttpContext: 将底层 HTTP 请求包装为 HttpContext
  • HttpRequest: HTTP 请求的完整映射
  • HttpResponse: HTTP 响应的完整映射
  • HttpSession: 会话管理的完整映射
  • 适配器类: Headers、Parameters、Cookies 的完整适配

路由集成示例

kotlin
// HTTP 服务器配置
embeddedServer(port = port, host = host) {
    install(ContentNegotiation) {
        json(Json {
            prettyPrint = configuration.developmentMode
            isLenient = true
        })
    }
    
    routing {
        route("{...}") {  // 捕获所有请求
            handle {
                val context = createHttpContextFromKtor(call)
                requestEngine?.processRequest(context)  // 从 ctx 注入的 RequestEngine
            }
        }
    }
}

2.3 MockHttpAdapter(neton-core)

  • 无 neton-http 时的测试用适配器:start 时仅打 warn、空转;port() 返回构造时的 mockPort(默认 8080)。用于不启动真实 HTTP 的测试场景。

三、HttpContext / HttpRequest / HttpResponse

3.1 HttpContext(neton-core)

kotlin
interface HttpContext {
    val traceId: String
    val request: HttpRequest
    val response: HttpResponse
    val session: HttpSession
    val attributes: MutableMap<String, Any>
    fun getAttribute(name: String): Any?
    fun setAttribute(name: String, value: Any)
    fun removeAttribute(name: String): Any?
    fun getApplicationContext(): NetonContext?
}
  • traceId:请求级唯一标识,由适配器在请求入口生成(如 req-{ts}-{r})。v1.1 冻结HttpContext.traceIdLogContext.traceId 必须为同一值(同源);access log / error log 一律使用该 traceId,禁止再引入 requestId 等分裂字段。
  • getApplicationContext():返回当前 NetonContext,用于在 handler 内获取 ValidatorRegistry、LoggerFactory 等;neton-http 实现中在构造 HttpContext 时注入。

3.2 HttpRequest(neton-core)

  • 只读:method、path、url、version、headers、queryParams、pathParams、cookies、remoteAddress、contentType、contentLength、isSecure 等。
  • 请求体body()text()json()form()uploadFiles()(suspend);由适配器从底层 call 读取。
  • MultipartuploadFiles() 返回 UploadFiles(结构化视图);isMultipart() 判断是否为 multipart 请求。
  • 便捷:isGet/isPost、header(name)、queryParam(name)、pathParam(name)、accepts(contentType) 等。

完整的请求处理能力

  • HTTP方法: GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS
  • 路径参数: /user/{id} 自动提取和类型转换
  • 查询参数: ?name=value&page=1 完整支持
  • 请求体: JSON、表单、原始字节流
  • 文件上传: uploadFiles() 解析 multipart/form-data,返回 UploadFiles 结构化视图
  • Cookie: 完整的Cookie读取和设置

3.3 HttpResponse(neton-core)

  • 可写:status、headers(MutableHeaders)、contentType、contentLength、cookie/removeCookie。
  • 写入与快捷方法write(data: ByteArray);扩展 text(data, status)json(data, status)redirect(url, status)notFound()unauthorized()error(status, message) 等。
  • v1.1 冻结(响应语义二选一,已选 B)response.write 优先。若 handler 在返回前调用了 context.response 的任意"提交"动作(如 writetextjsonredirecterrornotFound 等),则适配器不再对 handler 的返回值做 respond;仅当 response 未提交时,才用返回值驱动响应。实现须:① 在适配器内维护 isCommitted,且 所有 提交入口(含 redirect,因 core 中 redirect 不调用 write)均置 committed;② commit 后再次 write/text/json/redirect 时 fail-fast 抛 HttpException(500, "Response already committed");③ handleRoute 先判断 isCommitted 再决定是否 handleResponse(result)。

完整的响应生成能力

  • 状态码: 完整的HTTP状态码支持
  • 响应头: 灵活的Header设置
  • 响应体: JSON、文本、字节流、文件
  • Cookie设置: 完整的Cookie选项支持
  • 便捷方法: ok()、json()、text()、notFound()等扩展函数

3.4 HttpSession(neton-core)

  • 接口:id、creationTime、lastAccessTime、maxInactiveInterval、isNew、isValid、get/set/removeAttribute、invalidate()、touch()。
  • v1 硬规则:Session 默认仅 in-memory不承诺跨实例、不承诺持久化。若需分布式 Session,必须在 v2 通过 neton-redis 或专用 session store 实现,不得在 v1 文档或默认实现中暗示"天然跨节点"。
  • 实现:Core 提供 MemoryHttpSession;neton-http 当前为内存 Map 实现。

会话管理能力

  • 会话ID: 自动生成和管理
  • 属性存储: 键值对存储
  • 生命周期: 创建时间、最后访问时间、超时管理
  • 无效化: 手动或自动会话清理

3.5 类型与枚举(neton-core)

  • HttpStatus:OK、CREATED、NO_CONTENT、BAD_REQUEST、UNAUTHORIZED、NOT_FOUND、INTERNAL_SERVER_ERROR 等;带 code、message。
  • HttpMethod:GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS、TRACE。
  • Headers / MutableHeadersParametersCookie / MutableCookie:接口由适配器实现(含 Simple* 等简化实现)。

四、HandlerArgs 与参数视图

4.1 接口(neton-core)

kotlin
interface HandlerArgs {
    fun first(name: String): Any?   // path 优先,否则 query 首个
    fun all(name: String): List<String>?  // 仅 query
    operator fun get(name: String): Any? = first(name)
}
  • 只读;path 与 query 分离,避免合并大 Map;查找时 path 优先、再 query,符合路由参数语义。
  • 实现:ArgsView(path: Map, query: Map)、MapBackedHandlerArgs;HTTP 适配器在 handleRoute 中根据 pattern 与请求构建 ArgsView 传入 RouteHandler。

4.2 参数解析与 ParamConverter

  • ParamConverterRegistry:在 HttpConfig 中可配置(如 http { converters { register(UUID::class, ...) } }),默认 DefaultParamConverterRegistry(String/Int/Long/Boolean/Double/Float)。
  • ParameterResolver:SPI,用于从 HttpContext 解析控制器方法参数(PathVariable、RequestBody、ContextObject 等);与 KSP/路由层配合。
  • ParameterBinding:PathVariable、RequestBody、CurrentUser、ContextObject 等密封类,描述参数来源与类型。

4.3 类型安全的参数绑定

  • 智能绑定: 自动参数绑定和类型转换
  • 编译时检查: KSP代码生成确保编译时类型检查
  • 支持的绑定类型:
    • @PathVariable - 路径参数
    • @RequestBody - 请求体
    • @CurrentUser - 当前用户身份
    • ContextObject - 上下文对象注入

五、请求处理流程(neton-http)

5.1 路由注册

  • RequestEngine 由 neton-routing(或 KSP 生成)在 init 阶段注册 RouteDefinition 列表。
  • HTTP 适配器 start 后,从 ctx 取 RequestEngine,遍历 getRoutes(),按 method 注册路由(get/post/put/delete/patch/head/options),每个路由执行 handleRoute
  • P1 冻结固定端点(/health、/api/routes、/api/info 等)不得写死在适配器内。这些端点应由 Routing/RequestEngine 或独立 health、metrics 组件在 init/start 时以普通路由注册;适配器只负责 transport,仅保留最低限度 404 fallback(未匹配请求 route("{...}") 返回 404)。v1 若暂保留适配器内固定端点,须在文档中标明为过渡,v1.1 迁移至由路由/组件注册。

5.2 单请求流程

  1. 入口:handleRoute(route, call)。
  2. traceId:generateRequestTraceId();设置 LogContext(traceId, requestId, …);finally 中 clear。
  3. HttpContext:request/response/session 为适配层实现。
  4. 参数:buildHandlerArgs(call, pattern) → ArgsView(path 从 pattern 占位符与 URI 解析,query 从 queryParameters,path 优先)。
  5. 执行route.handler.invoke(httpContext, args),得到返回值 result。
  6. 响应(v1.1 冻结·方案 B):若 context.response.isCommitted == true不再对 result 做 respond;否则 handleResponse(call, result, …):
    • null/Unit → 204 No Content
    • String → 200 text/plain
    • Map → 200 JSON
    • Number/Boolean → 200 text/plain
    • 其他 → call.respond(result)(序列化后响应)
    • 实现落点:HttpResponse 实现须在 write/text/json/redirect/error 等"提交"动作时置 isCommitted=true,handleRoute 中先判断 response.isCommitted 再决定是否调用 handleResponse(result)。
    • 加固(v1.1):① 所有响应入口(text/json/redirect/error/notFound/…)最终都经 write 或显式 commit,保证 commit 点唯一;② commit 后禁止二次写,再次调用 write/text/json/redirect 等须 fail-fast 抛 HttpException(500, "Response already committed");③ 未 commit 时 status 由 handleResponse 语义决定,commit 时 status 必须在 commit 前写入 response.status,access log 统一用 response.status.code。
  7. 异常:ValidationException → 400 + ErrorResponse;HttpException → status + ErrorResponse;其他 → 500 + ErrorResponse。
  8. access log:finally 中打 msg 固定 "http.access"(规范冻结,实现须一致),字段:method、path、status、latencyMs、bytesIn、bytesOut、traceId(与 LogContext 同源);可选 routePattern(命中的路由模板,如 /users/{id},便于按路由聚合 metrics/日志)。bytesOut 语义:① 通过 write(data) 提交时 = data.size;② redirect 已 commit 但无 body,v1 bytesOut = 0;③ 非 committed(返回值驱动)路径 = 不保证,v1 记为 0。这样"committed ≠ bytesOut > 0"的边界明确。

5.3 错误响应体(neton-core)

kotlin
@Serializable
data class ErrorResponse(
    val success: Boolean = false,
    val message: String,
    val errors: List<ValidationError> = emptyList()
)
  • 4xx/5xx 时由适配器 respond 该结构(或等价 JSON),便于前端统一解析。

5.4 完整的异常处理

  • 异常体系: 完整的异常体系和HTTP状态码映射
  • 错误恢复: 完整的错误恢复和日志记录
  • 堆栈跟踪: 清晰的错误信息和堆栈跟踪

六、HttpComponent 与配置

6.1 组件职责(neton-http)

  • HttpComponent:NetonComponent<HttpConfig>;defaultConfig() = HttpConfig();init(ctx, config) 时:
    • 从 ctx 或 config 取 ParamConverterRegistry,bind 到 ctx;
    • HTTP 配置仅来自 application 配置(见 6.2),不再读取 http.conf;
    • 构造 HttpServerConfig(port、timeout、maxConnections、enableCompression 由 application 配置 + DSL 合并得到)、HTTP 适配器实例,并 bind HttpServerConfig、HttpAdapter 到 ctx。

6.2 配置来源(v1.1 冻结)

  • 禁止:不得使用 loadComponentConfig("HttpComponent") 或单独 http.conf;HTTP 属于"运行时基础设施",配置归属与 Core Config v1.1 一致。
  • 唯一文件来源仅允许在 application.conf(及 application.<env>.conf)中配置 HTTP,例如:
    • [server]:port 等(与 Core 5.1 推荐骨架一致);
    • [http]:timeout、maxConnections、enableCompression 等。
  • DSLhttp { port = 8080 }http { converters { ... } } 仅负责代码默认值或开发期 override,不构成独立配置体系。
  • 优先级(与 Config v1.1 一致):CLI/ENV > application.<env>.conf > application.conf > DSL defaultConfig(最低)。HttpComponent 应使用 ConfigLoader.loadApplicationConfig(configPath, env, ctx.args) 得到合并后的 map,再从中读取 server.port、http.timeout 等;若某 key 缺失,再用 DSL config 或默认值。
  • 这样可避免 ops 在 application.conf 修改 timeout 后仍被旧 http.conf 或代码默认值覆盖,避免 HTTP 配置分裂成两套。

6.3 HttpConfig(neton-core)

kotlin
class HttpConfig {
    var port: Int = 8080
    var converterRegistry: ParamConverterRegistry? = null
    var corsConfig: CorsConfig? = null
}
fun HttpConfig.converters(block: ParamConverterRegistry.() -> Unit)
fun HttpConfig.cors(block: CorsConfig.() -> Unit)
  • 上述 port 等为 DSL 默认值;最终生效值 = Config 优先级合并后的结果(见 6.2)。

6.4 CORS 配置

CORS 为纯 HTTP 层行为,通过 DSL 或 application.conf 配置。

kotlin
class CorsConfig {
    var allowedOrigins: List<String> = listOf("*")
    var allowedMethods: List<String> = listOf("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")
    var allowedHeaders: List<String> = listOf("*")
    var allowCredentials: Boolean = false
    var maxAgeSeconds: Long = 3600
}

DSL 用法

kotlin
http {
    port = 8080
    cors {
        allowedOrigins = listOf("http://localhost:3000", "https://admin.example.com")
        allowCredentials = true
        maxAgeSeconds = 86400
    }
}

application.conf 用法

toml
[cors]
allowedOrigins = ["http://localhost:3000", "https://admin.example.com"]
allowCredentials = true
maxAgeSeconds = 86400

优先级application.conf [cors] > DSL cors { }corsConfig = null 时不启用 CORS。

6.5 UploadFile 与 UploadFiles(文件上传抽象)

三层设计:UploadFile(协议真相)→ UploadFiles(结构化视图)→ KSP 参数级自动绑定(按 fieldName)。

UploadFile 接口

kotlin
interface UploadFile {
    val fieldName: String       // 表单字段名
    val filename: String        // 原始文件名
    val contentType: String?    // MIME 类型
    val size: Long              // 文件大小(字节)
    suspend fun bytes(): ByteArray  // 读取内容
}

UploadFiles 结构化视图

kotlin
class UploadFiles(private val parts: List<UploadFile>) {
    fun all(): List<UploadFile>                          // 所有文件(协议顺序)
    fun get(name: String): List<UploadFile>              // 按 fieldName 过滤
    fun first(name: String): UploadFile?                 // 按 fieldName 取第一个
    fun require(name: String): UploadFile                // 按 fieldName 取第一个,缺失抛 BadRequestException
    fun asMap(): Map<String, List<UploadFile>>           // 按 fieldName 分组
    fun isEmpty(): Boolean
    val size: Int
}
  • HttpRequest.uploadFiles() 返回 UploadFiles
  • 定义在 neton-core,平台无关;neton-http 适配器从 Ktor Multipart API 构造实现。

KSP 参数绑定规则(按 fieldName 匹配参数名)

参数声明生成代码说明
avatar: UploadFilecontext.request.uploadFiles().require("avatar")fieldName == "avatar",缺失抛 400
avatar: UploadFile?context.request.uploadFiles().first("avatar")fieldName == "avatar",缺失为 null
photos: List&lt;UploadFile&gt;context.request.uploadFiles().get("photos")fieldName == "photos",无匹配返回空列表
files: UploadFilescontext.request.uploadFiles()注入完整结构化视图

控制器用法

kotlin
@Post("/avatar")
suspend fun uploadAvatar(avatar: UploadFile): Map<String, Any> {
    val bytes = avatar.bytes()
    storage.write("avatars/${avatar.filename}", bytes)
    return mapOf("filename" to avatar.filename, "size" to avatar.size)
}

@Post("/photos")
suspend fun uploadPhotos(photos: List<UploadFile>): Map<String, Any> {
    return mapOf("count" to photos.size, "names" to photos.map { it.filename })
}

@Post("/mixed")
suspend fun mixedUpload(files: UploadFiles): Map<String, Any> {
    val avatar = files.require("avatar")
    val gallery = files.get("gallery")
    return mapOf("avatar" to avatar.filename, "galleryCount" to gallery.size)
}

6.6 灵活的配置管理

  • 配置管理: 灵活的适配器配置系统
  • 环境支持: 开发、测试、生产环境配置分离
  • 配置优先级: 明确的配置来源优先级

七、与 Core / 路由 / 安全的关系

7.1 RequestEngine(neton-core)

  • 接口:processRequest(context)、registerRoute(route)、getRoutes()、setAuthenticationContext(authContext)。
  • v1.1 职责收口(P1):RequestEngine 的定位为 "路由注册仓库 + security pipeline"不是唯一入口模式。当前模型为 "engine 提供 routes → adapter 按路由注册并驱动"
    • HTTP 适配器:只消费 getRoutes(),将每个 RouteDefinition 注册到 HTTP 引擎,请求时直接 handleRoute,调用 processRequest。
    • processRequest:保留给 Mock/测试单 catch-all 适配器(若未来有仅支持"单 catch-all + engine 内匹配"的实现)使用。
  • 认证上下文由 Core 在 configureRequestEngine 阶段 setAuthenticationContext,供路由/守卫使用。

7.2 RouteDefinition / RouteHandler(neton-core)

kotlin
data class RouteDefinition(
    val pattern: String,
    val method: HttpMethod,
    val handler: RouteHandler,
    val parameterBindings: List<ParameterBinding> = emptyList(),
    val controllerClass: String? = null,
    val methodName: String? = null
)
interface RouteHandler {
    suspend fun invoke(context: HttpContext, args: HandlerArgs): Any?
}
  • 路由由 neton-routing 或 KSP 注册到 RequestEngine;neton-http 仅消费 getRoutes() 并驱动 HTTP 引擎与 handler 调用。

7.3 安全

  • HttpContext 不直接暴露 principal;认证结果通过 AuthenticationContext / SecurityContext 与 RequestEngine 配合,在参数解析或守卫中使用。
  • 安全相关注解与类型
    • 使用 Identity(包含 id、roles、permissions)而非 Principal
    • 使用 @CurrentUser 注解注入当前用户身份
    • 认证上下文通过 RequestEngine 的 setAuthenticationContext 配置

八、实际应用示例

8.1 服务器启动

应用通过 Neton.run(args) 启动,HttpAdapter 由 HttpComponent 创建并绑定,无需手动构造。示例:

kotlin
fun main(args: Array<String>) {
    Neton.run(args) {
        http { port = 8080 }
        routing {
            get("/") { "Hello, Neton!" }
            get("/api/health") { mapOf("status" to "OK") }
        }
        onStart { ctx ->
            val port = ctx.getOrNull<HttpAdapter>()?.port() ?: 8080
            println("Server started on port $port")
        }
    }
}

8.2 控制器开发

kotlin
@Controller("/api")
class UserController {

    @Get("/users/{id}")
    suspend fun getUser(
        @PathVariable("id") userId: Long,
        request: HttpRequest
    ): String {
        return "User ID: $userId from ${request.remoteAddress}"
    }

    @Post("/users")
    suspend fun createUser(
        @RequestBody user: UserCreateRequest,
        @CurrentUser identity: Identity?
    ): UserResponse {
        // identity.id, identity.roles, identity.permissions 可用
        return UserResponse(
            id = generateId(),
            name = user.name,
            createdBy = identity?.id ?: "anonymous"
        )
    }

    @Put("/users/{id}")
    suspend fun updateUser(
        @PathVariable("id") userId: Long,
        @RequestBody updates: UserUpdateRequest,
        context: HttpContext
    ): String {
        // 使用 context 访问 session、attributes 等
        context.session.setAttribute("lastUpdate", System.currentTimeMillis())
        return "User $userId updated"
    }
}

8.3 开发与测试流程

开发阶段

bash
# 运行测试 - 使用Mock适配器
./gradlew test

# 本地开发 - 使用HTTP适配器
./gradlew run

生产部署

bash
# 编译原生可执行文件
./gradlew linkReleaseExecutableMacosArm64

# 运行生产服务器
./build/bin/macosArm64/releaseExecutable/myapp.kexe

九、架构优势与特性

9.1 企业级特性

  • 配置管理: 灵活的适配器配置系统
  • 异常处理: 完整的异常体系和错误恢复
  • 日志追踪: TraceID支持APM和日志系统
  • 性能监控: 请求处理时间和资源使用监控

9.2 开发者体验

  • 类型安全: KSP代码生成确保编译时检查
  • 智能绑定: 自动参数绑定和类型转换
  • 测试友好: Mock环境完整支持单元测试
  • 调试友好: 清晰的错误信息和堆栈跟踪

9.3 架构对比

特性MockHttpAdapterHTTP 适配器
用途测试和开发生产环境
性能内存操作真实网络I/O
功能完整性100%模拟100%真实
调试能力完全可控真实环境
配置复杂度简单中等
依赖要求无外部依赖neton-http

9.4 性能表现

基于 Kotlin/Native 和 HTTP 引擎的性能指标:

  • 启动时间: < 100ms(原生编译)
  • 内存使用: < 50MB(基础应用)
  • 吞吐量: > 10,000 RPS(单核心)
  • 延迟: < 1ms(本地请求)

十、v1 实现限制与后续方向

10.1 当前实现限制(v1)

  • Session:仅 in-memory,不承诺跨实例(见 3.4)。
  • Request/Response 适配:Simple* 为适配器简化实现;响应语义已按 v1.1 冻结为"response.write 优先"(见 3.3、5.2)。
  • CORS:已支持,通过 DSL cors { }application.conf [cors] 配置。
  • Multipart/文件上传:已支持,HttpRequest.uploadFiles() 返回 UploadFiles;支持按 fieldName 查询、KSP 按参数名自动绑定。
  • 中间件:无独立 Middleware 管道;日志、异常、access 在 handleRoute 内线性完成。
  • 多后端:当前 HTTP 引擎 + Mock(Core 内 MockHttpAdapter)。
  • 固定端点:/health、/api/routes、/api/info 若仍在适配器内,视为过渡,v1.1 迁移至由路由/组件注册(见 5.1)。

10.2 与设计文档的对应关系

  • HTTP 抽象层架构设计:四层分离、HttpContext 为数据总线、HandlerArgs path/query 分离、ParamConverter/ParameterResolver、错误响应体已落地;协程上下文集成、懒加载、对象池、完整中间件管道为后续。
  • HTTP适配器优化:TraceID(且与 LogContext 同源)、便捷 response 方法、类型安全上下文已具备;simulateRequest 增强、Mock 完整请求模拟可在测试/Mock 模块中补充。

10.3 建议的后续步骤

  • 固定端点迁移至 Routing/health 组件注册;适配器仅保留 404 fallback。
  • Session 分布式:v2 通过 neton-redis 或专用 session store。
  • 多后端:新增 neton-http-mock 或 test 专用适配器时,可复用 processRequest 作为入口。
  • 中间件管道:在 v2 引入独立的 Middleware 接口和管道机制。

十一、总结

通过完善 HTTP 适配器,Neton 框架具备:

  1. 完整的 HTTP 服务器能力 - 从 Mock 测试到生产部署的全栈支持
  2. 优雅的架构设计 - 清晰的分层和统一的抽象接口
  3. 企业级特性 - 配置管理、异常处理、性能监控、安全支持
  4. 优秀的开发体验 - 类型安全、智能绑定、测试友好、调试友好
  5. Kotlin/Native 优势 - 高性能、低内存、快速启动、单文件部署

Neton 是一个面向生产环境的 Kotlin/Native Web 框架,既保持现代开发体验,又提供传统 Web 框架的完整功能。


十二、文档与规范引用

  • Neton-Core-Spec:第六节(HTTP 抽象层)、第七节(路由与请求处理)。
  • 本规范:以 neton-core 与 neton-http 当前代码为准;若与 Core Spec 有措辞差异,以 Core Spec 为权威,本文为 HTTP 专项展开与实现说明。

Neton Framework Documentation