|
10Duke Scale C++ SDK
|
A checked-out license lease is valid for a limited time. To keep using the license, the application must heartbeat the lease before the lease expires. This page explains when a lease can be heartbeat, how the client behaves when a lease is not yet due, and what is the recommended pattern for heartbeating leases in an application.
See also client concepts, identity-based licensing and license key licensing.
Each lease (tenduke::se::licensing::Lease) carries two timestamps, both in epoch seconds, which together define the window in which the lease can be heartbeat:
In other words, a lease must be heartbeat after heartbeatNotBeforeEpochS but before expiresEpochS. A lease that was just checked out or just heartbeat is typically not yet due for heartbeat.
The heartbeat methods of tenduke::se::licensing::LicensingClient check every lease against heartbeatNotBeforeEpochS before sending anything to the server. How a lease that is not yet due is handled depends on the skipLeasesNotDue setting.
By default, leases that are not yet due are skipped:
Skipped leases are not errors: hasErrors() does not consider them. Use hasNotDue() to check if any leases were skipped.
A heartbeat response therefore has three parts, and the application should handle all of them:
| Field | Contents | Lease id | Lease cache |
|---|---|---|---|
| leases | Successfully heartbeat leases | New lease id (oldLeaseId holds the previous one) | Updated with the new leases |
| errors | Leases that the server failed to heartbeat | - | Depending on the error, the lease may be removed (see below) |
| notDue | Leases that were skipped, because they were not due | Unchanged | Unchanged |
If skipping is disabled, a lease that is not yet due causes tenduke::se::licensing::HeartbeatTooSoon to be thrown. The check is done before anything is sent to the server: A single lease that is not yet due cancels the entire batch, and none of the leases is heartbeat.
To use strict mode with the fluent API:
or, with parameters:
Strict mode is useful when the application wants to treat an early heartbeat as a programming error. For most applications, the default skipping behavior is more convenient.
The client compares heartbeatNotBeforeEpochS, which is set by the server, with the local clock of the device. To compensate for small clock differences between the device and the server, the client applies a tolerance: A lease is considered due if heartbeatNotBeforeEpochS - tolerance <= now. The default tolerance is 5 seconds (tenduke::se::licensing::DefaultLicensingClient::DEFAULT_HEARTBEAT_TOLERANCE_S).
The tolerance only helps with small differences. If the device clock is significantly off, the client may skip leases that the server would accept (clock behind), or send heartbeats that the server rejects (clock ahead). A heartbeat rejected by the server is reported in errors like any other heartbeat failure.
A successful heartbeat changes the lease id. Heartbeating or releasing a lease with an old lease id fails. The simplest way to always have current leases is to not keep leases in the application at all and read them from the lease cache (tenduke::se::licensing::Leases) when needed:
If the application keeps its own copy of the leases, it must combine leases and notDue from the heartbeat response. For example, to release everything after a heartbeat:
Releasing only heartbeatResponse.leases would leave the skipped leases checked out.
The lease cache can tell which leases are due for heartbeat right now:
The method returns the leases that are within their heartbeat window: The heartbeat window has been reached and the lease has not expired. This is useful, for example, for deciding whether there is anything to heartbeat, for showing the state of the leases in the user interface, or for picking the leases to heartbeat in strict mode.
The cache also tells when the next lease becomes due:
This is the earliest heartbeatNotBeforeEpochS of the leases that have not expired, and it is the natural point in time to schedule the next heartbeat. The value is in the past, if some lease is already due. If the cache has no leases that can be heartbeat, the method returns tenduke::se::licensing::Leases::NO_HEARTBEAT_DUE_EPOCH_S_MAGIC.
Note that the cache does not apply the clock-skew tolerance that the licensing client applies, so the client may accept a lease a few seconds before the cache reports it as due.
A lease that was just checked out is not due for heartbeat. With the default settings, heartbeating it just returns the lease in notDue, which is harmless but useless. Schedule the first heartbeat based on heartbeatNotBeforeEpochS of the checked-out leases instead (see the recommended patternthe recommended pattern below).
Some heartbeat errors mean that the lease can no longer be used, for example when the checkout has expired (licenseCheckoutExpired) or the server no longer knows the lease (leaseIdNotFound). The client removes such leases from the lease cache automatically. The application should react to these errors, typically by checking out the license again or by disabling the licensed functionality.
Networking failures are reported as exceptions (see errors in client concepts), and the client has no built-in retry logic. If a heartbeat fails because of a temporary network issue, the leases in the cache are unchanged, and the application can retry. The retry must happen before the leases expire.
Metered leases are heartbeat with the same methods, and the same rules apply. Note that the usage quantity given for a skipped lease is not reported to the server:
The lease ids change on every successful heartbeat. As recommended in client concepts, serialize the client state after heartbeat, so that the stored state has the current lease ids if the application terminates unexpectedly.
The recommended pattern is a single background task that heartbeats all leases in the cache and schedules itself based on the heartbeat window of the leases:
This pattern has several benefits:
Example:
In a real application, use a sleep that can be interrupted (for example, a condition variable with a timeout), so that the task can be woken up after a new checkout and stopped cleanly when the application exits. Stop the task before releasing the licenses on application exit.