6e6b899071
CI / docker (push) Successful in 7m37s
引入用户端对话/生图与管理端 Provider 配置,统一 invoke 编排、Gateway、请求日志与模型管理;前端按设计规范拆分布局,Go module 更名为 github.com/rose_cat707/Atlas;生图长任务 SSE 推送;管理端登录防爆破;协议与服务类型联动;Replicate 模型同步修复;用户端创作台 UI 重构;管理端创作历史替代请求日志。 Co-authored-by: Cursor <cursoragent@cursor.com>
91 lines
1.6 KiB
Go
91 lines
1.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type writeJob func(ctx context.Context, db *Store) error
|
|
|
|
type WriteQueue struct {
|
|
store *Store
|
|
ch chan writeJob
|
|
wg sync.WaitGroup
|
|
interval time.Duration
|
|
stopCh chan struct{}
|
|
}
|
|
|
|
func NewWriteQueue(s *Store, flushInterval time.Duration) *WriteQueue {
|
|
wq := &WriteQueue{
|
|
store: s,
|
|
ch: make(chan writeJob, 256),
|
|
interval: flushInterval,
|
|
stopCh: make(chan struct{}),
|
|
}
|
|
wq.wg.Add(1)
|
|
go wq.run()
|
|
return wq
|
|
}
|
|
|
|
func (wq *WriteQueue) Enqueue(job writeJob) {
|
|
select {
|
|
case wq.ch <- job:
|
|
default:
|
|
go func() { wq.ch <- job }()
|
|
}
|
|
}
|
|
|
|
func (wq *WriteQueue) ForceFlush(ctx context.Context, job writeJob) error {
|
|
return job(ctx, wq.store)
|
|
}
|
|
|
|
func (wq *WriteQueue) run() {
|
|
defer wq.wg.Done()
|
|
ticker := time.NewTicker(wq.interval)
|
|
defer ticker.Stop()
|
|
pending := make([]writeJob, 0, 32)
|
|
flush := func() {
|
|
if len(pending) == 0 {
|
|
return
|
|
}
|
|
jobs := pending
|
|
pending = pending[:0]
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
for _, job := range jobs {
|
|
if err := job(ctx, wq.store); err != nil {
|
|
log.Printf("write queue: %v", err)
|
|
}
|
|
}
|
|
}
|
|
for {
|
|
select {
|
|
case <-wq.stopCh:
|
|
flush()
|
|
for {
|
|
select {
|
|
case job := <-wq.ch:
|
|
pending = append(pending, job)
|
|
default:
|
|
flush()
|
|
return
|
|
}
|
|
}
|
|
case job := <-wq.ch:
|
|
pending = append(pending, job)
|
|
if len(pending) >= 32 {
|
|
flush()
|
|
}
|
|
case <-ticker.C:
|
|
flush()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (wq *WriteQueue) Close() {
|
|
close(wq.stopCh)
|
|
wq.wg.Wait()
|
|
}
|