[{"data":1,"prerenderedAt":179},["ShallowReactive",2],{"\u002F2026\u002Fhigh-concurrency-runtime":3,"surround-\u002F2026\u002Fhigh-concurrency-runtime":168},{"id":4,"title":5,"body":6,"categories":145,"date":147,"description":148,"draft":149,"extension":131,"image":150,"meta":151,"navigation":153,"path":154,"permalink":150,"published":150,"readingTime":155,"recommend":150,"references":150,"seo":160,"sitemap":161,"stem":162,"tags":163,"type":166,"updated":147,"__hash__":167},"content\u002Fposts\u002F2026\u002Fhigh-concurrency-runtime:调度层设计.md","high-concurrency-runtime:调度层设计",{"type":7,"value":8,"toc":135},"minimark",[9,13,17,20,23,26,34,37,47,50,54,60,66,69,75,84,90,94,101,107,110,116,120,126],[10,11,12],"h3",{"id":12},"前言",[14,15,16],"p",{},"为HTTP server增加一个调度层， 以下是我根据自写网络库和HTTP层的基础。",[10,18,19],{"id":19},"调度层的本质",[14,21,22],{},"调度层 = 任务管理 + 执行控制",[14,24,25],{},"核心作用 接受任务->排队等待->分配线程执行->控制生命周期(超时\u002F取消\u002F重试)",[14,27,28,29,33],{},"原先的问题，",[30,31,32],"code",{"code":32},"HTTP Server","直接调用上层的处理器Handler, 尝试写一个分支决定是否启用调度器模式。 通过把Handler继续封装成Task, 然后统一交给调度器扔给不同的线程。\n尝试不同的调度算法和提供更多机制。",[10,35,36],{"id":36},"整体架构",[38,39,45],"pre",{"className":40,"code":42,"language":43,"meta":44},[41],"language-cpp","┌─────────────────────────────────────┐\n│ runtime_http │\n│ HttpServer → SetScheduler() │\n│ │ │\n│ └──► Scheduler(新增) │\n└─────────────┬───────────────────────┘\n│ depends on\n┌─────────────▼───────────────────────┐\n│ runtime_task │\n│ Scheduler → ThreadPool (已有) │\n│ Task (新增) │\n└─────────────┬───────────────────────┘\n│ depends on\n┌─────────────▼───────────────────────┐\n│ runtime_foundation │\n└─────────────────────────────────────┘\n","cpp","",[30,46,42],{"__ignoreMap":44},[14,48,49],{},"流程， HTTP请求到达 Router找到Handler, 将Handler封装成一个Task， 提交给调度器， 调度器扔到线程池， 将结果封装成响应报文打回去。",[10,51,53],{"id":52},"task","Task",[14,55,56,59],{},[30,57,58],{"code":58},"Handler","封装一个任务Task， 下面注释表示字段的含义。\n为每个任务提供一个唯一标识， 这里是int自增， 用原子计数器作为标识。",[38,61,64],{"className":62,"code":63,"language":43,"meta":44},[41],"  Func func_; \u002F\u002F 真正执行任务的回调\n  int id_; \u002F\u002F Task唯一标识\n  int priority_{0}; \u002F\u002F 优先级， 为更多调度算法提供扩展性。\n  runtime::time::Timestamp deadline_{}; \u002F\u002F 超时截止， 用来设定限时任务\n  std::atomic\u003Cbool> cancelled_{false}; \u002F\u002F 取消标识， 任务应该可以主动取消。\n\n  static std::atomic\u003Cint> id_counter_; \u002F\u002F 原子计数器\n",[30,65,63],{"__ignoreMap":44},[14,67,68],{},"抛开设置和获取的成员函数， 最有价值的是Run方法。\n以下是成员函数，构造函数的声明。",[38,70,73],{"className":71,"code":72,"language":43,"meta":44},[41],"  explicit Task(Func f);\n\n  \u002F\u002F std::atomic is not movable, so Task defines a custom move constructor.\n  Task(Task&& other) noexcept\n      : func_(std::move(other.func_)),\n        id_(other.id_),\n        priority_(other.priority_),\n        deadline_(other.deadline_),\n        cancelled_(other.cancelled_.load()) {}\n\n  int Id() const { return id_; }\n  int Priority() const { return priority_; }\n  void SetPriority(int p) { priority_ = p; }\n\n  runtime::time::Timestamp Deadline() const { return deadline_; }\n  void SetDeadline(runtime::time::Timestamp t) { deadline_ = t; }\n\n  bool Cancelled() const { return cancelled_.load(); }\n  void Cancel() { cancelled_.store(true); }\n\n  void Run();\n",[30,74,72],{"__ignoreMap":44},[14,76,77,80,81],{},[30,78,79],{"code":79},"Run","方法实现逻辑，很简单。\n检查任务是否取消， 检查任务是否超时，执行回调",[30,82,83],{"code":83},"func_",[38,85,88],{"className":86,"code":87,"language":43,"meta":44},[41],"std::atomic\u003Cint> Task::id_counter_{0};\n\nTask::Task(Func f)\n    : func_(f),\n      id_(id_counter_++) {}\n\nvoid Task::Run() {\n    if (cancelled_) return;\n    if (deadline_.Valid() && runtime::time::Timestamp::Now() > deadline_) return;\n    func_();\n}\n",[30,89,87],{"__ignoreMap":44},[10,91,93],{"id":92},"scheduler","Scheduler",[14,95,96,97,100],{},"调度控制层， 包裹",[30,98,99],{"code":99},"ThreadPool",", 两者职责分离。\n调度层做限流， 设定任务队列的最大数量， 超过最大数量就丢弃。",[38,102,105],{"className":103,"code":104,"language":43,"meta":44},[41],"Submit(Task)\n   ├── pending >= max_queue_size →  throw（限流）\n   ├── pending++\n   ├── pool_.enqueue(lambda)\n         ├── pending--\n             Task::Run()\n",[30,106,104],{"__ignoreMap":44},[14,108,109],{},"设计理念:\n调度层",[38,111,114],{"className":112,"code":113,"language":43,"meta":44},[41],"\u002F\u002F Scheduler adds queue limits and task bookkeeping on top of ThreadPool.\nclass Scheduler : public runtime::base::NonCopyable {\npublic:\n  \u002F\u002F worker_count == 0 uses hardware concurrency.\n  \u002F\u002F max_queue_size == 0 means the queue is unbounded.\n  explicit Scheduler(std::size_t worker_count = 0,\n                     std::size_t max_queue_size = 0);\n\n  void Submit(Task task);\n\n  template \u003Ctypename Func>\n  void Submit(Func&& f) {\n    Submit(Task(std::forward\u003CFunc>(f)));\n  }\n\n  std::size_t PendingCount() const { return pending_.load(); }\n\nprivate:\n  ThreadPool pool_;\n  std::size_t max_queue_size_{0};\n  std::atomic\u003Cstd::size_t> pending_{0};\n};\n",[30,115,113],{"__ignoreMap":44},[10,117,119],{"id":118},"http层改动","HTTP层改动",[38,121,124],{"className":122,"code":123,"language":43,"meta":44},[41],"OnMessage() 收到完整请求\n    └── router_.Match()\n            ├── 无 scheduler_ → 原有同步调用（行为不变）\n            └── 有 scheduler_ → 封装为 Task 提交\n                    └── 工作线程执行 handler(req, resp)\n                            └── conn->Send()   ← 已是线程安全\n                                conn->Shutdown() ← 已是线程安全\n\n",[30,125,123],{"__ignoreMap":44},[38,127,133],{"className":128,"code":130,"language":131,"meta":132},[129],"language-md","\u003C!-- 你可以在此处书写大纲，并在上方完成文章 -->\n","md","wrap",[30,134,130],{"__ignoreMap":44},{"title":44,"searchDepth":136,"depth":136,"links":137},4,[138,140,141,142,143,144],{"id":12,"depth":139,"text":12},3,{"id":19,"depth":139,"text":19},{"id":36,"depth":139,"text":36},{"id":52,"depth":139,"text":53},{"id":92,"depth":139,"text":93},{"id":118,"depth":139,"text":119},[146],"技术","2026-04-24 00:10:39","high-concurrency-runtime调度层设计",false,null,{"slots":152},{},true,"\u002F2026\u002Fhigh-concurrency-runtime",{"text":156,"minutes":157,"time":158,"words":159},"4 min read",3.765,225900,753,{"title":5,"description":148},{"loc":154},"posts\u002F2026\u002Fhigh-concurrency-runtime\u002F调度层设计",[164,165,93],"C++","Net","tech","sosnWgI9fBdlC0jKd_7ipfEFfnawjZYZAQUAMILNfzg",[169,174],{"title":170,"path":171,"stem":172,"date":173,"type":166,"children":-1},"Leetcode 1 - 100","\u002F2026\u002Fleetcode-1-100","posts\u002F2026\u002FLeetcode 1 - 100","2026-04-22 22:44:05",{"title":175,"path":176,"stem":177,"date":178,"type":166,"children":-1},"KMP-(Knuth-Morris-Pratt)","\u002F2026\u002Fkmp-(knuth-morris-pratt)","posts\u002F2026\u002FKMP-(Knuth-Morris-Pratt)","2026-04-29 13:29:48",1777518298978]