@NotThreadSafe public interface

Ticket

com.atlassian.stash.throttle.Ticket

Class Overview

A ticket to access a named resource. When the resource is no longer needed, the ticket holder should release their ticket, allowing another request to acquire it.

Note: The maximum duration for a ticket is scoped to the surrounding operation (where "operation" may be an HTTP request, an executed task or some other arbitrary unit of work). The system forcibly releases any tickets which are still held at the end of that bounding operation, where possible. This is done to help prevent resource starvation due to "lost" tickets, which are never otherwise released by their holding code.

Warning: Tickets are not thread-safe, and should never be passed across threads. Acquiring a ticket on one thread and releasing it on another thread will fail. When queueing tasks in the ExecutorService exposed for plugins by the system, ticket state from the calling thread is automatically propagated to the executor thread when the task is run. As a result, queueing code does not need to pass the ticket reference it acquired to the new thread; instead, it should release the ticket. For threads managed any other way--for example, plugins which spin up their own ExecutorService--tickets must be reacquired on the new thread; they cannot be passed in.

See Also

Summary

Public Methods
@Nonnull String getResourceName()
Retrieves the name of the resource to which this ticket grants access.
void release()
Releases the ticket, indicating the named resource is now available to service another request.

Public Methods

@Nonnull public String getResourceName ()

Retrieves the name of the resource to which this ticket grants access.

Returns
  • the resource name

public void release ()

Releases the ticket, indicating the named resource is now available to service another request.

The system performs best effort detection of "lost" tickets and attempts to release them. However, it is better for ticketholders to release their own tickets after the guarded operation has been performed. This both reduces the duration during which the ticket is locked, allowing other code to acquire the ticket more readily, and helps ensure all tickets are eventually released.