@NotThreadSafe public interface

Ticket

implements AutoCloseable
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 close their ticket, allowing another request to acquire it. The preferred approach for this is to use a try-with-resources block:


     try (Ticket ticket = throttleService.acquireTicket("scm-command")) {
         //Do work, knowing the ticket will always be closed
     }

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
void close()
Releases the ticket, indicating the named resource is now available to service another request.
@Nonnull String getResourceName()
Retrieves the name of the resource to which this ticket grants access.
@Deprecated void release()
This method is deprecated. in 3.0 for removal in 4.0. To better support Java 7, Ticket is now AutoCloseable. Callers should use close() or a try-with-resources block instead.
[Expand]
Inherited Methods
From interface java.lang.AutoCloseable

Public Methods

public void close ()

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.

@Nonnull public String getResourceName ()

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

Returns
  • the resource name

@Deprecated public void release ()

This method is deprecated.
in 3.0 for removal in 4.0. To better support Java 7, Ticket is now AutoCloseable. Callers should use close() or a try-with-resources block instead.

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.