Files
Agent-System-Backend/internal/blobstore/blobstore.go
T
weichen 29f14ac5f2 Add multi-instance groundwork: blobstore seam for artifacts
Details (original message):
Add multi-instance groundwork: blobstore seam for artifacts, PG LISTEN/NOTIFY event fanout, and per-tenant rate limiting with native Redis backend
2026-09-30 02:39:50 +00:00

65 lines
1.7 KiB
Go

// Package blobstore 是工件对象存储抽象(阶段 3:多实例前置)。
// 默认本地目录档(单实例,零外部服务);多实例部署切共享卷路径或按本接口补
// 对象存储实现(S3 兼容等)——上层(store 工件方法)只依赖本接口。
package blobstore
import (
"fmt"
"os"
"path/filepath"
"sync"
)
// Store 是对象存储接口:Put 原子落盘并返回记录进库的引用(本地档=绝对路径,
// 对象存储档=对象键);Get/Delete 按该引用操作。
type Store interface {
Put(id string, content []byte) (ref string, err error)
Get(ref string) ([]byte, error)
Delete(ref string) error
}
// Local 是本地目录实现(data/artifacts;多实例时把目录指向共享卷即可)。
type Local struct {
dir string
mu sync.Mutex
}
func NewLocal(dir string) (*Local, error) {
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, err
}
return &Local{dir: dir}, nil
}
// Put 临时文件 → rename 原子写:崩溃只可能残留 .tmp 孤立文件,不产生半写对象。
func (l *Local) Put(id string, content []byte) (string, error) {
final := filepath.Join(l.dir, id)
tmp := final + ".tmp-" + id
if err := os.WriteFile(tmp, content, 0o644); err != nil {
return "", err
}
l.mu.Lock()
defer l.mu.Unlock()
if err := os.Rename(tmp, final); err != nil {
os.Remove(tmp)
return "", err
}
return final, nil
}
func (l *Local) Get(ref string) ([]byte, error) {
b, err := os.ReadFile(ref)
if os.IsNotExist(err) {
return nil, fmt.Errorf("blobstore: object %s not found", filepath.Base(ref))
}
return b, err
}
func (l *Local) Delete(ref string) error {
err := os.Remove(ref)
if os.IsNotExist(err) {
return nil
}
return err
}