public class UserLimitedBlockingPolicy extends LimitedBlockingPolicy
BlockingPolicy
extends the LimitedBlockingPolicy
.
It proposes to limit the blocking duration, but it also limits the
number of blocked threads for a given job and user.
The limit on the number of threads is valid ONLY for a given job AND a given user. For example, let's assume there is a limit of N blocking requests per job and user. The user U1 can start maximum N blocking requests to access the job J1 but not more. During this time he can also start up to N blocking access requests to any other job. And since this limit is valid only per user, another user U2 can also start up to N blocking requests on the job J1 without being affected by the fact that the limit is reached by the user U1 on this same job.
Note: If no user is identified, the IP address will be used instead.
In a such case, 2 strategies are proposed:
block(Thread, long, UWSJob, JobOwner, HttpServletRequest)
will
return 0)
The strategy to use MUST be specified at creation using
UserLimitedBlockingPolicy(long, int, boolean)
with a third
parameter set to true
to unblock the oldest thread if needed,
or false
to prevent blocking if the limit is reached.
Modifier and Type | Field and Description |
---|---|
protected java.util.Map<java.lang.String,java.util.concurrent.BlockingQueue<java.lang.Thread>> |
blockedThreads
List of all blocked threads.
|
static int |
DEFAULT_NB_MAX_BLOCKED
Default number of allowed blocked threads.
|
protected int |
maxBlockedThreadsByUser
The maximum number of blocked threads for a given job and user.
|
protected boolean |
unblockOld
Indicate what should happen when the maximum number of threads for a
given job and user is reached.
|
DEFAULT_TIMEOUT, timeout
Constructor and Description |
---|
UserLimitedBlockingPolicy()
Build a default
UserLimitedBlockingPolicy . |
UserLimitedBlockingPolicy(long timeout)
Build a
UserLimitedBlockingPolicy which will limit the blocking
duration to the given value and will limit the number of blocked threads
per job and user to the default value (i.e. |
UserLimitedBlockingPolicy(long timeout,
int maxNbBlocked)
Build a
UserLimitedBlockingPolicy which will limit the blocking
duration to the given value and will limit the number of blocked threads
per job and user to the given value. |
UserLimitedBlockingPolicy(long timeout,
int maxNbBlocked,
boolean unblockOld)
Build a
UserLimitedBlockingPolicy which will limit the blocking
duration to the given value and will limit the number of blocked threads
per job and user to the given value. |
Modifier and Type | Method and Description |
---|---|
long |
block(java.lang.Thread thread,
long userDuration,
UWSJob job,
JobOwner user,
javax.servlet.http.HttpServletRequest request)
Notify this
BlockingPolicy that the given thread is going to
be blocked for the specified duration. |
protected java.lang.String |
buildKey(UWSJob job,
JobOwner user,
javax.servlet.http.HttpServletRequest request)
Build the key for the map
blockedThreads . |
void |
unblocked(java.lang.Thread unblockedThread,
UWSJob job,
JobOwner user,
javax.servlet.http.HttpServletRequest request)
Notify this
BlockingPolicy that the given thread is not blocked
anymore. |
public static final int DEFAULT_NB_MAX_BLOCKED
protected final int maxBlockedThreadsByUser
protected final java.util.Map<java.lang.String,java.util.concurrent.BlockingQueue<java.lang.Thread>> blockedThreads
Keys are an ID identifying a given job AND a given user
(basically: jobId+";"+userId
; see
buildKey(UWSJob, JobOwner, HttpServletRequest)
for more
details).
Values are fixed-length queues of blocked threads.
protected final boolean unblockOld
true
to unblock the oldest blocked thread in order to allow
the new blocking.
false
to forbid new blocking.
public UserLimitedBlockingPolicy()
UserLimitedBlockingPolicy
.
This instance will limit the number of blocked threads per user and job
to the default value (i.e. 3) and will
limit the blocking duration to the default timeout
(see LimitedBlockingPolicy.DEFAULT_TIMEOUT
).
When the limit of threads is reached, the oldest thread is unblocked in order to allow the new incoming blocking.
public UserLimitedBlockingPolicy(long timeout)
UserLimitedBlockingPolicy
which will limit the blocking
duration to the given value and will limit the number of blocked threads
per job and user to the default value (i.e.
3).
When the limit of threads is reached, the oldest thread is unblocked in order to allow the new incoming blocking.
timeout
- Maximum blocking duration (in seconds).
If < 0, the default timeout (see
LimitedBlockingPolicy.DEFAULT_TIMEOUT
) will be
set.LimitedBlockingPolicy.LimitedBlockingPolicy(long)
public UserLimitedBlockingPolicy(long timeout, int maxNbBlocked)
UserLimitedBlockingPolicy
which will limit the blocking
duration to the given value and will limit the number of blocked threads
per job and user to the given value.
When the limit of threads is reached, the oldest thread is unblocked in order to allow the new incoming blocking.
timeout
- Maximum blocking duration (in seconds).
If < 0, the default timeout (see
LimitedBlockingPolicy.DEFAULT_TIMEOUT
) will
be set.maxNbBlocked
- Maximum number of blocked threads allowed for a
given job and a given user.
If ≤ 0, this parameter will be ignored and the
default value (i.e. 3)
will be set instead.public UserLimitedBlockingPolicy(long timeout, int maxNbBlocked, boolean unblockOld)
UserLimitedBlockingPolicy
which will limit the blocking
duration to the given value and will limit the number of blocked threads
per job and user to the given value.
When the limit of threads is reached, the oldest thread is unblocked if
the 3rd parameter is true
, or new incoming blocking will
be forbidden if this parameter is false
.
timeout
- Maximum blocking duration (in seconds).
If < 0, the default timeout (see
LimitedBlockingPolicy.DEFAULT_TIMEOUT
) will
be set.maxNbBlocked
- Maximum number of blocked threads allowed for a
given job and a given user.
If ≤ 0, this parameter will be ignored and the
default value (i.e. 3)
will be set instead.unblockOld
- Set the behavior to adopt when the maximum number of
threads is reached for a given job and user.
true
to unblock the oldest thread in
order to allow the new incoming blocking,
false
to forbid the new incoming
blocking.protected final java.lang.String buildKey(UWSJob job, JobOwner user, javax.servlet.http.HttpServletRequest request)
blockedThreads
.
The built key is: jobId + ";" + userId
.
Note: If no user is logged in or if the user is not specified here or if it does not have any ID, the IP address of the HTTP client will be used instead.
job
- Job associated with the request to block.
MUST NOT be NULL.user
- User who asked the blocking behavior.
If NULL (or it has a NULL ID), the IP address of the
HTTP client will be used.request
- HTTP request which should be blocked.
SHOULD NOT be NULL.public long block(java.lang.Thread thread, long userDuration, UWSJob job, JobOwner user, javax.servlet.http.HttpServletRequest request)
BlockingPolicy
BlockingPolicy
that the given thread is going to
be blocked for the specified duration. This function then decides how
long the given thread must really wait before resuming.
The parameter "userDuration" and the returned value are durations expressed in seconds. Both follow the same rules:
Since a timeout or another special behavior may be chosen by this
BlockingPolicy
, the returned value may be different from the
user's asked duration. The value that should be taken into account is
obviously the returned one.
IMPORTANT:
This function may UNblock an older request/thread, in function of
the strategy chosen/implemented by this BlockingPolicy
.
block
in interface BlockingPolicy
block
in class LimitedBlockingPolicy
thread
- Thread that is going to be blocked.
MUST NOT be NULL. If NULL this function will
either do nothing and return 0 (no blocking)
or throw a NullPointerException
.userDuration
- Waiting duration (in seconds) asked by the user.
< 0 means indefinite, 0 means no wait and
> 0 means waiting for the specified amount of
seconds.job
- The job associated with the thread.
Should not be NULL.user
- The user who asked for the blocking behavior.
If NULL, the request will be concerned as
anonymous and a decision to identify the user
(e.g. use the IP address) may be chosen by the
BlockingPolicy
implementation if
required.request
- The request which is going to be blocked.
Should not be NULL.public void unblocked(java.lang.Thread unblockedThread, UWSJob job, JobOwner user, javax.servlet.http.HttpServletRequest request)
BlockingPolicy
BlockingPolicy
that the given thread is not blocked
anymore.unblocked
in interface BlockingPolicy
unblocked
in class LimitedBlockingPolicy
unblockedThread
- Thread that is not blocked any more.
MUST be NOT NULL.job
- The job associated with the unblocked Thread.
Should not be NULL.user
- The user who originally asked for the blocking
behavior.
If NULL, the request will be concerned as
anonymous and a decision to identify the user
(e.g. use the IP address) may be chosen by the
BlockingPolicy
implementation if
required.request
- The request which has been unblocked.
Should not be NULL.