manager.go
Filename: manager.go
-
Overview The
manager.go
file defines theManager
struct and its associated methods, which are responsible for managing the application's license. This includes loading, validating, activating, and refreshing licenses, as well as checking and initializing features based on the active license. -
Detailed Documentation
LM
var LM *Manager
- Purpose: Global variable to hold a single instance of the
Manager
.
validationFrequency
var validationFrequency = 24 * 60 * time.Minute
- Purpose: Defines the frequency at which the license is validated.
Manager
type Manager struct {
repo *Repo
mutex sync.Mutex
validatorRunning bool
done chan struct{}
terminated chan struct{}
lastValidated int64
failedAttempts uint64
activeLicenseV3 *model.LicenseV3
activeFeatures basemodel.FeatureSet
}
- Purpose: Manages the license and its features.
repo
: Pointer to theRepo
struct for database interactions.mutex
: Mutex to protect concurrent access to the manager's state.validatorRunning
: Boolean indicating whether the validator is running.done
: Channel used to signal the validator to stop.terminated
: Channel used to signal that the validator has stopped.lastValidated
: Timestamp of the last license validation.failedAttempts
: Counter for consecutive failed validation attempts.activeLicenseV3
: Pointer to the activeLicenseV3
model.activeFeatures
: Set of active features.
StartManager
func StartManager(db *sqlx.DB, store sqlstore.SQLStore, features ...basemodel.Feature) (*Manager, error) {
}
- Purpose: Initializes and returns a new
Manager
instance. If an instance already exists, it returns the existing one.- Parameters:
db
:*sqlx.DB
- Database connection.store
:sqlstore.SQLStore
- SQL store interface.features
:...basemodel.Feature
- Variable number of features to be initialized.
- Returns:
*Manager
: A pointer to theManager
instance.error
: An error, if any.
- Parameters:
start
func (lm *Manager) start(features ...basemodel.Feature) error {
}
- Purpose: Loads the active license into memory and starts the validator.
- Parameters:
features
:...basemodel.Feature
- Variable number of features.
- Returns:
error
: An error, if any.
- Parameters:
Stop
func (lm *Manager) Stop() {
}
- Purpose: Stops the license validator.
- Returns: None
SetActiveV3
func (lm *Manager) SetActiveV3(l *model.LicenseV3, features ...basemodel.Feature) {
}
- Purpose: Sets the active license and features.
- Parameters:
l
:*model.LicenseV3
- License to activate.features
:...basemodel.Feature
- Variable number of features.
- Returns: None
- Parameters:
setDefaultFeatures
func setDefaultFeatures(lm *Manager) {
}
- Purpose: Appends default features to the active features.
- Parameters:
lm
:*Manager
- The license manager.
- Returns: None
- Parameters:
LoadActiveLicenseV3
func (lm *Manager) LoadActiveLicenseV3(features ...basemodel.Feature) error {
}
- Purpose: Loads the active license from the database and sets it in the manager. If no active license is found, it defaults to the basic plan.
- Parameters:
features
:...basemodel.Feature
- Variable number of features.
- Returns:
error
: An error, if any.
- Parameters:
GetLicensesV3
func (lm *Manager) GetLicensesV3(ctx context.Context) (response []*model.LicenseV3, apiError *model.ApiError) {
}
- Purpose: Retrieves all licenses from the database.
- Parameters:
ctx
:context.Context
- Context for the operation.
- Returns:
[]*model.LicenseV3
: A slice ofLicenseV3
pointers.*model.ApiError
: An API error, if any.
- Parameters:
ValidatorV3
func (lm *Manager) ValidatorV3(ctx context.Context) {
}
- Purpose: Validates the license periodically.
- Parameters:
ctx
:context.Context
- Context for the operation.
- Returns: None
- Parameters:
RefreshLicense
func (lm *Manager) RefreshLicense(ctx context.Context) *model.ApiError {
}
- Purpose: Refreshes the active license by validating it against an external service.
- Parameters:
ctx
:context.Context
- Context for the operation.
- Returns:
*model.ApiError
: An API error, if any.
- Parameters:
ValidateV3
func (lm *Manager) ValidateV3(ctx context.Context) (reterr error) {
}
- Purpose: Validates the active license and handles validation failures.
- Parameters:
ctx
:context.Context
- Context for the operation.
- Returns:
error
: An error, if any.
- Parameters:
ActivateV3
func (lm *Manager) ActivateV3(ctx context.Context, licenseKey string) (licenseResponse *model.LicenseV3, errResponse *model.ApiError) {
}
- Purpose: Activates a license using a license key.
- Parameters:
ctx
:context.Context
- Context for the operation.licenseKey
:string
- The license key to activate.
- Returns:
*model.LicenseV3
: The activated license.*model.ApiError
: An API error, if any.
- Parameters:
GetActiveLicense
func (lm *Manager) GetActiveLicense() *model.LicenseV3 {
}
- Purpose: Returns the active license.
- Returns:
*model.LicenseV3
: A pointer to the activeLicenseV3
.
- Returns:
CheckFeature
func (lm *Manager) CheckFeature(featureKey string) error {
}
- Purpose: Checks if a feature is active.
- Parameters:
featureKey
:string
- The key of the feature to check.
- Returns:
error
: An error if the feature is unavailable, nil otherwise.
- Parameters:
GetFeatureFlags
func (lm *Manager) GetFeatureFlags() (basemodel.FeatureSet, error) {
}
- Purpose: Returns the current active features.
- Returns:
basemodel.FeatureSet
: A set of active features.error
: An error, if any.
- Returns:
InitFeatures
func (lm *Manager) InitFeatures(features basemodel.FeatureSet) error {
}
- Purpose: Initializes features in the repository.
- Parameters:
features
:basemodel.FeatureSet
- The set of features to initialize.
- Returns:
error
: An error, if any.
- Parameters:
UpdateFeatureFlag
func (lm *Manager) UpdateFeatureFlag(feature basemodel.Feature) error {
}
- Purpose: Updates a feature flag in the repository.
- Parameters:
feature
:basemodel.Feature
- The feature to update.
- Returns:
error
: An error, if any.
- Parameters:
GetFeatureFlag
func (lm *Manager) GetFeatureFlag(key string) (basemodel.Feature, error) {
}
- Purpose: Retrieves a feature flag from the repository.
- Parameters:
key
:string
- The key of the feature to retrieve.
- Returns:
basemodel.Feature
: The retrieved feature.error
: An error, if any.
- Parameters:
GetRepo
func (lm *Manager) GetRepo() *Repo {
}
- Purpose: Returns the license repository.
- Returns:
*Repo
: A pointer to theRepo
.
- Returns:
- Code Examples None
Include in Getting Started: NO