Skip to content

Add ID resolver for AWS lambda #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Add ID resolver for AWS lambda #10

wants to merge 12 commits into from

Conversation

inxilpro
Copy link
Contributor

Snowflakes/etc rely on unique datacenter and worker IDs to operate. Because AWS lambdas are different machines each time, it's impossible to assign each a unique worker/device ID.

This PR adds a new ID resolution mechanism that uses cache locks to acquire a unique ID when required, and then release that ID when the process is terminated. This means that you can effectively have 1024 processes generating unique snowflake IDs in parallel.

By default, Bits will use this mechanism if the AWS_LAMBDA_FUNCTION_NAME environmental variable is set.


public function __destruct()
{
$this->release();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do an App::terminating callback in the constructor rather than a destruct?

Or we might end up releasing before verbs commits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the advantage of App::terminating over __destruct? I think destruct is a little more flexible, but I could be wrong…

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If destruct happens after laravel tears down, could the cache be disconnected?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to add some extra fun... If you're running octane on lambda, then you're likely to run into problems relying on __destruct(), but you're also likely to run into problems with the singletons below.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution for Octane is to use a scoped singleton. Here's the relevant section of the docs:

Binding Scoped Singletons
The scoped method binds a class or interface into the container that should only be resolved one time within a given Laravel request / job lifecycle. While this method is similar to the singleton method, instances registered using the scoped method will be flushed whenever the Laravel application starts a new "lifecycle", such as when a Laravel Octane worker processes a new request or when a Laravel queue worker processes a new job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants