Skip to content
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

Patch configuration and log actual port on startup #3160

Merged
merged 12 commits into from Apr 26, 2024

Conversation

bisgardo
Copy link
Contributor

@bisgardo bisgardo commented Mar 13, 2024

These changes are now available in 4.94.1

Before this change, the application

let app = Application(.testing)
defer { app.shutdown() }
try app.server.start(hostname: nil, port: 0)
defer { app.server.shutdown() }

would log the following message before starting the server:

[Vapor] Server starting on http://127.0.0.1:0

After this change it instead logs a message like the following after starting the server:

[Vapor] Server starting on http://127.0.0.1:57935

The input configuration is also patched such that app.http.server.configuration.port will hold the actual port after startup. Currently if it has value 0 it will keep that value (only app.http.server.shared.localAddress?.port will have the correct one).

Fixes #3159.

Before this change, the application

```swift
let app = Application(.testing)
defer { app.shutdown() }
try app.server.start(hostname: nil, port: 0)
defer { app.server.shutdown() }
```

would log the following message *before* starting the server:

```
[Vapor] Server starting on http://127.0.0.1:0
```

After this change it instead logs a message like the following *after* starting the server:

```
[Vapor] Server started on [IPv4]127.0.0.1/127.0.0.1:57935
```
@bisgardo
Copy link
Contributor Author

I have no experience with this code base so I'm just guessing that it's safe to force unwrap self.localAddress after the server has successfully started.

I also realize that the log message is now less readable and doesn't include the protocol. This was just the simplest possible solution I could come up with. I'll be happy to change it.

Looking forward to your feedback.

Sources/Vapor/HTTP/Server/HTTPServer.swift Outdated Show resolved Hide resolved
@bisgardo bisgardo requested a review from 0xTim March 15, 2024 15:39
Sources/Vapor/HTTP/Server/HTTPServer.swift Outdated Show resolved Hide resolved
Sources/Vapor/HTTP/Server/HTTPServer.swift Show resolved Hide resolved
Sources/Vapor/HTTP/Server/HTTPServer.swift Show resolved Hide resolved
Sources/Vapor/HTTP/Server/HTTPServer.swift Outdated Show resolved Hide resolved
@bisgardo bisgardo requested a review from 0xTim March 24, 2024 21:55
Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

Thanks! Just a couple of nits left

Sources/Vapor/HTTP/Server/HTTPServer.swift Outdated Show resolved Hide resolved
Just invoking the property twice seemed cleaner to me compared to duplicating the code block. Fixed a little indentation issue in the process

I don't know if it's ever relevant, but config is now patched on all address types, including unix socket.
@bisgardo bisgardo requested a review from 0xTim March 26, 2024 22:22
@bisgardo
Copy link
Contributor Author

Ping @0xTim :)

@bisgardo
Copy link
Contributor Author

@0xTim Is there anything that needs to change before this can get merged? If you disagree with extracting addressDescription I can inline it, no problem.

Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

One minor change then good to go!

Sources/Vapor/HTTP/Server/HTTPServer.swift Outdated Show resolved Hide resolved
@0xTim 0xTim added the semver-patch Internal changes only label Apr 19, 2024
@bisgardo bisgardo changed the title Log actual port on startup Patch configuration and log actual port on startup Apr 22, 2024
Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

Sorry one last thing to add - can you add a test for the configuration? Essentially run the server set to port 0 and then try and read the port from the configuration and make sure it isn't 0

@bisgardo bisgardo requested a review from 0xTim April 23, 2024 14:51
Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

Thanks!

@0xTim 0xTim enabled auto-merge (squash) April 23, 2024 18:53
@0xTim
Copy link
Member

0xTim commented Apr 23, 2024

@bisgardo the new test is failing

auto-merge was automatically disabled April 24, 2024 21:31

Head branch was pushed to by a user without write access

@bisgardo
Copy link
Contributor Author

@bisgardo the new test is failing

It was an existing test, not the new one. I've pushed a fix.

Copy link

codecov bot commented Apr 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.44%. Comparing base (11cdb29) to head (10d373b).
Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3160      +/-   ##
==========================================
+ Coverage   76.86%   77.44%   +0.57%     
==========================================
  Files         211      210       -1     
  Lines        8119     7822     -297     
==========================================
- Hits         6241     6058     -183     
+ Misses       1878     1764     -114     
Files Coverage Δ
Sources/Vapor/HTTP/Server/HTTPServer.swift 95.85% <100.00%> (+15.08%) ⬆️

... and 92 files with indirect coverage changes

@0xTim 0xTim merged commit e5cb0ab into vapor:main Apr 26, 2024
16 checks passed
keniwhat pushed a commit to keniwhat/vapor that referenced this pull request Apr 27, 2024
* main:
  Patch configuration and log actual port on startup (vapor#3160)
  Update provider tests to 5.10 (vapor#3178)
  Migrate to Async NIOFileIO APIs (vapor#3167)
  Removed streamFile deprecation + deactivated advancedETagComparison by default (vapor#3177)
  Remove HeadResponder (vapor#3147)
  Advanced ETag Comparison now supported (vapor#3015)
  Enabled Request Decompression By Default (vapor#3175)
  HTTP2 Response Compression/Request Decompression (vapor#3126)
  Don't set ignore status for SIGTERM and SIGINT on Linux (vapor#3174)
  Fix typos across the codebase (vapor#3162)
  Fix some Sendable warnings on 5.10 (vapor#3158)
  Allow `HTTPServer`'s configuration to be dynamically updatable (vapor#3132)
  Fix issue when client disconnects midway through a stream (vapor#3102)
  Fix handling of "flag" URL query params (vapor#3151)
  Bump the dependencies group with 1 update (vapor#3148)
  Merge Async Tests (vapor#3141)
  Fix URI handling with multiple slashes and variable components. (vapor#3143)
  Fix broken URI behaviors (vapor#3140)

# Conflicts:
#	Package.swift
@bisgardo bisgardo deleted the log-actual-port branch May 7, 2024 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-patch Internal changes only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Log actual port when it's picked by the OS
3 participants