-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Add support for fasthttp #98
Conversation
OK, Travis failed because |
I pulled the fasthttp stuff into its own file and added a |
I still need to update some of the copyright years and add the copyright header to the new file. I could do a new commit or |
This is the first I've heard of the fasthttp package. I'll need to learn more about the package before deciding if I want add a dependency to the package. If a dependency is added, it will need to be in a new package (github.com/gorilla/websocket/need-a-good-name-here). The API in this PR is inconvenient for the many applications that need to access the request before upgrading the connection. The API does not provide a way for applications to set cookies on the handshake response. I suggest changing the API to:
I am not a fan of the constants added in the PR. I think the code |
Hi Gary, I think https://github.com/valyala/fasthttp/tree/master/examples/fileserver As far as your recommended changes, it is still possible to add cookies with the current design because of how fasthttp works: upgrader := &websocket.FastHTTPUpgrader{
CheckOrigin: func(ctx *fasthttp.RequestCtx) bool { return true },
Handler: someWebsocketHandler,
}
addCookies := func(ctx *fasthttp.RequestCtx) {
cookie := &fasthttp.Cookie{}
cookie.SetKey("MyKey")
cookie.SetValue("MyValue")
ctx.Response.Header.SetCookie(cookie)
upgrader.UpgradeHandler(ctx)
}
if err := fasthttp.ListenAndServe(":12345", addCookies); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
} Though I do like the idea of having the callback as a parameter to avoid the annoying issue if it not being provided in the struct. But the reason I didn't do that was to make As for the constants, we can probably agree to disagree. I see your point but then the same string is repeated in multiple places because I only added the constants when the string was used repeatedly. But I can change that if you really dislike it. |
One major issue with putting this in a sub-package of gorilla/websocket is then it couldn't use any of the unexported utility functions. Most especially we would need to export some way to make a If you really don't want to make func NewServerConn(conn net.Conn, subprotocol string, readBufferSize, writeBufferSize int) *Conn Which of course just calls There would be some repeated code for the handshake stuff, but nothing too crazy. Mainly I don't see a reason to reinvent Then I could put all this into a new |
The Gorilla upgrade API is designed the way it is because it's convenient for applications. The design was not dictated by design of the net/http hijack API. A sub-package can be supported by moving utility functions to an internal package. The proposed NewServerConn requires more options. The handshake response headers and compression options are the two that come to mind. I expect that more options will be added in the future. |
I like this package and the design but a big reason you need the responseHeader parameter in As for the subpackage yeah I think an internal package could work, it would just require Go 1.4 or above I think. Either way: what do you think the next steps should be on this? I'm working today and then will take a long vacation for the holidays. But if I get a chance I might be able to do some work on this. Obviously no one else really needs it at this point so there isn't a huge hurry. |
Godoc.org lists two importers of the package. There are two contributors. The package does not have enough traction for this project to take on code specific to fasthttp. I suggest that fasthttp provide an adaptor from its API to the net/http API. An application can set headers on a response writer header map and pass the response writer along. An application can set values on the header map and later hijack the connection. The internal package structure can be used on all versions of Go. Go 1.4 and above is required restrict access to the packages per the rules in the design document. |
I'm surprised there are even that many importers this early on. I understand your reasoning in not wanting to take on code for such a new project. I think making an adaptor between net/http and fasthttp would be pretty tricky, especially for hijacking. My understanding is that the fasthttp author broke away from the net/http API because it limited certain optimization possibilities. I'll just close this PR and maintain my fork with the fasthttp websocket upgrader. Later on if fasthttp gets more traction we can reconsider merging. |
@garyburd, the situation has changed since then. Can we reconsider it now? |
How has the situation changed? The issues above have not been addressed. |
|
I use Although there is an adaptor for fasthttp here |
Adding
Please correct me if otherwise. |
@elithrar You are correct on all counts except one in my opinion. There doesn't need to be a dependency between this package and |
This package should have a dependency with net/http so that package users
don't have to stitch a bunch of logic together and hope for the best.
Reviewing many of the issues created here, making it more "manual" to get
started would turn many away, for minimal benefit. If there were no defacto
Go HTTP library, sure, but that's not where we are today.
…On Sun, May 21, 2017 at 1:34 PM Nikunj Yadav ***@***.***> wrote:
@elithrar <https://github.com/elithrar> You are correct on all counts
except one in my opinion. There doesn't need to be a dependency between
this package and net/http. So it seems to me that making certain methods
public would not be such a bad idea ?
I stumbled on this issue while looking to use websockets, I am not super
passionate about the PR either cause I can achieve this on my own too.
Although I DO think certain methods might be useful to expose in the public
API.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#98 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABIcMzals_iKUuNxpf0J7rgZ2__Sc35ks5r8J_UgaJpZM4G14xd>
.
|
Here's a summary of the issues with this PR:
This is based on a quick skim of my comments above. There may be other problems. |
Open a new issue to discuss fasthttp support. Please limit discussion here to this specific PR. |
As I have mentioned before I don't really have time to maintain these changes I made. At the moment I don't have need of them. As @garyburd is suggesting there is probably a cleaner way of adding support for fasthttp to gorilla. Both projects have probably changed a lot since I wrote this. Either way it really wasn't rocket science to implement the upgrade logic so I would suggest someone take a look at what I did, consider Gary's advice, and do a new PR which adds support for fasthttp in a way which this project can merge. That might involve making some currently private APIs public, which I'm not sure gorilla would want to do, but at this point I leave that up to you guys, because bottom line I won't be working on this any time soon. |
So this is a bit of a "speculative" PR in that I don't know if you guys are open to this kind of contribution and also whether the way I have written it matches the rest of the project.
The basic idea is I need websocket support in a project which uses the relatively new Go HTTP package fasthttp.
Gorilla websocket seemed like the best option to add this.
As you can see in the diff I have refactored various non-exported utility functions to make them usable from both
net/http
andfasthttp
code, and then I added aFastHTTPUpgrader
. The way fasthttp does hijacking is quite a bit different (and better IMO) thannet/http
, so theFastHTTPUpgrader .UpgradeHandler()
is quite a bit more sane than the one for the standardUpgrader.Upgrade()
.The existing tests pass but I did not add any tests for my addition. I could do that but didn't want to spend the time if this PR would be rejected.