Add concurrency init to api (#3749)

expose middleware init via api layer
This commit is contained in:
Keepers 2023-07-03 19:19:21 -06:00 committed by GitHub
parent 7fbe03ae37
commit 70bc241831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/alcionai/corso/src/internal/m365/graph" "github.com/alcionai/corso/src/internal/m365/graph"
"github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/path"
) )
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -55,6 +56,14 @@ func NewClient(creds account.M365Config) (Client, error) {
return Client{creds, s, li, rqr}, nil return Client{creds, s, li, rqr}, nil
} }
// initConcurrencyLimit ensures that the graph concurrency limiter is
// initialized, so that calls do not step over graph api's service limits.
// Limits are derived from the provided servie type.
// Callers will need to call this func before making api calls an api client.
func InitConcurrencyLimit(ctx context.Context, pst path.ServiceType) {
graph.InitializeConcurrencyLimiter(ctx, pst == path.ExchangeService, 4)
}
// Service generates a new graph servicer. New servicers are used for paged // Service generates a new graph servicer. New servicers are used for paged
// and other long-running requests instead of the client's stable service, // and other long-running requests instead of the client's stable service,
// so that in-flight state within the adapter doesn't get clobbered. // so that in-flight state within the adapter doesn't get clobbered.

View File

@ -11,6 +11,7 @@ import (
"github.com/alcionai/corso/src/internal/m365/graph" "github.com/alcionai/corso/src/internal/m365/graph"
"github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/services/m365/api" "github.com/alcionai/corso/src/pkg/services/m365/api"
) )
@ -73,7 +74,7 @@ func UsersCompatNoInfo(ctx context.Context, acct account.Account) ([]*UserNoInfo
// UserHasMailbox returns true if the user has an exchange mailbox enabled // UserHasMailbox returns true if the user has an exchange mailbox enabled
// false otherwise, and a nil pointer and an error in case of error // false otherwise, and a nil pointer and an error in case of error
func UserHasMailbox(ctx context.Context, acct account.Account, userID string) (bool, error) { func UserHasMailbox(ctx context.Context, acct account.Account, userID string) (bool, error) {
ac, err := makeAC(acct) ac, err := makeAC(ctx, acct, path.ExchangeService)
if err != nil { if err != nil {
return false, clues.Stack(err).WithClues(ctx) return false, clues.Stack(err).WithClues(ctx)
} }
@ -93,7 +94,7 @@ func UserHasMailbox(ctx context.Context, acct account.Account, userID string) (b
// UserHasDrives returns true if the user has any drives // UserHasDrives returns true if the user has any drives
// false otherwise, and a nil pointer and an error in case of error // false otherwise, and a nil pointer and an error in case of error
func UserHasDrives(ctx context.Context, acct account.Account, userID string) (bool, error) { func UserHasDrives(ctx context.Context, acct account.Account, userID string) (bool, error) {
ac, err := makeAC(acct) ac, err := makeAC(ctx, acct, path.OneDriveService)
if err != nil { if err != nil {
return false, clues.Stack(err).WithClues(ctx) return false, clues.Stack(err).WithClues(ctx)
} }
@ -124,7 +125,7 @@ func checkUserHasDrives(ctx context.Context, dgdd getDefaultDriver, userID strin
// TODO: Remove this once we remove `Info` from `Users` and instead rely on the `GetUserInfo` API // TODO: Remove this once we remove `Info` from `Users` and instead rely on the `GetUserInfo` API
// to get user information // to get user information
func usersNoInfo(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*UserNoInfo, error) { func usersNoInfo(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*UserNoInfo, error) {
ac, err := makeAC(acct) ac, err := makeAC(ctx, acct, path.UnknownService)
if err != nil { if err != nil {
return nil, clues.Stack(err).WithClues(ctx) return nil, clues.Stack(err).WithClues(ctx)
} }
@ -156,7 +157,7 @@ func usersNoInfo(ctx context.Context, acct account.Account, errs *fault.Bus) ([]
// Users returns a list of users in the specified M365 tenant // Users returns a list of users in the specified M365 tenant
func Users(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*User, error) { func Users(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*User, error) {
ac, err := makeAC(acct) ac, err := makeAC(ctx, acct, path.ExchangeService)
if err != nil { if err != nil {
return nil, clues.Stack(err).WithClues(ctx) return nil, clues.Stack(err).WithClues(ctx)
} }
@ -209,7 +210,7 @@ func GetUserInfo(
acct account.Account, acct account.Account,
userID string, userID string,
) (*api.UserInfo, error) { ) (*api.UserInfo, error) {
ac, err := makeAC(acct) ac, err := makeAC(ctx, acct, path.ExchangeService)
if err != nil { if err != nil {
return nil, clues.Stack(err).WithClues(ctx) return nil, clues.Stack(err).WithClues(ctx)
} }
@ -243,7 +244,7 @@ type Site struct {
// Sites returns a list of Sites in a specified M365 tenant // Sites returns a list of Sites in a specified M365 tenant
func Sites(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*Site, error) { func Sites(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*Site, error) {
ac, err := makeAC(acct) ac, err := makeAC(ctx, acct, path.SharePointService)
if err != nil { if err != nil {
return nil, clues.Stack(err).WithClues(ctx) return nil, clues.Stack(err).WithClues(ctx)
} }
@ -315,7 +316,13 @@ func SitesMap(
// helpers // helpers
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
func makeAC(acct account.Account) (api.Client, error) { func makeAC(
ctx context.Context,
acct account.Account,
pst path.ServiceType,
) (api.Client, error) {
api.InitConcurrencyLimit(ctx, pst)
creds, err := acct.M365Config() creds, err := acct.M365Config()
if err != nil { if err != nil {
return api.Client{}, clues.Wrap(err, "getting m365 account creds") return api.Client{}, clues.Wrap(err, "getting m365 account creds")