fix(worker): 单次任务固定同一 WeKnora 连接
TargetIdentity 与业务调用原先各自 Resolve:身份核验通过后,上传/轮询/验收/ 移除的后续调用仍可能在下一次解析时切到新实例。现在 BeginTask 只解析一次, 用同一连接读取对象租户并把该不可变连接钉进返回 context;后续上传、列库恢复、 查询、chunk 读取、删除与删除后重查全部复用它。 TaskConnector 优先于 IdentityProvider;仅实现 TargetIdentity 的窄假客户端仍可 工作,生产路径不会走双解析。新增假端点测试断言一次处理只解析一次、绑定后改 当前实例仍全部打到原实例;四个 handler 的 TaskConnector 假客户端断言所有业务 调用携带同一连接令牌,覆盖上传恢复和删除重查。
This commit is contained in:
@@ -15,15 +15,71 @@ var ErrNotConfigured = errors.New("WeKnora 有效端点/凭据未配置")
|
||||
// The readiness probe has its own 10s cap; it must not bound upload/poll calls.
|
||||
const defaultBusinessTimeout = 60 * time.Second
|
||||
|
||||
// Client resolves the effective WeKnora target on every call. It deliberately
|
||||
// owns no cached *weknora.Client: endpoint, key and binding are console-managed.
|
||||
// Client resolves the effective WeKnora target on every call. Direct calls are
|
||||
// intentionally dynamic so console-managed endpoint/key/binding changes take
|
||||
// effect. A job that performs several calls must call BeginTask first: it
|
||||
// resolves once, verifies the owning tenant and pins that immutable connection
|
||||
// in the returned context, so no later call can silently switch instances.
|
||||
type Client struct {
|
||||
Resolver readiness.Resolver
|
||||
Timeout time.Duration
|
||||
AllowDockerHTTP bool
|
||||
}
|
||||
|
||||
type taskConnection struct {
|
||||
remote *weknora.Client
|
||||
baseURL string
|
||||
}
|
||||
|
||||
type taskConnectionKey struct{}
|
||||
|
||||
func taskConnectionFrom(ctx context.Context) *taskConnection {
|
||||
connection, _ := ctx.Value(taskConnectionKey{}).(*taskConnection)
|
||||
return connection
|
||||
}
|
||||
|
||||
func (client *Client) newRemote(target readiness.Target) (*weknora.Client, error) {
|
||||
timeout := client.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultBusinessTimeout
|
||||
}
|
||||
return weknora.NewClient(weknora.ClientConfig{
|
||||
BaseURL: target.BaseURL,
|
||||
APIKey: target.APIKey,
|
||||
Timeout: timeout,
|
||||
AllowDockerHTTP: client.AllowDockerHTTP,
|
||||
})
|
||||
}
|
||||
|
||||
// BeginTask resolves the endpoint/key/binding exactly once, reads the owning
|
||||
// tenant through that same connection and returns a context that pins it.
|
||||
// Upload, poll, validate and remove must pass the returned context to every
|
||||
// follow-up call and re-read, including recovery and delete read-back.
|
||||
func (client *Client) BeginTask(ctx context.Context, knowledgeBaseID string) (context.Context, weknora.InstanceIdentity, error) {
|
||||
target, err := client.Resolver.Resolve(ctx)
|
||||
if err != nil {
|
||||
return ctx, weknora.InstanceIdentity{}, err
|
||||
}
|
||||
if target.BaseURL == "" || target.APIKey == "" || knowledgeBaseID == "" {
|
||||
return ctx, weknora.InstanceIdentity{}, ErrNotConfigured
|
||||
}
|
||||
remote, err := client.newRemote(target)
|
||||
if err != nil {
|
||||
return ctx, weknora.InstanceIdentity{}, err
|
||||
}
|
||||
base, err := remote.GetKnowledgeBase(ctx, knowledgeBaseID)
|
||||
if err != nil {
|
||||
return ctx, weknora.InstanceIdentity{}, err
|
||||
}
|
||||
connection := &taskConnection{remote: remote, baseURL: weknora.NormalizeInstanceURL(target.BaseURL)}
|
||||
bound := context.WithValue(ctx, taskConnectionKey{}, connection)
|
||||
return bound, weknora.InstanceIdentity{BaseURL: connection.baseURL, TenantID: base.TenantID}, nil
|
||||
}
|
||||
|
||||
func (client *Client) with(ctx context.Context, call func(*weknora.Client) error) error {
|
||||
if connection := taskConnectionFrom(ctx); connection != nil && connection.remote != nil {
|
||||
return call(connection.remote)
|
||||
}
|
||||
target, err := client.Resolver.Resolve(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -31,16 +87,7 @@ func (client *Client) with(ctx context.Context, call func(*weknora.Client) error
|
||||
if target.BaseURL == "" || target.APIKey == "" {
|
||||
return ErrNotConfigured
|
||||
}
|
||||
timeout := client.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultBusinessTimeout
|
||||
}
|
||||
remote, err := weknora.NewClient(weknora.ClientConfig{
|
||||
BaseURL: target.BaseURL,
|
||||
APIKey: target.APIKey,
|
||||
Timeout: timeout,
|
||||
AllowDockerHTTP: client.AllowDockerHTTP,
|
||||
})
|
||||
remote, err := client.newRemote(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -48,34 +95,25 @@ func (client *Client) with(ctx context.Context, call func(*weknora.Client) error
|
||||
}
|
||||
|
||||
func (client *Client) TargetIdentity(ctx context.Context, knowledgeBaseID string) (weknora.InstanceIdentity, error) {
|
||||
target, err := client.Resolver.Resolve(ctx)
|
||||
if err != nil {
|
||||
return weknora.InstanceIdentity{}, err
|
||||
if connection := taskConnectionFrom(ctx); connection != nil && connection.remote != nil {
|
||||
if knowledgeBaseID == "" {
|
||||
return weknora.InstanceIdentity{}, ErrNotConfigured
|
||||
}
|
||||
base, err := connection.remote.GetKnowledgeBase(ctx, knowledgeBaseID)
|
||||
if err != nil {
|
||||
return weknora.InstanceIdentity{}, err
|
||||
}
|
||||
return weknora.InstanceIdentity{BaseURL: connection.baseURL, TenantID: base.TenantID}, nil
|
||||
}
|
||||
if target.BaseURL == "" || target.APIKey == "" || knowledgeBaseID == "" {
|
||||
return weknora.InstanceIdentity{}, ErrNotConfigured
|
||||
}
|
||||
timeout := client.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultBusinessTimeout
|
||||
}
|
||||
remote, err := weknora.NewClient(weknora.ClientConfig{
|
||||
BaseURL: target.BaseURL,
|
||||
APIKey: target.APIKey,
|
||||
Timeout: timeout,
|
||||
AllowDockerHTTP: client.AllowDockerHTTP,
|
||||
})
|
||||
if err != nil {
|
||||
return weknora.InstanceIdentity{}, err
|
||||
}
|
||||
base, err := remote.GetKnowledgeBase(ctx, knowledgeBaseID)
|
||||
if err != nil {
|
||||
return weknora.InstanceIdentity{}, err
|
||||
}
|
||||
return weknora.InstanceIdentity{
|
||||
BaseURL: weknora.NormalizeInstanceURL(target.BaseURL),
|
||||
TenantID: base.TenantID,
|
||||
}, nil
|
||||
return client.identityOnce(ctx, knowledgeBaseID)
|
||||
}
|
||||
|
||||
// identityOnce is the pre-task entry used by callers that only need identity.
|
||||
// Multi-call jobs use BeginTask so verification and every later call share the
|
||||
// same connection.
|
||||
func (client *Client) identityOnce(ctx context.Context, knowledgeBaseID string) (weknora.InstanceIdentity, error) {
|
||||
_, identity, err := client.BeginTask(ctx, knowledgeBaseID)
|
||||
return identity, err
|
||||
}
|
||||
|
||||
func (client *Client) UploadFile(ctx context.Context, request weknora.UploadRequest) (weknora.Knowledge, error) {
|
||||
|
||||
Reference in New Issue
Block a user