Documentation ¶
Overview ¶
Package promise provides functionalities for managing asynchronous responses.
Package promise handles functionalities to create, find, and manage asynchronous promises.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Promise ¶
type Promise struct { Id string // Id is a unique identifier for the promise. Length int // Length is the number of responses required to resolve the promise. Closed bool // Closed indicates whether the promise has been resolved. // contains filtered or unexported fields }
Promise struct represents an asynchronous operation that may complete at some point. It manages the lifecycle of an asynchronous request, collecting responses until it is resolved.
func Create ¶
Create creates a new promise with a callback function. It stores the promise in the repository and starts a timer to remove the promise after a timeout.
Parameters: - callback: func(...*generated.Response) Function to call when the promise is resolved.
Returns: - *Promise: A pointer to the newly created Promise. - error: An error if the Promise creation fails.
func Find ¶
Find locates a promise in the repository by its ID. It returns the promise if found, or an error if the promise ID is empty or not found.
Parameters: - promiseId: string The ID of the promise to locate.
Returns: - *Promise: The promise associated with the given ID, if found. - error: An error if the promise ID is empty or the promise is not found.
func (*Promise) Add ¶
Add adds a request to the promise and increments the number of responses required. It associates the promise ID with the request, indicating that the request is part of the promise.
Parameters: - req: *generated.Request The request to be added to the promise.
func (*Promise) Close ¶
func (p *Promise) Close()
Close marks the promise as closed and executes the callback if all responses are received. It also removes the promise from the repository of active promises.
func (*Promise) Resolve ¶
Resolve accumulates responses and resolves the promise if enough responses have been received. It adds the response to the promise and checks if the promise has received all expected responses. If all responses are received, it triggers the callback function and closes the promise.
Parameters: - res: *generated.Response The response to be added to the promise.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository manages a set of promises. It provides concurrent-safe operations to store and retrieve promises by their ID.