nocobase-app-plugin-concurrency
它能做什么
这项技能配置 NocoBase 3 应用中仅限进程内的锁服务。适合需要防止两个任务同时修改同一内容的开发者。
适用场景
- ✓当两个请求可能同时修改同一条记录时。
- ✓当你需要在 NocoBase 服务器代码中使用命名锁时。
- ✓当你想在应用的配置文件中设置锁的参数时。
技能链接
https://github.com/nocobase/nocobase/blob/v3-develop/packages/plugins/app-plugin-concurrency/skills/nocobase-app-plugin-concurrency/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我添加一把锁,让两个用户不能同时执行库存更新,用于我的 NocoBase 应用。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会注册并发插件,在配置中设置命名锁管理器,并在需要的代码中使用锁服务。该锁只在一个正在运行的进程内生效。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Concurrency
Install and register
Install @nocobase/app-plugin-concurrency with compatible application-supplied @nocobase/app-server, @nocobase/service-provider and @nocobase/concurrency peers. Register the default /server definition before business plugins and register defineConcurrencyConfig() from /config under concurrency. Installation and both registrations are separate requirements.
The factory supplies default: memory and memory.adapter: mutex, declares CONCURRENCY_DEFAULT and exposes no browser fields. Entry fields are adapter, optional namespace (application name by default), optional ttl, and implementation-owned connection. Mutex forbids connection. TTL is a renewable lease duration, an integer from 1 through 2147483647 milliseconds. Local Mutex accepts and validates TTL but ignores it, with no holding timer, expiration release or local renewal. Resource TTL overrides configuration TTL; undefined retains configuration TTL. Unknown options and invalid selectors fail. Entries do not inherit from one another. Two enabled local entries cannot share a namespace.
Consume the original service
import { concurrencyServiceToken } from '@nocobase/app-plugin-concurrency/server';
const service = app.container.resolve(concurrencyServiceToken);
// Obtain managers in ready or normal running work.
const manager = await service.getManager();
const executor = await manager.createSynchronous('orders:sync');
const result = await executor.tryRun(async () => 'done', 500);
Inject the owner-created Token; never create a second Token or service. The same configuration name shares one manager. Same-name Lock and Executor handles compete in the same domain regardless of TTL. Use bounded, stable resource names: the local native Mutex table remains until instance shutdown. Local locks coordinate only within that instance, never across processes.
lock and run wait without a deadline until success, cancellation or failure. Omitted/zero positional try budgets use native tryAcquire, including its zero-millisecond timer window. Positive try budgets use native withTimeout, not a precise return deadline. Busy results come only from explicit native non-acquisition. Capture each call's signal once; cancellation before delivery must not affect other waiters. Cancellation after delivery does not release a Lease or stop a runner. Late native success after cancellation is released using its own releaser without delivery, runner execution or TTL timer.
Lease release is asynchronous and idempotent; concurrent calls share a result and failures permit explicit retry on the same handle while its instance remains usable. Executors combine Lock acquisition with a no-argument runner, wait for actual runner settlement and diagnose cleanup failure without replacing business values or exceptions. Local Mutex remains held beyond an explicit TTL until manual release, actual runner settlement or instance shutdown. TTL is not a runner timeout or a validity guarantee. Redis implementations use native automatic renewal for both Lock and Executor, not an Executor-owned renewal loop. Use application transactions and idempotency where required.
Lifecycle and extensions
The Provider owns initialization and shutdown. Default startup is eager; named configurations initialize lazily and concurrently share one initialization. Boot freezes the complete snapshot, including unused entries. Reload requires restart to apply changes and never changes active or lazy domains.
Applications stop dependent business work before Concurrency shutdown. Shutdown rejects new acquisitions, interrupts waits, observes issued native requests and actively releases confirmed Leases without draining runners. Release failures are diagnosed while cleanup continues. Concurrent shutdown shares a Promise and successful shutdown is idempotent. Failed owned-resource closure retains its handle for explicit owner service.shutdown() retry. A second app.shutdown() does not repeat cached Provider shutdown. No public operation, cleanup or overall shutdown deadline is configured.
Implementation authors use /server: ManagedLeaseRegistry, startAcquire, createSynchronousExecutor, encodeResourceKey and the original implementation/configuration types. The registry registers confirmed releasers synchronously and owns only Lease release and record disposal, not acquisition, pending work, TTL or renewal. `startAcquire(registry, { si
常见问题
锁能跨多台服务器生效吗?
本地锁只在一个进程内生效。技能说明了它的限制。
锁可以设置时间限制吗?
可以设置存活时间,但本地锁不会使用它。
锁会保存在数据库中吗?
不会。它是应用进程内部的锁服务。
详情
作者的原始描述
Configure and consume NocoBase 3 process-local Mutex locks, strict named managers, original service Token, optional TTL, public implementation extensions and application-owned shutdown.
repo: nocobase/nocobase
热度
- GitHub 星标
- 25k
- 排名
- #10094
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
nocobase-app-development
25k帮你的智能体为完整的 NocoBase 3 业务应用构建页面、路由、数据、权限和测试。
shadcn
25k帮你的智能体在 Web 项目中查找、添加、修复和美化 shadcn/ui 组件,并使用其官方文档和注册表。
nocobase-app-plugin-development
25k帮你的智能体在现有应用中创建和维护 NocoBase 3 插件,包括服务端、客户端、数据库和注册部分。
nocobase-app-upgrade
25k帮你的智能体把 NocoBase 应用升级到较新的模板版本,引入模板的更改,同时保留你自己的工作。