SectorLock
in package
This class is responsible for ensuring that page processing occurs sequentially as requests come in, avoiding race conditions by using a blocking queue.
The lock creates exclusive access to a specified sector, since that is the primary way (caveat: but not the only way) in which simultaneous processing would create inconsistencies.
There are more elegant solutions to this problem (such a row-level locking within the database). There are also some deficiencies in this implementation, in particular the inability for a single player to lock two sectors at once (to avoid, e.g., race conditions with movement).
Table of Contents
Constants
- LOCK_DURATION = 10
- The max time (in seconds) for a lock to be active before being considered stale.
- RETRY_DURATION = 5
- The max time (in seconds) to retry acquiring locks before giving up.
Properties
- $accountID : int|null
- $failed : bool
- $gameID : int|null
- $lockID : int|null
- $sectorID : int|null
Methods
- acquire() : bool
- Acquire an exclusive lock on a sector.
- acquireForPlayer() : bool
- Convenience wrapper to acquire a lock for the player in their current sector.
- getInstance() : self
- Returns the instance of this class from the DI container.
- getSectorID() : int
- hasFailed() : bool
- isActive() : bool
- release() : bool
- resetInstance() : void
- Reset the instance of this class associated with this page request (i.e. stored in the DI container).
- setFailed() : void
Constants
LOCK_DURATION
The max time (in seconds) for a lock to be active before being considered stale.
private
mixed
LOCK_DURATION
= 10
RETRY_DURATION
The max time (in seconds) to retry acquiring locks before giving up.
private
mixed
RETRY_DURATION
= 5
Should be less than LOCK_DURATION.
Properties
$accountID
private
int|null
$accountID
= null
$failed
private
bool
$failed
= false
$gameID
private
int|null
$gameID
= null
$lockID
private
int|null
$lockID
= null
$sectorID
private
int|null
$sectorID
= null
Methods
acquire()
Acquire an exclusive lock on a sector.
public
acquire(int $gameID, int $accountID, int $sectorID) : bool
Parameters
- $gameID : int
- $accountID : int
- $sectorID : int
Return values
bool —True if a new lock is acquired or false if existing lock used.
acquireForPlayer()
Convenience wrapper to acquire a lock for the player in their current sector.
public
acquireForPlayer(AbstractPlayer $player) : bool
Parameters
- $player : AbstractPlayer
Return values
boolgetInstance()
Returns the instance of this class from the DI container.
public
static getInstance() : self
The first time this is called, it will populate the DI container.
Return values
selfgetSectorID()
public
getSectorID() : int
Return values
inthasFailed()
public
hasFailed() : bool
Return values
boolisActive()
public
isActive() : bool
Tags
Return values
boolrelease()
public
release() : bool
Return values
bool —Whether there was a lock to release or not
resetInstance()
Reset the instance of this class associated with this page request (i.e. stored in the DI container).
public
static resetInstance() : void
NOTE: This should never be called by normal page requests, and should only be used by the CLI programs that run continuously.
setFailed()
private
setFailed() : void