mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-07 01:28:27 +08:00
issue: #33285 - add two grpc resolver (by session and by streaming coord assignment service) - add one grpc balancer (by serverID and roundrobin) - add lazy conn to avoid block by first service discovery - add some utility function for streaming service Signed-off-by: chyezh <chyezh@outlook.com>
38 lines
783 B
Go
38 lines
783 B
Go
package lazygrpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// WithServiceCreator creates a lazy grpc service with a service creator.
|
|
func WithServiceCreator[T any](conn Conn, serviceCreator func(*grpc.ClientConn) T) Service[T] {
|
|
return &serviceImpl[T]{
|
|
Conn: conn,
|
|
serviceCreator: serviceCreator,
|
|
}
|
|
}
|
|
|
|
// Service is a lazy grpc service.
|
|
type Service[T any] interface {
|
|
Conn
|
|
|
|
GetService(ctx context.Context) (T, error)
|
|
}
|
|
|
|
// serviceImpl is a lazy grpc service implementation.
|
|
type serviceImpl[T any] struct {
|
|
Conn
|
|
serviceCreator func(*grpc.ClientConn) T
|
|
}
|
|
|
|
func (s *serviceImpl[T]) GetService(ctx context.Context) (T, error) {
|
|
conn, err := s.Conn.GetConn(ctx)
|
|
if err != nil {
|
|
var result T
|
|
return result, err
|
|
}
|
|
return s.serviceCreator(conn), nil
|
|
}
|