kitc执行流程分析

最近阅读了RPC框架kitc的部分源码,kitc是kite框架的客户端实现,kite框架是公司内部基于开源的thrift封装二次开发的,与开源的kite框架并不一样。kitc 的执行过程也是中间件调用链的执行过程。

对目标接口方法进行封装
// client端调用目标接口的方法,由工具根据接口定义自动生成
func (p *ExampleServiceClient) Method1(req *ExampleRequest) (r *ExampleResponse, err error) {
  if err = p.sendMethod1(req); err != nil { return }
   return p.recvMethod1()
 }
 
 // EndPoint represent one method for calling from remote.
 // EndPoint 代表一个远端方法的调用过程
type EndPoint func(ctx context.Context, req interface{}) (resp interface{}, err error)

// 对目标接口方法进行封装,封装为endpoint.EndPoint类型
func mkMethod1(client *example.ExampleServiceClient) endpoint.EndPoint {
     return func(ctx context.Context, request interface{}) (interface{}, error) {
         transport := client.Transport.(kitc.Transport)
         err := transport.OpenWithContext(ctx)
         if err != nil {...}
         defer transport.Close()
         // Method1为proto文件中定义的服务接口 
         resp, err := client.Method1(request.(endpoint.KitcCallRequest).RealRequest().(*example.ExampleRequest))
         addr := transport.RemoteAddr()
         return &KitcExampleResponse{resp, addr}, err
     }
 }
kitc中间件的定义
// Middleware deal with input EndPoint and output EndPoint
// 输入为EndPoint类型,输出为EndPoint类型
type Middleware func(EndPoint) EndPoint

// client端执行目标接口方法调用
func (c *Client) Method1(ctx context.Context, r *example.ExampleRequest) (*KitcExampleResponse, error) {
     kc := c.client
     if kc == nil {...}
     // 中间件的初始化
     **ctx, err := kc.MethodInit("Method1", ctx, r)**  @1
     if err != nil {...}
     client := GetExampleServiceClient(kc)
     **next := mkMethod1(client)**
     request := &KiteExampleRequest{r}
     **resp, err := kc.MethodCall(next, ctx, request)** @2
     kitcResp, ok := resp.(*KitcExampleResponse)
     if !ok {...}
     return kitcResp, err
 }

// @1 处的代码
func (kc *KitcClient) MethodInit(method string, ctx context.Context, request interface{}) (context.Context, error) {
     metricsClient.EmitCounter("kite.request.throughput", 1)
     // 初始化中间件调用链,包含服务发现中间件,负载均衡中间件, 连接池中间件等等
     kc.once.Do(kc.initMWChain)
     // 因为request会被生成代码包裹,
     // 如果用户使用了自定义的LB, 需要保证直接把最原始的req传递给用户
     ctx = context.WithValue(ctx, KITC_RAW_REQUEST_KEY, request)
     return kc.initRPCInfo(method, ctx)
 }

// @2 处的代码
func (kc *KitcClient) MethodCall(next endpoint.EndPoint, ctx context.Context, request interface{}) (endpoint.KitcCallResponse, error) {
    ......
    // kc.chain(next)执行中间件调用链,生成一个EndPoint类型,然后执行EndPoint方法
    // 如此就完成了目标接口方法的调用
     resp, err := kc.chain(next)(ctx, request)
     if _, ok := resp.(endpoint.KitcCallResponse); !ok {...}
     return resp.(endpoint.KitcCallResponse), err
}
怎么改{ "extensions.ignoreRecommendations": true, "explorer.confirmDragAndDrop": false, "explorer.confirmDelete": false, "security.workspace.trust.untrustedFiles": "open", "workbench.editor.enablePreview": false, "files.autoSave": "afterDelay", "editor.fontSize": 22, "workbench.commandPalette.experimental.suggestCommands": true, "code-runner.executorMap": { "javascript": "node", "java": "cd $dir && javac $fileName && java $fileNameWithoutExt", "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "zig": "zig run", "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "php": "php", "python": "python -u", "perl": "perl", "perl6": "perl6", "ruby": "ruby", "go": "go run", "lua": "lua", "groovy": "groovy", "powershell": "powershell -ExecutionPolicy ByPass -File", "bat": "cmd /c", "shellscript": "bash", "fsharp": "fsi", "csharp": "scriptcs", "vbscript": "cscript //Nologo", "typescript": "ts-node", "coffeescript": "coffee", "scala": "scala", "swift": "swift", "julia": "julia", "crystal": "crystal", "ocaml": "ocaml", "r": "Rscript", "applescript": "osascript", "clojure": "lein exec", "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt", "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt", "racket": "racket", "scheme": "csi -script", "ahk": "autohotkey", "autoit": "autoit3", "dart": "dart", "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt", "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt", "haskell": "runghc", "nim": "nim compile --verbosity:0 --hints:off --run", "lisp": "sbcl --script", "kit": "kitc --run", "v": "v run", "sass": "sass --style expanded", "scss": "scss --style expanded", "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css", "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", "sml": "cd $dir && sml $fileName", "mojo": "mojo run", "erlang": "escript", "spwn": "spwn build", "pkl": "cd $dir && pkl eval -f yaml $fileName -o $fileNameWithoutExt.yaml", "gleam": "gleam run -m $fileNameWithoutExt" } }
03-21
这是用户区的settings.json: { "code-runner.fileDirectoryAsCwd": true, "code-runner.runInTerminal": true, "code-runner.clearPreviousOutput": true, "security.workspace.trust.untrustedFiles": "open", "markdown-preview-enhanced.mathRenderingOption": "MathJax", "markdown.extension.completion.enabled": true, "python.defaultInterpreterPath": "C:\\Users\\imsfp\\AppData\\Local\\Programs\\Python\\Python313\\python.exe", "editor.fontFamily": "Maple Mono, Consolas, monospace", "terminal.integrated.fontFamily": "monospace", "workbench.iconTheme": "vscode-icons", "workbench.colorCustomizations": { "editorCursor.foreground": "#FF0000", "terminalCursor.foreground": "#FF5555" }, "window.titleBarStyle": "custom", "python.terminal.executeInFileDir": true, "python.analysis.completeFunctionParens": true, "code-runner.executorMap": { "python": "set PYTHONIOENCODING=utf8 && $pythonPath -u $fullFileName" }, "code-runner.showExecutionMessage": false, "git.ignoreMissingGitWarning": true, "git.openRepositoryInParentFolders": "never", "github.copilot.chat.agent.thinkingTool": true, "github.copilot.chat.languageContext.typescript.enabled": true, "github.copilot.chat.newWorkspaceCreation.enabled": true, "github.copilot.chat.editor.temporalContext.enabled": true, "terminal.integrated.defaultProfile.windows": "Windows PowerShell", "editor.formatOnSave": true, "python.analysis.typeCheckingMode": "strict", "chat.editing.confirmEditRequestRetry": false, "github.copilot.chat.agent.toolValidation": "strict", "github.copilot.codeQualityScan.enabled": true, "chat.mcp.discovery.enabled": true, "[markdown]": { "editor.defaultFormatter": "DavidAnson.vscode-markdownlint" }, "explorer.confirmDelete": false, "files.autoSave": "afterDelay", "geminicodeassist.project": "noble-subset-466101-u9", "IntelligentCodeCompletion.disableLanguages": { "vue": "false", "typescript": "false", "javascript": "false", "python": "false", "go": "false", "c": "false", "c++": "false", "shell": "false", "bash": "false", "batch": "false", "lua": "false", "java": "false", "php": "false", "ruby": "false" }, "FunctionQuickCommands.disableLanguages": { "typescript": "false", "javascript": "false", "python": "false", "go": "false", "c": "false", "c++": "false", "lua": "false", "java": "false", "php": "false", "ruby": "false" }, "IntelligentCodeCompletion.enabled": true, "zgsm.allowedCommands": [ "npm test", "npm install", "tsc", "git log", "git diff", "git show" ], "github.copilot.nextEditSuggestions.enabled": true, "[python]": { "diffEditor.ignoreTrimWhitespace": false, "editor.defaultColorDecorators": "never", "gitlens.codeLens.symbolScopes": [ "!Module" ], "editor.formatOnType": true, "editor.wordBasedSuggestions": "off" }, "terminal.integrated.splitCwd": "workspaceRoot", "code-runner.executorMapByFileExtension": { ".vb": "cd $dir && vbc /nologo $fileName && $dir$fileNameWithoutExt", ".vbs": "cscript //Nologo", ".scala": "scala", ".jl": "julia", ".cr": "crystal", ".ml": "ocaml", ".zig": "zig run", ".exs": "elixir", ".hx": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt", ".rkt": "racket", ".scm": "csi -script", ".ahk": "autohotkey", ".au3": "autoit3", ".kt": "cd $dir && kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar", ".kts": "kotlinc -script", ".dart": "dart", ".pas": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt", ".pp": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt", ".d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt", ".hs": "runhaskell", ".nim": "nim compile --verbosity:0 --hints:off --run", ".csproj": "dotnet run --project", ".fsproj": "dotnet run --project", ".lisp": "sbcl --script", ".kit": "kitc --run", ".v": "v run", ".vsh": "v run", ".sass": "sass --style expanded", ".cu": "cd $dir && nvcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", ".ring": "ring", ".sml": "cd $dir && sml $fileName", ".mojo": "mojo run", ".erl": "escript", ".spwn": "spwn build", ".pkl": "cd $dir && pkl eval -f yaml $fileName -o $fileNameWithoutExt.yaml", ".gleam": "gleam run -m $fileNameWithoutExt" }, "code-runner.saveAllFilesBeforeRun": true, "code-runner.saveFileBeforeRun": true, "code-runner.languageIdToFileExtensionMap": { "bat": ".bat", "powershell": ".ps1", "typescript": ".ts" }, "code-runner.terminalRoot": "'/mnt/", } 检查是哪个设置出了问题使得coderunner持续的使用unix风格命令
07-20
内容概要:本文介绍了一个基于MATLAB实现的无人机三维路径规划项目,采用蚁群算法(ACO)与多层感知机(MLP)相结合的混合模型(ACO-MLP)。该模型通过三维环境离散化建模,利用ACO进行全局路径搜索,并引入MLP对环境特征进行自适应学习与启发因子优化,实现路径的动态调整与多目标优化。项目解决了高维空间建模、动态障碍规避、局部最优陷阱、算法实时性及多目标权衡等关键技术难题,结合并行计算与参数自适应机制,提升了路径规划的智能性、安全性和工程适用性。文中提供了详细的模型架构、核心算法流程及MATLAB代码示例,涵盖空间建模、信息素更新、MLP训练与融合优化等关键步骤。; 适合人群:具备一定MATLAB编程基础,熟悉智能优化算法与神经网络的高校学生、科研人员及从事无人机路径规划相关工作的工程师;适合从事智能无人系统、自动驾驶、机器人导航等领域的研究人员; 使用场景及目标:①应用于复杂三维环境下的无人机路径规划,如城市物流、灾害救援、军事侦察等场景;②实现飞行安全、能耗优化、路径平滑与实时避障等多目标协同优化;③为智能无人系统的自主决策与环境适应能力提供算法支持; 阅读建议:此资源结合理论模型与MATLAB实践,建议读者在理解ACO与MLP基本原理的基础上,结合代码示例进行仿真调试,重点关注ACO-MLP融合机制、多目标优化函数设计及参数自适应策略的实现,以深入掌握混合智能算法在工程中的应用方法。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值