Conversation
Declaring a shared memory zone currently means calling `ngx_shared_memory_add` by hand, installing an `unsafe extern "C"` initialization callback, and casting the untyped `ngx_slab_pool_t::data` pointer back to the payload type at every access. Nothing ties those casts together, so a module using more than one zone can read a zone as the wrong type without any diagnostic. `SharedZone<T>` keeps the payload type in the signature instead. `SharedZone::add` declares or joins a zone and installs a generic initialization callback, and `SharedZone::get` hands back a `&T` without a cast. Implementers provide `SharedZoneData::new_in` to build the initial value in the zone's slab pool, and may override `SharedZoneData::reuse` to validate a value left behind by a previous configuration, which the raw interface offers no obvious place for. Zone contents are recovered from the slab pool rather than from `ngx_shm_zone_t::data`, so a zone reused across a configuration reload and a zone inherited from the master process on binary upgrade take the same path. Zones declared `noreuse` are not covered: their mapping is replaced, and carrying data across that boundary needs the `data` argument this wrapper deliberately leaves alone. `examples/shared_dict` is ported over as the first user, dropping the ten `unsafe` blocks it needed for zone handling. Signed-off-by: Y.Horie <u5.horie@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed changes
Declaring a shared memory zone currently means calling
ngx_shared_memory_addby hand, installing anunsafe extern "C"initialization callback, and casting the untypedngx_slab_pool_t::datapointer back to the payload type at every access. Nothing ties those casts together, so a module using more than one zone can read a zone as the wrong type with no diagnostic.This adds
ngx::core::SharedZone<T>, which keeps the payload type in the signature:SharedZone::adddeclares or joins a zone and installs a generic initialization callback;SharedZone::gethands back a&Twithout a cast.SharedZoneData::reuseis an optional hook for validating a value left behind by a previous configuration, which the raw interface offers no obvious place for.Two design notes worth reviewing:
ngx_shm_zone_t::data, so a zone reused across a configuration reload and a zone inherited from the master process on binary upgrade take the same path.noreuseare not covered: their mapping is replaced, and carrying data across that boundary needs thedataargument this wrapper deliberately leaves alone. Happy to extend this if you would rather it be handled up front.examples/shared_dictis ported over as the first user, which drops the tenunsafeblocks it needed for zone handling (57 → 48unsafein the file overall).Marked as a draft: the API shape is the part I would like feedback on before polishing.
Testing
cargo test --lib,cargo clippy --all-targets,cargo fmt --checkclean, with and without--features vendored.examples/t/shared_dict.tagainst nginx 1.30.4 built from source: identical results before and after this change (3 pre-existing failures on macOS, unrelated to this PR — they concern updating an existing key inRbTreeMap, and reproduce onmain).Checklist