-
Notifications
You must be signed in to change notification settings - Fork 19
WASI P3 docs #182
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
Draft
itowlson
wants to merge
1
commit into
spinframework:main
Choose a base branch
from
itowlson:wasi-p3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WASI P3 docs #182
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,9 @@ url = "https://github.com/spinframework/spin-docs/blob/main/content/v3/http-trig | |||||
| - [Exposing HTTP Triggers Using HTTPS](#exposing-http-triggers-using-https) | ||||||
| - [Trigger Options](#trigger-options) | ||||||
| - [Environment Variables](#environment-variables) | ||||||
| - [Controlling Instance Reuse](#controlling-instance-reuse) | ||||||
| - [Developer Guidelines for Instance Reuse](#developer-guidelines-for-instance-reuse) | ||||||
| - [Preventing Instance Reuse](#preventing-instance-reuse) | ||||||
|
|
||||||
| HTTP applications are an important workload in event-driven environments, | ||||||
| and Spin has built-in support for creating and running HTTP | ||||||
|
|
@@ -540,3 +543,31 @@ Once set, `spin up` will automatically use these explicitly set environment vari | |||||
| export SPIN_TLS_CERT=<path/to/cert> | ||||||
| export SPIN_TLS_KEY=<path/to/key> | ||||||
| ``` | ||||||
|
|
||||||
| ## Controlling Instance Reuse | ||||||
|
|
||||||
| Instance reuse is when Spin handles more than one HTTP request within a single component instance. Instance reuse improves performance and density, because Spin does not need to create a new instance for every request. Spin can reuse instances both consecutively and concurrently - that is, it can assign a new request to either a freshly created instance, an instance that has finished handling a request, or an instance that is _in the middle of_ handling another request. | ||||||
|
|
||||||
| The exact details depend on your component. | ||||||
|
|
||||||
| A WASI P2 component is _never_ subject to instance reuse. (This includes all triggers other than HTTP.) | ||||||
|
|
||||||
| A WASI P3 component is _always_ subject to instance reuse, unless it calls specific APIs to prevent concurrent use. You can control instance reuse behaviour using the following `spin up` flags: | ||||||
|
|
||||||
| * `--max-instance-reuse-count` sets the maximum number of times a single instance can be reused | ||||||
| * `--max-instance-concurrent-reuse-count` sets the maximum number of requests that can be running in a single instance at the same time | ||||||
| * `--idle-instance-timeout` controls how long Spin will allow a reusable instance to be sit idle before evicting it | ||||||
|
|
||||||
| All of these flags accept ranges. When you provide a range, Spin assigns each new instance a random value from that range. This is to help you test that your component works correctly both when fresh (e.g. you do not rely on long-running state) and when reused (e.g. you are not unintentionally leaking data from one request to another). | ||||||
|
|
||||||
| ### Developer Guidelines for Instance Reuse | ||||||
|
|
||||||
| When writing WASI P3 HTTP components, you can take advantage of reuse in your code, by placing static data in static or global variables, which will become part of the instance state. For example, if your component contains a routing table, you could cache the parsed table in a static variable - you would then parse the table only if the cache had not been initialised (i.e. in a fresh instance), avoiding the overhead of parsing on every request. | ||||||
|
|
||||||
| > Don't rely on techniques like this for expensive operations. Spin doesn't guarantee the degree of instance reuse, and reuse may vary across differnt Spin hosts. | ||||||
|
|
||||||
| Conversely, take care that request data is not stored in static or global variables. If you're used to the WASI P2 model, you may have an implicit expectation that each request finds your component in an entirely fresh state. In WASI P3, that's no longer the case. You should store data that's private to a request in local variables; if you must store it in a static variable, make sure to isolate it (for example storing it in a map under a request-specific key). | ||||||
|
|
||||||
| ### Preventing Instance Reuse | ||||||
|
|
||||||
| Although you can control instance reuse on the `spin up` command line, this isn't available in other hosts. If the structure of your code means that it's not safe to reuse instances, then you can use Wasm Component Model backpressure functions in your code to tell the host not to schedule further requests to the current instance. How these are surfaced will depend on your language - for example, in Rust you would use `wit_bindgen::backpressure_inc` to suspend re-use and a balancing `wit_bindgen::backpressure_dec` to re-enable it. See the [Component Model documentation](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md#backpressure) for detailed information. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could mention here that future versions of Spin might support optionally enabling consecutive reuse for P2 components.