Use lazy initialization for the default grpc client

This avoids using a public global variable. It allows us to initialize
the default client a bit later (outside of init()). That way we can e.g.
properly initialize the in-memory registry.
This commit is contained in:
Ralf Haferkamp
2022-09-21 17:40:28 +02:00
committed by Ralf Haferkamp
parent 0ae75f8e66
commit 01650a5023
7 changed files with 28 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ package grpc
import (
"strings"
"sync"
"time"
mgrpcc "github.com/go-micro/plugins/v4/client/grpc"
@@ -15,16 +16,25 @@ import (
)
// DefaultClient is a custom oCIS grpc configured client.
var DefaultClient = getDefaultGrpcClient()
var (
defaultClient client.Client
once sync.Once
)
func DefaultClient() client.Client {
return getDefaultGrpcClient()
}
func getDefaultGrpcClient() client.Client {
once.Do(func() {
reg := registry.GetRegistry()
reg := registry.GetRegistry()
return mgrpcc.NewClient(
client.Registry(reg),
client.Wrap(mbreaker.NewClientWrapper()),
)
defaultClient = mgrpcc.NewClient(
client.Registry(reg),
client.Wrap(mbreaker.NewClientWrapper()),
)
})
return defaultClient
}
// Service simply wraps the go-micro grpc service.
@@ -40,7 +50,7 @@ func NewService(opts ...Option) Service {
// first add a server because it will reset any options
micro.Server(mgrpcs.NewServer()),
// also add a client that can be used after initializing the service
micro.Client(DefaultClient),
micro.Client(DefaultClient()),
micro.Address(sopts.Address),
micro.Name(strings.Join([]string{sopts.Namespace, sopts.Name}, ".")),
micro.Version(sopts.Version),

View File

@@ -168,7 +168,7 @@ func (g Graph) GetSingleDrive(w http.ResponseWriter, r *http.Request) {
}
func canCreateSpace(ctx context.Context, ownPersonalHome bool) bool {
s := settingssvc.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient)
s := settingssvc.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient())
pr, err := s.GetPermissionByID(ctx, &settingssvc.GetPermissionByIDRequest{
PermissionId: settingsServiceExt.CreateSpacePermissionID,
@@ -420,7 +420,7 @@ func (g Graph) ListStorageSpacesWithFilters(ctx context.Context, filters []*stor
client := g.GetGatewayClient()
permissions := make(map[string]struct{}, 1)
s := settingssvc.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient)
s := settingssvc.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient())
_, err := s.GetPermissionByID(ctx, &settingssvc.GetPermissionByIDRequest{
PermissionId: settingsServiceExt.ListAllSpacesPermissionID,
@@ -700,7 +700,7 @@ func getQuota(quota *libregraph.Quota, defaultQuota string) *storageprovider.Quo
}
func canSetSpaceQuota(ctx context.Context, user *userv1beta1.User) (bool, error) {
settingsService := settingssvc.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient)
settingsService := settingssvc.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient())
_, err := settingsService.GetPermissionByID(ctx, &settingssvc.GetPermissionByIDRequest{PermissionId: settingsServiceExt.SetSpaceQuotaPermissionID})
if err != nil {
merror := merrors.FromError(err)

View File

@@ -137,7 +137,7 @@ func NewService(opts ...Option) Service {
}
if options.RoleService == nil {
svc.roleService = settingssvc.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient)
svc.roleService = settingssvc.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient())
} else {
svc.roleService = options.RoleService
}

View File

@@ -38,7 +38,7 @@ func NewService(opts ...Option) Service {
roleService := options.RoleService
if roleService == nil {
roleService = settingssvc.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient)
roleService = settingssvc.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient())
}
roleManager := options.RoleManager
if roleManager == nil {

View File

@@ -127,7 +127,7 @@ func Server(cfg *config.Config) *cli.Command {
}
func loadMiddlewares(ctx context.Context, logger log.Logger, cfg *config.Config) alice.Chain {
rolesClient := settingssvc.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient)
rolesClient := settingssvc.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient())
revaClient, err := cs3.GetGatewayServiceClient(cfg.Reva.Address)
var userProvider backend.UserBackend
switch cfg.AccountBackend {
@@ -145,7 +145,7 @@ func loadMiddlewares(ctx context.Context, logger log.Logger, cfg *config.Config)
logger.Fatal().Msgf("Invalid accounts backend type '%s'", cfg.AccountBackend)
}
storeClient := storesvc.NewStoreService("com.owncloud.api.store", grpc.DefaultClient)
storeClient := storesvc.NewStoreService("com.owncloud.api.store", grpc.DefaultClient())
if err != nil {
logger.Error().Err(err).
Str("gateway", cfg.Reva.Address).

View File

@@ -37,7 +37,7 @@ func Index(cfg *config.Config) *cli.Command {
return parser.ParseConfig(cfg)
},
Action: func(c *cli.Context) error {
client := searchsvc.NewSearchProviderService("com.owncloud.api.search", grpc.DefaultClient)
client := searchsvc.NewSearchProviderService("com.owncloud.api.search", grpc.DefaultClient())
_, err := client.IndexSpace(context.Background(), &searchsvc.IndexSpaceRequest{
SpaceId: c.String("space"),
UserId: c.String("user"),

View File

@@ -69,8 +69,8 @@ func NewService(opts ...Option) (Service, error) {
config: conf,
log: options.Logger,
mux: m,
searchClient: searchsvc.NewSearchProviderService("com.owncloud.api.search", grpc.DefaultClient),
thumbnailsClient: thumbnailssvc.NewThumbnailService("com.owncloud.api.thumbnails", grpc.DefaultClient),
searchClient: searchsvc.NewSearchProviderService("com.owncloud.api.search", grpc.DefaultClient()),
thumbnailsClient: thumbnailssvc.NewThumbnailService("com.owncloud.api.thumbnails", grpc.DefaultClient()),
revaClient: gwc,
}