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

Change HTTP::Request#remote_address type to Socket::Address? #9210

Merged
merged 4 commits into from
May 11, 2020

Conversation

waj
Copy link
Member

@waj waj commented Apr 30, 2020

This is a breaking change, but probably with a very low impact.

Currently the remote_address is a String with the format "ip:port", so if just the IP is needed extra parsing must be done. Also, this is consistent with other occurrences of local_address and remote_address in the std.

@asterite
Copy link
Member

I implemented it as a string because in Go it's a string. I'm sure there must be a good reason why the chose String. Why do you need the address?

@waj
Copy link
Member Author

waj commented Apr 30, 2020

In this case for logging just the IP. The port changes on every request and is not useful at all for 99.99% of the cases.

Other useful cases are auditing, filtering, geolocation of clients, stats. All these cases need just the IP address without the port.

@asterite
Copy link
Member

Can you split the string by : and just keep the left-hand side?

I think this PR might be fine, but I'd like to know why Go chose to use a String.

@straight-shoota
Copy link
Member

I believe a string is just more flexible.

The port changes on every request and is not useful at all for 99.99% of the cases.

In light of that we might perhaps consider only storing the IP address in remote_address. That would need to be a string because we have no data type for only the IP without port.

@waj
Copy link
Member Author

waj commented Apr 30, 2020

Go is not the only language out there 🙂

@waj
Copy link
Member Author

waj commented Apr 30, 2020

I believe a string is just more flexible

Of course. What's more flexible than a string? But we don't put strings everywhere for that reason. And the address can be easily converted to a string.

@waj
Copy link
Member Author

waj commented Apr 30, 2020

Can you split the string by : and just keep the left-hand side?

Of course I can parse the value, but first it has to be matched with a regex in case is not an IPAddress. But why add that overhead when the original IPAddress was already known?

In the future, if we find another possibility for Address that doesn't fit within the concept of a Socket we could move that class to a different, more general module and add another subclass.

@waj
Copy link
Member Author

waj commented Apr 30, 2020

FYI, I just tested the performance of samples/http_server.cr in my machine with wrk and I obtain 130k req/s without the change and 139k req/s with it. Not a big difference specially thinking that this "hello world" app is not representative but to see that there is an impact creating extra strings.

@asterite
Copy link
Member

Cool! If other languages provide a non-string value then this change is perfect 💯

@waj waj force-pushed the http-request-remote-address-type branch from f32bd60 to 6be86e8 Compare May 1, 2020 03:30
Co-authored-by: Johannes Müller <johannes.mueller@smj-fulda.org>
@waj
Copy link
Member Author

waj commented May 1, 2020

I just disabled some specs in win32, mostly for HTTP. I'm confused about the require "http" within src/mime/media_type.cr but I removed it and then I cannot run some individual specs. So probably a task for a separate PR.

@straight-shoota
Copy link
Member

Disabling that many specs which worked before this PR is not a good idea. We can just make remote_address = nil on win32 for now and it should be fine.

@waj
Copy link
Member Author

waj commented May 1, 2020

Ok, I’ll try that

@waj
Copy link
Member Author

waj commented May 6, 2020

@straight-shoota the specs in win32 are back. I left String? as the type for remote_address in Windows for now.

@straight-shoota
Copy link
Member

Why String? Can't it just be nil on win32?

Public APIs should not have different types depending on the platform.

@waj
Copy link
Member Author

waj commented May 6, 2020

I put String to avoid disabling more specs. Nobody is going to use HTTP on Windows yet, because sockets are not working yet, AFAIK.

I mean, I don't expect the APIs to be different once it's working on Windows.

@waj waj added this to the 0.35.0 milestone May 7, 2020
@waj waj merged commit 90d89d0 into crystal-lang:master May 11, 2020
@waj waj deleted the http-request-remote-address-type branch May 11, 2020 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants