chyezh fda720b880
enhance: streaming service grpc utilities (#34436)
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>
2024-07-15 20:49:38 +08:00

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
}