连接池,redis,和配置文件
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
@@ -34,6 +35,7 @@ func init() {
|
||||
httpClient := gclient.New()
|
||||
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
|
||||
httpClient.SetHeader("Content-Type", "application/json")
|
||||
httpClient.SetTimeout(60 * time.Second) // RAGFlow AI 推理需要较长时间
|
||||
|
||||
globalClient = &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
|
||||
@@ -2,58 +2,28 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/redis"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/os/grpool"
|
||||
)
|
||||
|
||||
// WorkerPool RAGFlow 请求处理协程池
|
||||
// 默认协程池大小
|
||||
const defaultPoolSize = 200
|
||||
|
||||
// workerPool 协程池单例(grpool.New 是原型模式,需要变量引用)
|
||||
var workerPool = grpool.New(defaultPoolSize)
|
||||
|
||||
// WorkerPool RAGFlow 请求处理协程池(封装 grpool)
|
||||
type WorkerPool struct {
|
||||
pool *grpool.Pool
|
||||
size int
|
||||
}
|
||||
|
||||
// 单例模式相关变量
|
||||
var (
|
||||
workerPoolInstance *WorkerPool
|
||||
workerPoolOnce sync.Once
|
||||
)
|
||||
|
||||
// GetWorkerPoolWithSize 获取指定大小的协程池单例
|
||||
// 使用 sync.Once 确保只创建一次,size 仅首次调用生效
|
||||
func GetWorkerPoolWithSize(size int) *WorkerPool {
|
||||
workerPoolOnce.Do(func() {
|
||||
if size <= 0 {
|
||||
size = 200 // 默认大小
|
||||
}
|
||||
workerPoolInstance = &WorkerPool{
|
||||
pool: grpool.New(size),
|
||||
size: size,
|
||||
}
|
||||
})
|
||||
return workerPoolInstance
|
||||
}
|
||||
|
||||
// GetWorkerPool 获取协程池单例(使用默认大小 200)
|
||||
func GetWorkerPool() *WorkerPool {
|
||||
return GetWorkerPoolWithSize(200)
|
||||
}
|
||||
|
||||
// NewWorkerPool 创建协程池(兼容旧代码,内部使用单例)
|
||||
// 参数:
|
||||
// - size: 协程池大小,仅首次调用生效
|
||||
//
|
||||
// 返回:
|
||||
// - *WorkerPool: 协程池单例实例
|
||||
// - error: 创建失败时返回错误
|
||||
func NewWorkerPool(size int) (*WorkerPool, error) {
|
||||
if size <= 0 {
|
||||
return nil, gerror.New("协程池大小必须大于0")
|
||||
}
|
||||
return GetWorkerPoolWithSize(size), nil
|
||||
// Pool 协程池单例实例(直接引用使用)
|
||||
var Pool = &WorkerPool{
|
||||
pool: workerPool,
|
||||
size: defaultPoolSize,
|
||||
}
|
||||
|
||||
// Submit 提交任务到协程池
|
||||
|
||||
Reference in New Issue
Block a user