refactor: 将 Pool 重构为接口,提取 pool 为具体实现

This commit is contained in:
2026-04-10 16:38:37 +08:00
parent cca4860df0
commit 308faa95ff
2 changed files with 27 additions and 15 deletions

View File

@@ -20,7 +20,7 @@ import (
//
// ctx 取消时会立即中断与 Python 的通信并返回 ctx.Err()。
// 对于流式输出/双向流ctx 取消会关闭返回的 channel。
func Invoke[R any](ctx context.Context, pool *Pool, method string, args ...any) (R, error) {
func Invoke[R any](ctx context.Context, pool Pool, method string, args ...any) (R, error) {
rt := reflect.TypeFor[R]()
// 查找 chan 类型的输入参数
@@ -89,7 +89,7 @@ func contextErr(ctx context.Context, err error) error {
return err
}
func invokeRegular[R any](ctx context.Context, pool *Pool, method string, args ...any) (R, error) {
func invokeRegular[R any](ctx context.Context, pool Pool, method string, args ...any) (R, error) {
var zero R
argsJSON, err := json.Marshal(args)
@@ -102,7 +102,7 @@ func invokeRegular[R any](ctx context.Context, pool *Pool, method string, args .
return zero, err
}
id := pool.reqID.Add(1)
id := pool.nextReqID()
stop := watchCtx(ctx, conn, id)
defer stop()
@@ -136,7 +136,7 @@ func invokeRegular[R any](ctx context.Context, pool *Pool, method string, args .
return result, nil
}
func invokeStreamOut[R any](ctx context.Context, pool *Pool, method string, rt reflect.Type, args ...any) (R, error) {
func invokeStreamOut[R any](ctx context.Context, pool Pool, method string, rt reflect.Type, args ...any) (R, error) {
var zero R
argsJSON, err := json.Marshal(args)
@@ -149,7 +149,7 @@ func invokeStreamOut[R any](ctx context.Context, pool *Pool, method string, rt r
return zero, err
}
id := pool.reqID.Add(1)
id := pool.nextReqID()
if err := writeMsg(conn, Message{
ID: id,
Type: TypeCall,
@@ -187,7 +187,7 @@ func invokeStreamOut[R any](ctx context.Context, pool *Pool, method string, rt r
return ch.Interface().(R), nil
}
func invokeStreamIn[R any](ctx context.Context, pool *Pool, method string, streamArgIdx int, streamCh reflect.Value, args ...any) (R, error) {
func invokeStreamIn[R any](ctx context.Context, pool Pool, method string, streamArgIdx int, streamCh reflect.Value, args ...any) (R, error) {
var zero R
jsonArgs := make([]any, len(args))
@@ -204,7 +204,7 @@ func invokeStreamIn[R any](ctx context.Context, pool *Pool, method string, strea
return zero, err
}
id := pool.reqID.Add(1)
id := pool.nextReqID()
stop := watchCtx(ctx, conn, id)
defer stop()
@@ -265,7 +265,7 @@ func invokeStreamIn[R any](ctx context.Context, pool *Pool, method string, strea
return result, nil
}
func invokeStreamBoth[R any](ctx context.Context, pool *Pool, method string, streamArgIdx int, streamCh reflect.Value, rt reflect.Type, args ...any) (R, error) {
func invokeStreamBoth[R any](ctx context.Context, pool Pool, method string, streamArgIdx int, streamCh reflect.Value, rt reflect.Type, args ...any) (R, error) {
var zero R
jsonArgs := make([]any, len(args))
@@ -282,7 +282,7 @@ func invokeStreamBoth[R any](ctx context.Context, pool *Pool, method string, str
return zero, err
}
id := pool.reqID.Add(1)
id := pool.nextReqID()
if err := writeMsg(conn, Message{
ID: id,
Type: TypeCall,