Dify 插件守护进程运行时执行机制详解
1. 概述
Dify Plugin Daemon 是一个用 Go 语言编写的服务,负责管理插件的完整生命周期。它支持三种不同的运行时模式,每种模式针对不同的使用场景进行了优化。
1.1 运行时类型总览
|
|
|
|
|
|---|---|---|---|
| 「Local Runtime」 |
|
|
|
| 「Debug Runtime」 |
|
|
|
| 「Serverless Runtime」 |
|
|
|
2. 系统架构
2.1 整体架构图
┌─────────────────────────────────────┐
│ Dify API Server │
│ (Python/Flask) │
└──────────────┬────────────────────┘
│ HTTP Request
▼
┌────────────────────────────────────────────────────────────────────────────┐
│ Plugin Daemon (Go) │
├────────────────────────────────────────────────────────────────────────────┤
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ HTTP │ │ Session │ │ Plugin │ │ Backwards │ │
│ │ Server │──│ Manager │──│ Manager │──│ Invocation │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │ │ │
│ │ ┌─────────────────────┼─────────────────────┐ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Control │ │ Local │ │ Debug │ │ Serverless │ │
│ │ Panel │ │ Runtime │ │ Runtime │ │ Runtime │ │
│ └──────────────┘ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │
└──────────────────────────┼─────────────────┼─────────────────┼────────────┘
│ │ │
▼ ▼ ▼
┌────────────┐ ┌────────────┐ ┌────────────┐
│ Plugin │ │ TCP │ │ HTTP │
│ Process │ │ Client │ │ Endpoint │
│ (Python) │ │ (Dev IDE) │ │ (Lambda) │
└────────────┘ └────────────┘ └────────────┘
2.2 核心组件说明
|
|
|
|---|---|
| 「HTTP Server」 |
|
| 「Session Manager」 |
|
| 「Plugin Manager」 |
|
| 「Control Panel」 |
|
| 「Backwards Invocation」 |
|
3. Local Runtime(本地运行时)
3.1 架构设计
本地运行时是生产环境的默认模式,通过子进程方式启动插件,使用标准输入输出进行通信。
┌─────────────────────────────────────────────────────────────────┐
│ Plugin Daemon Process │
├─────────────────────────────────────────────────────────────────┤
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Plugin Manager │ │ IO Tunnel │ │
│ │ │ │ │ │
│ │ - Install │ │ - Message │ │
│ │ - Uninstall │ │ Encoding │ │
│ │ - Start/Stop │ │ - Message │ │
│ │ - Health Check │ │ Decoding │ │
│ └────────┬────────┘ └────────┬────────┘ │
│ │ │ │
│ │ ┌───────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Process Spawner │ │
│ │ │ │
│ │ os/exec.Command("python", "-m", "main") │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ STDIN │ │ STDOUT │ │ │
│ │ │ (Write) │ │ (Read) │ │ │
│ │ └──────┬──────┘ └──────┬──────┘ │ │
│ └─────────┼────────────────┼──────────────────┘ │
└────────────┼────────────────┼───────────────────────────────────┘
│ │
│ Pipe │ Pipe
▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ Plugin Process (Python) │
├─────────────────────────────────────────────────────────────────┤
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ sys.stdin │ │ sys.stdout │ │
│ │ (Read) │ │ (Write) │ │
│ └────────┬────────┘ └────────┬────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Message Handler │ │
│ │ │ │
│ │ - Parse Request │ │
│ │ - Route to Tool/Model/Extension │ │
│ │ - Execute Plugin Logic │ │
│ │ - Format Response │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Plugin Implementation │ │
│ │ │ │
│ │ - Tool: GoogleSearchTool._invoke() │ │
│ │ - Model: OpenAILLM._invoke() │ │
│ │ - Extension: MyExtension.handle() │ │
│ └─────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
3.2 执行流程
3.2.1 插件启动流程
1. Plugin Daemon 接收启动请求
│
▼
2. Plugin Manager 检查插件状态
│
▼
3. 准备 Python 运行环境
- 设置 PYTHON_INTERPRETER_PATH
- 配置虚拟环境
- 安装依赖 (uv/pip)
│
▼
4. 创建子进程
cmd := exec.Command(pythonPath, "-m", "main")
cmd.Stdin = stdinPipe
cmd.Stdout = stdoutPipe
cmd.Stderr = stderrPipe
│
▼
5. 启动进程并建立通信通道
cmd.Start()
│
▼
6. 注册到 Session Manager
│
▼
7. 返回启动成功
3.2.2 请求处理流程
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
│ Dify │ │ Plugin │ │ IO │ │ Plugin │
│ API │ │ Daemon │ │ Tunnel │ │ Process │
└────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘
│ │ │ │
│ HTTP Request │ │ │
│───────────────►│ │ │
│ │ │ │
│ │ Encode Msg │ │
│ │───────────────►│ │
│ │ │ │
│ │ │ Write STDIN │
│ │ │───────────────►│
│ │ │ │
│ │ │ │ Execute
│ │ │ │ Plugin
│ │ │ │ Logic
│ │ │ │
│ │ │ Read STDOUT │
│ │ │◄───────────────│
│ │ │ │
│ │ Decode Msg │ │
│ │◄───────────────│ │
│ │ │ │
│ HTTP Response │ │ │
│◄───────────────│ │ │
│ │ │ │
3.3 消息协议
本地运行时使用基于行的 JSON 消息协议:
3.3.1 请求消息格式
{
"type": "invoke",
"session_id": "uuid-session-id",
"request_id": "uuid-request-id",
"plugin_type": "tool",
"action": "invoke",
"data": {
"tool_name": "google_search",
"parameters": {
"query": "Dify AI platform"
},
"credentials": {
"api_key": "encrypted_key"
}
}
}
3.3.2 响应消息格式
{
"type": "response",
"session_id": "uuid-session-id",
"request_id": "uuid-request-id",
"status": "success",
"data": {
"type": "text",
"content": "Search results..."
}
}
3.3.3 流式响应格式
{"type": "stream_start", "session_id": "...", "request_id": "..."}
{"type": "stream_chunk", "data": {"content": "Hello"}}
{"type": "stream_chunk", "data": {"content": " World"}}
{"type": "stream_end", "data": {"total_tokens": 100}}
3.4 进程管理
3.4.1 健康检查
// 伪代码示意
func(r *LocalRuntime)HealthCheck()error {
// 发送心跳消息
heartbeat := Message{Type: "heartbeat"}
r.stdin.Write(heartbeat.Encode())
// 等待响应,超时时间 5 秒
select {
case resp := <-r.stdout:
if resp.Type == "heartbeat_ack" {
returnnil
}
case <-time.After(5 * time.Second):
return ErrHealthCheckTimeout
}
}
3.4.2 资源限制
# manifest.yaml 中的资源配置
resource:
memory:268435456# 256MB 内存限制
permission:
tool:
enabled:true
storage:
enabled:true
size:1048576# 1MB 存储限制
3.5 Python 端实现
# Plugin SDK 中的本地运行时处理
import sys
import json
classLocalRuntimeHandler:
def__init__(self):
self.stdin = sys.stdin
self.stdout = sys.stdout
defrun(self):
"""主循环:读取请求,处理,返回响应"""
whileTrue:
# 从 STDIN 读取一行 JSON
line = self.stdin.readline()
ifnot line:
break
try:
request = json.loads(line)
response = self.handle_request(request)
# 写入响应到 STDOUT
self.stdout.write(json.dumps(response) + "\n")
self.stdout.flush()
except Exception as e:
self.send_error(str(e))
defhandle_request(self, request: dict) -> dict:
"""路由请求到对应的处理器"""
action = request.get("action")
if action == "invoke":
return self.invoke_plugin(request)
elif action == "validate":
return self.validate_credentials(request)
elif action == "heartbeat":
return {"type": "heartbeat_ack"}
else:
raise ValueError(f"Unknown action: {action}")
4. Debug Runtime(调试运行时)
4.1 架构设计
调试运行时用于插件开发阶段,通过 TCP 连接实现远程调试。
┌─────────────────────────────────────────────────────────────────┐
│ Dify Cloud / Self-hosted │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Plugin Daemon │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ Debug Runtime Server │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────┐ ┌─────────────────────────┐ │ │ │
│ │ │ │ TCP Server │ │ Connection Manager │ │ │ │
│ │ │ │ Port: 5003 │ │ │ │ │ │
│ │ │ │ │ │ - Auth Validation │ │ │ │
│ │ │ │ Listen() │ │ - Session Tracking │ │ │ │
│ │ │ │ Accept() │ │ - Message Routing │ │ │ │
│ │ │ └──────┬──────┘ └────────────┬────────────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ │ └────────────┬───────────┘ │ │ │
│ │ │ │ │ │ │
│ │ └──────────────────────┼───────────────────────────┘ │ │
│ │ │ │ │
│ └─────────────────────────┼───────────────────────────────┘ │
│ │ │
└────────────────────────────┼─────────────────────────────────────┘
│
│ TCP Connection
│ (Full Duplex)
│
┌────────────────────────────┼─────────────────────────────────────┐
│ │ │
│ ┌─────────────────────────▼───────────────────────────────┐ │
│ │ Debug Plugin Client │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ TCP Client │ │ │
│ │ │ │ │ │
│ │ │ Connect(host:port) │ │ │
│ │ │ Authenticate(api_key) │ │ │
│ │ │ SendReceive(messages) │ │ │
│ │ └──────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ Plugin Implementation │ │ │
│ │ │ │ │ │
│ │ │ - Hot Reload Support │ │ │
│ │ │ - Breakpoint Debugging │ │ │
│ │ │ - Real-time Logging │ │ │
│ │ └──────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │
│ Developer's Local Machine │
└──────────────────────────────────────────────────────────────────┘
4.2 连接建立流程
┌──────────┐ ┌──────────┐
│ Debug │ │ Plugin │
│ Client │ │ Daemon │
└────┬─────┘ └────┬─────┘
│ │
│ 1. TCP Connect (host:5003) │
│────────────────────────────────────────►│
│ │
│ 2. Connection Accepted │
│◄────────────────────────────────────────│
│ │
│ 3. Auth Request │
│ { │
│ "type": "auth", │
│ "api_key": "debug-key-xxx", │
│ "plugin_id": "my-plugin" │
│ } │
│────────────────────────────────────────►│
│ │
│ 4. Validate Key │
│ 5. Register │
│ Session │
│ │
│ 6. Auth Response │
│ { │
│ "type": "auth_success", │
│ "session_id": "uuid" │
│ } │
│◄────────────────────────────────────────│
│ │
│ 7. Ready for Requests │
│◄───────────────────────────────────────►│
│ │
4.3 配置方式
4.3.1 环境变量配置
# .env 文件
INSTALL_METHOD=remote
REMOTE_INSTALL_HOST=debug-plugin.dify.dev # 或自托管地址
REMOTE_INSTALL_PORT=5003
REMOTE_INSTALL_KEY=your-debug-api-key
4.3.2 获取调试凭据
-
登录 Dify 平台 -
点击右上角 “Plugins” 图标 -
点击调试图标(虫子图标) -
复制 API Key 和 Host Address
4.4 通信协议
调试运行时使用 TCP 全双工通信,消息格式与本地运行时类似,但增加了调试相关的消息类型:
4.4.1 调试专用消息类型
// 日志消息
{
"type": "log",
"level": "debug",
"message": "Processing request...",
"timestamp": "2024-01-01T00:00:00Z"
}
// 断点命中
{
"type": "breakpoint",
"file": "tools/search.py",
"line": 42,
"variables": {
"query": "test search"
}
}
// 热重载通知
{
"type": "reload",
"status": "success",
"message": "Plugin reloaded successfully"
}
4.5 Python 端实现
import socket
import json
import threading
classDebugRuntimeClient:
def__init__(self, host: str, port: int, api_key: str):
self.host = host
self.port = port
self.api_key = api_key
self.socket = None
self.session_id = None
defconnect(self):
"""建立 TCP 连接并认证"""
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host, self.port))
# 发送认证请求
auth_msg = {
"type": "auth",
"api_key": self.api_key,
"plugin_id": self.plugin_id
}
self._send(auth_msg)
# 等待认证响应
response = self._receive()
if response.get("type") == "auth_success":
self.session_id = response.get("session_id")
print(f"Connected with session: {self.session_id}")
else:
raise AuthenticationError("Authentication failed")
defrun(self):
"""主循环:处理来自服务器的请求"""
whileTrue:
try:
request = self._receive()
if request:
response = self.handle_request(request)
self._send(response)
except ConnectionError:
self.reconnect()
def_send(self, data: dict):
"""发送消息"""
message = json.dumps(data) + "\n"
self.socket.sendall(message.encode())
def_receive(self) -> dict:
"""接收消息"""
buffer = b""
whileb"\n"notin buffer:
chunk = self.socket.recv(4096)
ifnot chunk:
raise ConnectionError("Connection closed")
buffer += chunk
line, _ = buffer.split(b"\n", 1)
return json.loads(line.decode())
4.6 调试特性
|
|
|
|---|---|
| 「热重载」 |
|
| 「实时日志」 |
|
| 「断点调试」 |
|
| 「变量检查」 |
|
| 「请求追踪」 |
|
5. Serverless Runtime(无服务器运行时)
5.1 架构设计
无服务器运行时支持将插件部署到 AWS Lambda 等云函数平台。
┌─────────────────────────────────────────────────────────────────┐
│ Dify Platform │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Plugin Daemon │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ Serverless Connector │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────┐ ┌─────────────────────────┐ │ │ │
│ │ │ │ HTTP Client │ │ Request Builder │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ POST() │ │ - Serialize Request │ │ │ │
│ │ │ │ Timeout │ │ - Add Auth Headers │ │ │ │
│ │ │ │ Retry │ │ - Handle Response │ │ │ │
│ │ │ └──────┬──────┘ └────────────┬────────────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ │ └────────────┬───────────┘ │ │ │
│ │ │ │ │ │ │
│ │ └──────────────────────┼───────────────────────────┘ │ │
│ │ │ │ │
│ └─────────────────────────┼───────────────────────────────┘ │
│ │ │
└────────────────────────────┼─────────────────────────────────────┘
│
│ HTTPS Request
│
┌────────────────────┼────────────────────┐
│ │ │
▼ ▼ ▼
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ AWS Lambda │ │ Google Cloud │ │ Azure │
│ │ │ Functions │ │ Functions │
│ ┌────────┐ │ │ ┌────────┐ │ │ ┌────────┐ │
│ │ Plugin │ │ │ │ Plugin │ │ │ │ Plugin │ │
│ │ Handler│ │ │ │ Handler│ │ │ │ Handler│ │
│ └────────┘ │ │ └────────┘ │ │ └────────┘ │
└──────────────┘ └──────────────┘ └──────────────┘
5.2 请求处理流程
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
│ Dify │ │ Plugin │ │ Serverless│ │ Lambda │
│ API │ │ Daemon │ │ Connector │ │ Function │
└────┬─────┘ └────┬─────┘ └────┬──────┘ └────┬─────┘
│ │ │ │
│ Plugin Invoke │ │ │
│───────────────►│ │ │
│ │ │ │
│ │ Build Request │ │
│ │───────────────►│ │
│ │ │ │
│ │ │ HTTP POST │
│ │ │───────────────►│
│ │ │ │
│ │ │ │ Cold Start
│ │ │ │ (if needed)
│ │ │ │
│ │ │ │ Execute
│ │ │ │ Handler
│ │ │ │
│ │ │ HTTP Response │
│ │ │◄───────────────│
│ │ │ │
│ │ Parse Response│ │
│ │◄───────────────│ │
│ │ │ │
│ Plugin Result │ │ │
│◄───────────────│ │ │
│ │ │ │
5.3 SRI (Serverless Runtime Interface)
5.3.1 请求格式
POST /invoke HTTP/1.1
Host: lambda-endpoint.amazonaws.com
Content-Type: application/json
X-Dify-Plugin-Session: session-id
X-Dify-Plugin-Signature: hmac-signature
{
"action": "invoke",
"plugin_type": "tool",
"tool_name": "google_search",
"parameters": {
"query": "Dify AI"
},
"credentials": {
"api_key": "..."
},
"context": {
"user_id": "user-123",
"app_id": "app-456"
}
}
5.3.2 响应格式
{
"status": "success",
"data": {
"type": "text",
"content": "Search results..."
},
"usage": {
"execution_time_ms": 1500,
"memory_used_mb": 128
}
}
5.3.3 流式响应
对于需要流式输出的场景,使用 Server-Sent Events (SSE):
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
event: start
data: {"session_id": "..."}
event: chunk
data: {"content": "Hello"}
event: chunk
data: {"content": " World"}
event: end
data: {"total_tokens": 100}
5.4 AWS Lambda 部署示例
5.4.1 Handler 实现
# lambda_handler.py
import json
from dify_plugin import Tool
deflambda_handler(event, context):
"""AWS Lambda 入口函数"""
try:
# 解析请求
body = json.loads(event.get('body', '{}'))
action = body.get('action')
if action == 'invoke':
return handle_invoke(body)
elif action == 'validate':
return handle_validate(body)
else:
return {
'statusCode': 400,
'body': json.dumps({'error': 'Unknown action'})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
defhandle_invoke(body: dict) -> dict:
"""处理插件调用"""
tool_name = body.get('tool_name')
parameters = body.get('parameters', {})
credentials = body.get('credentials', {})
# 实例化工具并执行
tool = get_tool_instance(tool_name)
result = tool._invoke(parameters, credentials)
return {
'statusCode': 200,
'body': json.dumps({
'status': 'success',
'data': result.to_dict()
})
}
5.4.2 部署配置
# serverless.yml
service:dify-plugin-example
provider:
name:aws
runtime:python3.12
region:us-east-1
memorySize:256
timeout:30
functions:
plugin:
handler:lambda_handler.lambda_handler
events:
-http:
path:/invoke
method:post
cors:true
plugins:
-serverless-python-requirements
custom:
pythonRequirements:
dockerizePip:true
5.5 特性对比
|
|
|
|
|
|---|---|---|---|
| 「冷启动」 |
|
|
|
| 「扩展性」 |
|
|
|
| 「成本」 |
|
|
|
| 「延迟」 |
|
|
|
| 「调试」 |
|
|
|
| 「部署」 |
|
|
|
6. 运行时选择策略
6.1 决策流程图
┌─────────────────┐
│ 开始选择 │
│ 运行时模式 │
└────────┬────────┘
│
▼
┌─────────────────┐
│ 是否在开发 │
│ 调试阶段? │
└────────┬────────┘
│
┌──────────────┴──────────────┐
│ Yes │ No
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Debug Runtime │ │ 需要弹性 │
│ │ │ 扩展吗? │
│ - 热重载 │ └────────┬────────┘
│ - 实时日志 │ │
│ - 断点调试 │ ┌──────────┴──────────┐
└─────────────────┘ │ Yes │ No
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Serverless │ │ Local Runtime │
│ Runtime │ │ │
│ │ │ - 低延迟 │
│ - 自动扩展 │ │ - 资源可控 │
│ - 按需计费 │ │ - 简单部署 │
└─────────────────┘ └─────────────────┘
6.2 使用场景推荐
|
|
|
|
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7. 高级特性
7.1 回调机制 (Backwards Invocation)
插件可以回调 Dify API 获取额外信息:
classMyTool(Tool):
def_invoke(self, parameters: dict) -> ToolInvokeMessage:
# 回调获取应用信息
app_info = self.session.app.get_info()
# 回调获取用户信息
user_info = self.session.user.get_info()
# 回调存储数据
self.session.storage.set("key", "value")
return self.create_text_message("Done")
7.2 会话管理
// Session Manager 核心功能
type SessionManager struct {
sessions map[string]*Session
mutex sync.RWMutex
}
type Session struct {
ID string
PluginID string
Runtime RuntimeType
CreatedAt time.Time
LastActive time.Time
State SessionState
}
func(sm *SessionManager)CreateSession(pluginID string, runtime RuntimeType) *Session {
session := &Session{
ID: uuid.New().String(),
PluginID: pluginID,
Runtime: runtime,
CreatedAt: time.Now(),
State: StateActive,
}
sm.mutex.Lock()
sm.sessions[session.ID] = session
sm.mutex.Unlock()
return session
}
7.3 错误处理
# 插件端错误处理
classPluginError(Exception):
"""插件基础错误"""
pass
classCredentialsValidationError(PluginError):
"""凭证验证失败"""
pass
classRateLimitError(PluginError):
"""速率限制"""
pass
classTimeoutError(PluginError):
"""执行超时"""
pass
# 错误响应格式
{
"status": "error",
"error": {
"type": "CredentialsValidationError",
"message": "Invalid API key",
"code": "INVALID_CREDENTIALS"
}
}
8. 性能优化
8.1 Local Runtime 优化
# 环境变量配置
PLUGIN_MAX_EXECUTION_TIMEOUT:2400# 最大执行超时(秒)
PYTHON_ENV_INIT_TIMEOUT:640# Python 环境初始化超时
PLUGIN_WORKER_NUM:4# 工作进程数
8.2 连接池管理
// HTTP 连接池配置(Serverless Runtime)
var httpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
},
Timeout: 30 * time.Second,
}
8.3 缓存策略
// 插件资源缓存
type AssetCache struct {
cache *lru.Cache
size int
}
func(c *AssetCache)Get(key string)([]byte, bool) {
if val, ok := c.cache.Get(key); ok {
return val.([]byte), true
}
returnnil, false
}
9. 安全机制
9.1 进程隔离
-
每个插件运行在独立进程中 -
使用 cgroups 限制资源使用 -
文件系统访问限制
9.2 认证机制
// Debug Runtime 认证
func(s *DebugServer)Authenticate(conn net.Conn, apiKey string)error {
// 验证 API Key
if !s.validateAPIKey(apiKey) {
return ErrInvalidAPIKey
}
// 检查速率限制
if s.isRateLimited(conn.RemoteAddr()) {
return ErrRateLimited
}
returnnil
}
9.3 数据加密
-
凭证在传输和存储时加密 -
使用 TLS 加密 TCP/HTTP 通信 -
敏感日志脱敏处理
10. 总结
Dify Plugin Daemon 的三种运行时模式各有特点:
|
|
|
|
|---|---|---|
| 「Local」 |
|
|
| 「Debug」 |
|
|
| 「Serverless」 |
|
|
这种多运行时架构设计使 Dify 能够灵活适应不同的部署场景和开发需求,同时保持了统一的插件开发体验。
夜雨聆风
