Skip to content

feat: add stream module api#286

Open
plusls wants to merge 2 commits into
nginx:mainfrom
plusls:main
Open

feat: add stream module api#286
plusls wants to merge 2 commits into
nginx:mainfrom
plusls:main

Conversation

@plusls

@plusls plusls commented May 7, 2026

Copy link
Copy Markdown

Proposed changes

Add stream module api support

impl #108

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have written my commit messages in the Conventional Commits format.
  • I have read the CONTRIBUTING doc
  • I have added tests (when possible) that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

plusls added 2 commits May 7, 2026 10:51
Stream API was compiled whenever NGINX reported stream support, but
nginx-sys only generated stream FFI when its own stream feature was
enabled. Add a stream feature to the root crate that propagates to
nginx-sys/stream and gate the module with both conditions.

@avahahn avahahn left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks good to me mostly, just a few requests.

Comment thread src/stream/conf.rs
/// Stream phases in which a module can register handlers.
#[repr(usize)]
pub enum StreamPhase {
/// Post-accept phase

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

these comments are not needed.

Comment thread src/stream/conf.rs
);
return Err(AllocError);
}
// set an H::PHASE phase handler

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

S::PHASE? also comment unneeded.

Comment thread src/stream/conf.rs
}
}

pub use upstream::NgxStreamUpstreamModule;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is it possible to bundle the use statements up at the top?

Comment thread src/stream/session.rs
///
/// The caller has provided a valid non-null pointer to a valid `ngx_stream_session_t`
/// which shares the same representation as `Request`.
pub unsafe fn from_ngx_stream_session<'a>(r: *mut ngx_stream_session_t) -> &'a mut Session {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You already added this as a From impl so I think this member function is redundant.

Comment thread src/stream/session.rs
///
/// The option wraps an ngx_stream_upstream_t instance, it will be none when the underlying NGINX
/// request does not have a pointer to a [`ngx_stream_upstream_t`] upstream structure.
pub fn upstream(&self) -> Option<*mut ngx_stream_upstream_t> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please return a mut ref instead of a pointer here. This can be implemented as follows:

    pub fn upstream(&self) -> Option<&mut ngx_stream_upstream_t> {
        self.0.upstream.as_mut()
    }

This will allow rust callers to use the ref without having to play with as much unsafe code.

Comment thread src/stream/session.rs
/// Pointer to a [`ngx_connection_t`] client connection object.
///
/// [`ngx_connection_t`]: https://nginx.org/en/docs/dev/development_guide.html#connection
pub fn connection(&self) -> *mut ngx_connection_t {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Again, I would prefer this interface return a mut ref instead. Feel free to .expect(...) it so that you dont need to return an option either.

Comment thread src/stream/session.rs
/// Pointer to a [`ngx_log_t`].
///
/// [`ngx_log_t`]: https://nginx.org/en/docs/dev/development_guide.html#logging
pub fn log(&self) -> *mut ngx_log_t {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as previous comments about returning a ref.

@plusls

plusls commented Jun 19, 2026

Copy link
Copy Markdown
Author

These methods follow the http module's Request, and I kept the API deliberately consistent with it. The corresponding http methods also return raw pointers, and the http module likewise keeps both the from_ngx_http_request method and the From impl:

// src/http/request.rs
pub unsafe fn from_ngx_http_request<'a>(r: *mut ngx_http_request_t) -> &'a mut Request
pub fn upstream(&self) -> Option<*mut ngx_http_upstream_t>
pub fn connection(&self) -> *mut ngx_connection_t
pub fn log(&self) -> *mut ngx_log_t

Returning &mut references would be more idiomatic Rust, but it would diverge from the existing http module style. I'd like to defer to the maintainer here: if we decide to switch to references, I'd suggest doing the same in the http module for consistency. I just followed the original style.

@avahahn

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.

2 participants