{"id":42701352,"url":"https://github.com/storage-lock/go-storage","last_synced_at":"2026-01-29T14:20:14.635Z","repository":{"id":186148949,"uuid":"674630960","full_name":"storage-lock/go-storage","owner":"storage-lock","description":"Storage Lock中的Lock是要存储在Storage上的，这个仓库就是定义了Storage的相关实现规范。","archived":false,"fork":false,"pushed_at":"2023-08-07T14:09:19.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-01-27T17:05:13.883Z","etag":null,"topics":["storage","storage-api","storage-engine"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/storage-lock.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-08-04T12:15:11.000Z","updated_at":"2023-08-07T16:35:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"c9473d34-d60f-4169-a73b-17f85ed8e0b3","html_url":"https://github.com/storage-lock/go-storage","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"634131be858244a0c2a11b4aa26e35e6b9beb138"},"previous_names":["storage-lock/go-storage"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/storage-lock/go-storage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storage-lock%2Fgo-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storage-lock%2Fgo-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storage-lock%2Fgo-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storage-lock%2Fgo-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/storage-lock","download_url":"https://codeload.github.com/storage-lock/go-storage/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/storage-lock%2Fgo-storage/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28879332,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-29T10:31:27.438Z","status":"ssl_error","status_checked_at":"2026-01-29T10:31:01.017Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["storage","storage-api","storage-engine"],"created_at":"2026-01-29T14:20:13.728Z","updated_at":"2026-01-29T14:20:14.581Z","avatar_url":"https://github.com/storage-lock.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Storage的接口定义\n\n# 一、这是什么\n\nStorage Lock中的Lock是要存储在Storage上的，这个仓库就是定义了Storage的相关实现规范。\n\n# 二、安装依赖\n\n```bash\ngo get -u github.com/storage-lock/go-storage\n```\n\n# 三、组件简介\n\n## LockInformation\n\nLockInformation用于表示锁相关的信息，包括锁当前被谁持有，如果是重入锁的话当前的深度是多少，锁被获取的事件是啥时候，锁过期的时间是啥时候，锁每次被修改的时候版本号都会增加，那么锁的当前的版本号也会记录在LockInformation中。\n\n```go\n// LockInformation 锁的相关信息，是要持久化保存到相关介质中的\ntype LockInformation struct {\n\n\t// 锁的ID\n\tLockId string `json:\"lock_id\"`\n\n\t// 当前时谁在持有这个锁，是一个全局唯一的ID\n\tOwnerId string `json:\"owner_id\"`\n\n\t// 锁的变更版本号，乐观锁避免CAS的ABA问题\n\tVersion Version `json:\"version\"`\n\n\t// 锁被锁定了几次，是为了支持可重入锁，在释放锁的时候会根据加锁的次数来决定是否真正的释放锁还是就减少一次锁定次数\n\tLockCount int `json:\"lock_count\"`\n\n\t// 这个锁是从啥时候开始被OwnerId所持有的，用于判断持有锁的时间\n\tLockBeginTime time.Time `json:\"lock_begin_time\"`\n\n\t// 锁的owner持有此锁的租约过期时间，\n\tLeaseExpireTime time.Time `json:\"lease_expire_time\"`\n}\n```\n\n## Storage\n\n定义了Storage的相关规范与功能，Storage的作用是用于把上面定义的锁的信息持久化存储。\n\n```go\npackage storage\n\nimport (\n\t\"context\"\n\t\"github.com/golang-infrastructure/go-iterator\"\n)\n\n// Version 表示一个锁的版本号，锁在每次被更改状态，比如每次被持有释放的时候都会增加版本号\ntype Version uint64\n\n// Storage 表示一个存储介质的实现，要实现四个增删改查的方法和一个初始化的方法，以及能够提供Storage的日期，\n// 因为在分布式系统中日期很重要，必须保证参与分布式运算的各个节点使用相同的时间\ntype Storage interface {\n\n\t// GetName Storage的名称，用于区分不同的Storage的实现\n\t// Returns:\n\t//     string: Storage的名字，应该返回一个有辨识度并且简单易懂的名字，名字不能为空，否则认为是不合法的Storage实现\n\tGetName() string\n\n\t// Init 初始化操作，比如创建存储锁的表，需要支持多次调用，每次创建Storage的时候会调用此方法初始化\n\t// Params:\n\t//     ctx:\n\t// Returns:\n\t//    error: 初始化发生错误时返回对应的错误\n\tInit(ctx context.Context) error\n\n\t// UpdateWithVersion 如果存储的是指定版本的话，则将其更新\n\t// Params:\n\t//     lockId 表示锁的ID\n\t//     exceptedValue 仅当老的值为这个时才进行更新\n\t//     newValue 更新为的新的值\n\t// Returns:\n\t//    error: 如果是版本不匹配，则返回错误 ErrVersionMiss，如果是其它类型的错误，依据情况自行返回\n\tUpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion Version, lockInformation *LockInformation) error\n\n\t// CreateWithVersion 尝试将锁的信息插入到存储介质中，返回是否插入成功，底层存储的时候应该将锁的ID作为唯一ID，不能重复存储\n\t// 也就是说这个方法仅在锁不存在的时候才能执行成功，其它情况应该插入失败返回对应的错误\n\t// Params:\n\t//     ctx:\n\t//     lockId:\n\t//     version:\n\t//     lockInformation:\n\t// Returns:\n\t//     error:\n\tCreateWithVersion(ctx context.Context, lockId string, version Version, lockInformation *LockInformation) error\n\n\t// DeleteWithVersion 如果锁的当前版本是期望的版本，则将其删除\n\t// 如果是版本不匹配，则返回错误 ErrVersionMiss，如果是其它类型的错误，依据情况自行返回\n\t//\n\tDeleteWithVersion(ctx context.Context, lockId string, exceptedVersion Version, lockInformation *LockInformation) error\n\n\t// Get 获取锁之前存储的值，如果没有的话则返回空字符串，如果发生了错误则返回对应的错误信息，如果正常返回则是LockInformation的JSON字符串\n\t// Params:\n\t//     ctx: 用来做超时控制之类的\n\t//     lockId: 要查询的锁的ID\n\t// Returns:\n\t//    string:\n\t//    error:\n\tGet(ctx context.Context, lockId string) (string, error)\n\n\t// TimeProvider 分布式锁的话时间必须使用统一的时间，这个时间推荐是以Storage的时间为准，Storage要能够提供时间查询的功能\n\t// 这是因为分布式锁的算法需要根据时间来协调推进，而当时间不准确的时候算法可能会失效从而导致锁失效\n\t// TODO 2023-5-15 01:48:19 基于实例的时间在分布式数据库中可能会失效，单实例没问题\n\t// TODO 2023-8-3 21:53:35 在文档中用实际例子演示分布式情况下可能会存在的问题\n\t// Params:\n\t//     ctx: 用来做超时控制之类的\n\t// Returns:\n\t//     time.Time: 返回Storage的当前时间\n\t//     error: 获取时间失败时则返回对应的错误\n\tTimeProvider\n\n\t// Close 关闭此存储介质，一般在系统退出释放资源的时候调用一下\n\t// Params:\n\t//     ctx: 用来做超时控制之类的\n\t// Returns:\n\t//     error: 如果关闭失败，则返回对应的错误\n\tClose(ctx context.Context) error\n\n\t// List 列出当前的Storage所持有的所有的锁的信息，因为数量可能会比较多，所以这里使用了一个迭代器模式\n\t// 虽然实际上可能用channel会更Golang一些，但是迭代器会比较易于实现并能够绑定一些内置的方法便于操作\n\t// Params:\n\t//     ctx: 用来做超时控制之类的\n\t// Returns:\n\t//     iterator.Iterator[*LockInformation]: 迭代器用来承载当前所有的锁\n\t//     error: 如果列出失败，则返回对应类型的错误\n\tList(ctx context.Context) (iterator.Iterator[*LockInformation], error)\n}\n```\n\n## TimeProvider\n\n分布式情况下涉及到不同节点的协同，它们之间的时间一致性很重要，Storage要实现TimeProvider接口，在分布式算法中作为时间源提供时间。\n\n```go\n// TimeProvider 能够提供时间的时间源，可以从数据库读取，也可以从NTP服务器上读取，只要能够返回一个准确的时间即可（其实不准确也可以，只要是统一的，一直往前的，不会出现时钟回拨的）\ntype TimeProvider interface {\n\n\t// GetTime 获取当前时间\n\tGetTime(ctx context.Context) (time.Time, error)\n}\n```\n\n## ConnectionManager\n\nConnectionManager用来管理与Storage的连接，包括连接的申请与回收，以及在系统退出时的资源销毁等等。\n\n```go\n// ConnectionManager 把与Storage的连接的管理抽象为一个组件，属于比较底层的接口，用来适配上层的各种情况\n// 比如上层可以是从DSN直接创建数据库连接，也可以是从一个已经存在的连接池中拿出来连接，甚至从已有的ORM、sqlx、sql.DB中复用连接\n// 或者任何你想扩展的实现，总之它是一个带泛型的接口，你可以根据你的需求发挥想象力任意创造！\ntype ConnectionManager[Connection any] interface {\n\n\t// Name 连接提供器的名字，用于区分不同的连接提供器，连接器的名字必须指定不允许为空字符串\n\tName() string\n\n\t// Take 获取一个往Storage的连接\n\tTake(ctx context.Context) (Connection, error)\n\n\t// Return 使用完毕，把Storage的连接归还，用于在一些从连接池中拿连接使用完毕必须手动释放否则会资源泄露的场景下及时释放资源\n\tReturn(ctx context.Context, connection Connection) error\n\n\t// Shutdown 把整个连接管理器关闭掉，彻底不用了，Storage Lock并不会调用这个方法，你应该在你的系统退出的时候调用此方法释放整个连接管理器使用到的资源\n\tShutdown(ctx context.Context) error\n}\n```\n\n对于ConnectionManager，内置了三个实现：\n\n- DsnConnectionManager：指定driverName和DSN创建一个连接，之后就一直使用这个连接\n\n- FixedSqlDBConnectionManager：每次都返回一个固定的sql.DB\n\n- FuncConnectionProvider：把上面的接口的几个方法使用函数式的方式包裹了一下，用于不方便声明struct的情况下用几个func组合出ConnectionManager接口\n\n# 四、实现示例\n\n下面是一些实现了Storage的例子：\n\n- https://github.com/storage-lock/go-mysql-storage\n- https://github.com/storage-lock/go-postgresql-storage\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstorage-lock%2Fgo-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstorage-lock%2Fgo-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstorage-lock%2Fgo-storage/lists"}