重构:将示例文件组织到子目录

修复 Go 规范问题:一个目录不能有多个 package main

文件结构调整:
- example/concurrent_example.go → example/concurrent/main.go
- example/index_example.go → example/index/main.go
- example/topic_processor_example.go → example/topic_processor/main.go

修复 API 适配:
- index/main.go: 更新为新的查询 API(移除 startIdx/endIdx 参数)
- webapp/main.go: 使用 processor.Query 方法替代 RecordQuery
  - 移除 queryCache,直接使用 processor
  - 更新查询调用,移除状态参数

文档更新:
- example/README.md: 更新所有示例的运行路径
- example/RUN_CONCURRENT.md: 更新运行命令

所有示例编译测试通过 
This commit is contained in:
2025-10-04 01:27:41 +08:00
parent 4ec153c1ac
commit 3d82a6845e
6 changed files with 34 additions and 39 deletions

View File

@@ -8,17 +8,14 @@ import (
"net/http"
"os"
"strconv"
"sync"
"time"
"code.tczkiot.com/seqlog"
)
var (
seq *seqlog.Seqlog
logger *slog.Logger
queryCache = make(map[string]*seqlog.RecordQuery)
queryCacheMu sync.RWMutex
seq *seqlog.Seqlog
logger *slog.Logger
)
func main() {
@@ -522,20 +519,12 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
forward = 10
}
// 从缓存中获取或创建 query 对象
queryCacheMu.Lock()
query, exists := queryCache[topic]
if !exists {
var err error
query, err = seq.NewTopicQuery(topic)
if err != nil {
queryCacheMu.Unlock()
http.Error(w, err.Error(), http.StatusNotFound)
return
}
queryCache[topic] = query
// 获取 processor
processor, err := seq.GetProcessor(topic)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
queryCacheMu.Unlock()
// 获取当前处理索引和读取索引
startIdx := seq.GetProcessingIndex(topic)
@@ -546,7 +535,7 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
// 向后查询
if backward > 0 && startIdx > 0 {
backResults, err := query.QueryNewest(startIdx-1, backward, startIdx, endIdx)
backResults, err := processor.QueryNewest(startIdx-1, backward)
if err == nil {
results = append(results, backResults...)
}
@@ -554,7 +543,7 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
// 当前位置
if startIdx < endIdx {
currentResults, err := query.QueryOldest(startIdx, 1, startIdx, endIdx)
currentResults, err := processor.QueryOldest(startIdx, 1)
if err == nil {
results = append(results, currentResults...)
}
@@ -562,7 +551,7 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
// 向前查询
if forward > 0 {
forwardResults, err := query.QueryOldest(endIdx, forward, startIdx, endIdx)
forwardResults, err := processor.QueryOldest(endIdx, forward)
if err == nil {
results = append(results, forwardResults...)
}