fasthttp / websocket Goto Github PK
View Code? Open in Web Editor NEWWebSocket implementation for fasthttp.
License: BSD 3-Clause "New" or "Revised" License
WebSocket implementation for fasthttp.
License: BSD 3-Clause "New" or "Revised" License
a random panic with this stack trace, cant reproduce it just a random, and happen from time to time
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x1adec3e]
goroutine 79691 [running]:
github.com/valyala/fasthttp.(*hijackConn).SetWriteDeadline(0xc000bcdf80, 0xbf5cbb4f4adda610, 0x238e4ae57c47, 0x397c860, 0x2, 0x0)
<autogenerated>:1 +0x2e
github.com/fasthttp/websocket.(*Conn).write(0xc000c40dc0, 0x8, 0xbf5cbb4f4adda610, 0x238e4ae57c47, 0x397c860, 0xc000cfd30c, 0x2, 0x1002, 0x39a0e10, 0x0, ...)
/home/max/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:389 +0x11f
github.com/fasthttp/websocket.(*messageWriter).flushFrame(0xc00100fe20, 0x39a0e01, 0x39a0e10, 0x0, 0x0, 0x0, 0xcbbea8)
/home/max/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:614 +0x1f3
github.com/fasthttp/websocket.(*Conn).WriteMessage(0xc000c40dc0, 0x8, 0x39a0e10, 0x0, 0x0, 0x238e4ae57c47, 0x397c860)
/home/max/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:763 +0x23d
main/websocks.(*Client).StartWriter(0xc000f52180)
/main/websocks/client.go:127 +0x7eb
can anybody help or maybe have an idea how to fix?
valyala/fasthttp#597
this is the issues,but I find in upgrader.Upgrade(ctx,func(ws *websocket.Conn) {
fmt.Println(string(ctx.RequestURI())) //here the ctx is not fill with value ,the value is the correct
})
How do we upgrade a *fasthttp.RequestCtx
??
Any plan to support Fiber v2?
There is a type mismatch if we use the newest Fiber v2
cannot use websocket.New((func(c *websocket.Conn) literal)) (value of type func(*fiber.Ctx)) as func(*fiber.Ctx) value in return
When trying to fetch project, I get following error
go get -u github.com/fasthttp/websocket
github.com/fasthttp/websocket imports
github.com/savsgio/gotils: cannot find module providing package github.com/savsgio/gotils
I have the following benchmark:
for i := 0; i < b.N; i++ {
_, err := c.NextWriter(websocket.BinaryMessage)
require.Nil(b, err)
}
Result:
8137 ns/op 496 B/op 7 allocs/op
Benchmark like this:
for i := 0; i < b.N; i++ {
err = c.WriteMessage(websocket.BinaryMessage, []byte("Wow wow wow"))
require.Nil(b, err)
}
gives the following:
8676 ns/op 512 B/op 8 allocs/op
Seems NextWriter() is a "slow" operation because it allocates memory on the heap. Can this allocation be avoided somehow?
I think the same is true for NextReader()
this rfc says that upgrade header values should be treated as case-insensitive ASCII https://tools.ietf.org/html/rfc6455#section-4.2.1
but in source code, the comparison is case sensitive
func (u *FastHTTPUpgrader) Upgrade(ctx *fasthttp.RequestCtx, handler FastHTTPHandler) error {
// ...
if !bytes.Contains(ctx.Request.Header.Peek("Connection"), strUpgrade) {
return u.responseError(ctx, fasthttp.StatusBadRequest, fmt.Sprintf("%s 'upgrade' token not found in 'Connection' header", badHandshake))
}
// ...
}
fasthttp/websocket module returns "websocket: bad handshake" error when HTTP response code is different than 101 or header values are incorrect. This hides any other non-websocket HTTP errors like 401 Unauthorized, 404 Not Found, 500 Internal Server Error, etc. Recently I was debugging one such issue and had to modify fasthttp/websocket to see what was wrong. Please add new error to report non-101 response from server. Make sure to allow getting HTTP status code from error - some errors may need special handling beside logging, e.g. prompt user for credentials when server returns 401 Unauthorized.
https://github.com/fasthttp/websocket/blob/master/client.go#L393-L396
Hi,
Can you show this as a fork of gorilla/websocket library so that it is easy to understand how much it is behind/ahead/diff from the gorilla/websocket lib?
Thanks.
https://github.com/nhooyr/websocket is much better than gorilla/websocket.
Is it possible to have fasthttp support for that?
In the release section, it is written that the version 1.4.2 has been re-uploaded. This has introduced some install failures :
verifying github.com/fasthttp/[email protected]: checksum mismatch
downloaded: h1:AU/zSiIIAuJjBMf5o+vO0syGOnEfvZRu40xIhW/3RuM=
go.sum: h1:hTnl0DRQ9Tj4uVc4QcZCGp+mgF5mVKnijR1eNyn2waY=
SECURITY ERROR
This download does NOT match an earlier download recorded in go.sum.
The bits may have been replaced on the origin server, or an attacker may
have intercepted the download attempt.
For more information, see 'go help module-auth'.
In my company, we are currently using internal go proxy based on JFrog. Even if I remove entries in my project go.sum, it is still failing. If I'm using golang proxy and removing these entries, it is working.
Unfortunately, our CI is using our internal proxy and we are completely stuck.
Is it possible for you to release a new tag v1.4.3
in order to fix it ? Thx.
I saw this to handle client disconnect.
Please read this:
gorilla/websocket#34
https://github.com/gorilla/websocket/blob/master/examples/chat/client.go
i'm curious if there's an easier way to do this ping pong thing. any other elegant examples with lesser code?
with reference to this
valyala/fasthttp#362
When using upgrading with the gorilla library
conn, err := upgrader.Upgrade(w, r, nil)
Using the fasthttp upgrader, we have a return function
upgrader.Upgrade(fctx, func(conn *websocket.Conn) {
// use conn
})
This works fine, but I was wondering if its possible to wrap this without being bound to the return function.
var upgrader = websocket.FastHTTPUpgrader{}
func upgrade(fctx *fasthttp.RequestCtx) <-chan *websocket.Conn {
channel := make(chan *websocket.Conn)
go func() {
defer close(channel)
upgrader.Upgrade(ctx, func(conn *websocket.Conn) {
channel <- conn
})
}()
return channel
}
conn := <-upgrade(fctx)
// <nil>
The IsWebsocketUpgrade function is returning false, leading to the server returning a 426. You can try this out by going to https://www.websocket.org/echo.html and putting in the location as wss://api.nv7haven.tk/ws/123?v=1.0
. Any ideas why this could be happening?
I am having panic fatal error, whenever client tries to reconnect
panic: repeated read on failed websocket connection
goroutine 67 [running]:
github.com/fasthttp/websocket.(*Conn).NextReader(0xc005a14160, 0xffffffffffffffff, 0x0, 0x0, 0xc5f5a0, 0xc00000f500)
/home/ubuntu/go/pkg/mod/github.com/fasthttp/websocket@v1.4.2/conn.go:1001 +0x343
github.com/fasthttp/websocket.(*Conn).ReadMessage(0xc005a14160, 0xffffffffffffffff, 0x0, 0x0, 0x0, 0xc5f5a0, 0xc00000f500)
/home/ubuntu/go/pkg/mod/github.com/fasthttp/websocket@v1.4.2/conn.go:1064 +0x2f
main.main.func10(0xc0003d38e0)
/home/ubuntu/go/src/projectTwo/main.go:309 +0x1ea
github.com/gofiber/websocket.New.func2.2(0xc005a14160)
/home/ubuntu/go/pkg/mod/github.com/gofiber/websocket@v0.3.0/main.go:91 +0xab
github.com/fasthttp/websocket.(*FastHTTPUpgrader).Upgrade.func1(0xc6ffe0, 0xc0003d0cc0)
/home/ubuntu/go/pkg/mod/github.com/fasthttp/websocket@v1.4.2/server_fasthttp.go:201 +0x227
github.com/valyala/fasthttp.hijackConnHandler(0xc5f140, 0xc0001f6ae0, 0xc70160, 0xc00031ef90, 0xc000148c00, 0xc00023cf40)
/home/ubuntu/go/pkg/mod/github.com/valyala/fasthttp@v1.12.0/server.go:2228 +0x7b
created by github.com/valyala/fasthttp.(*Server).serveConn
/home/ubuntu/go/pkg/mod/github.com/valyala/fasthttp@v1.12.0/server.go:2189 +0xa82
exit status 2
My main.go:309 line is:
_, data, err := c.ReadMessage()
Deprecated : As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details.
It is currently in use here:
❯ rg 'ioutil\.' .
./conn_broadcast_test.go
48: w: ioutil.Discard,
./conn.go
846: if _, err := io.CopyN(ioutil.Discard, c.br, c.readRemaining); err != nil {
1157: p, err = ioutil.ReadAll(r)
./client_server_test.go
552: p, err := ioutil.ReadAll(resp.Body)
./compression_test.go
45: w := ioutil.Discard
56: w := ioutil.Discard
./conn_test.go
128: rbuf, err := ioutil.ReadAll(r)
370: _, err = io.Copy(ioutil.Discard, r)
404: _, err = io.Copy(ioutil.Discard, r)
429: _, err = io.Copy(ioutil.Discard, r)
493: _, err = io.Copy(ioutil.Discard, r)
./client.go
403: resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
421: resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
./_examples/filewatch/main.go
52: p, err := ioutil.ReadFile(filename)
./_examples/filewatch/fasthttp/main.go
52: p, err := ioutil.ReadFile(filename)
./_examples/autobahn/server.go
87:// with ioutil.ReadAll.
./_examples/autobahn/fasthttp/server.go
89:// with ioutil.ReadAll.```
My client was mastodon(a decentralized social media like twitter) web and native apps.
My server was powered by fiber and websocket component was offical websocket middleware of fiber based on this amazing library.
The issue was websocket upgrade prediction websocket.IsWebSocketUpgrade(ctx)
was always false.
I checked the request and dig into the source code, and found:
ws client sent request header with Upgrade: websocket
, but the server side only return true when the value was Websocket
.
Does it should convert the value of Upgrade
header to same case before checking?
Line 227 in c48d95b
// FastHTTPIsWebSocketUpgrade returns true if the client requested upgrade to the
// WebSocket protocol.
func FastHTTPIsWebSocketUpgrade(ctx *fasthttp.RequestCtx) bool {
return tokenContainsValue(strconv.B2S(ctx.Request.Header.Peek("Connection")), "Upgrade") &&
tokenContainsValue(strconv.B2S(ctx.Request.Header.Peek("Upgrade")), "Websocket")
}
Refer: https://github.com/gorilla#gorilla-toolkit
Questions:
I am focus on a websocket proxy gateway works,so i need both server and client of websocket. but i notidy that this project support only server side of websocket base on fasthttp,is there any plan to support the client side ?
Fast HTTP is using well-optimized https://github.com/klauspost/compress library for compression. I think we can change it in here too, with that way we can get very nice performance and resource optimization for websocket connections with compression.
PANIC: runtime error: invalid memory address or nil pointer dereference
STACK:
goroutine 83 [running]:
c:/go/src/runtime/panic.go:513 +0x1c7
github.com/valyala/fasthttp.(*hijackConn).SetWriteDeadline(0xc000004040, 0x0, 0x0, 0x0, 0x46e47b8, 0xc000160101)
:1 +0x39
github.com/fasthttp/websocket.(*Conn).write(0xc00008a000, 0x1, 0x0, 0x0, 0x0, 0xc00051200a, 0x2e4c, 0xfa004, 0xc0002b4e48, 0x0, ...)
C:/gopath/src/github.com/fasthttp/websocket/conn.go:376 +0x102
github.com/fasthttp/websocket.(*messageWriter).flushFrame(0xc000501ed8, 0xc0002b2001, 0xc0002b4e48, 0x0, 0x1b8, 0x0, 0x0)
C:/gopath/src/github.com/fasthttp/websocket/conn.go:601 +0x24d
github.com/fasthttp/websocket.(*Conn).WriteMessage(0xc00008a000, 0x1, 0xc0002b2000, 0x2e48, 0x3000, 0x0, 0x0)
C:/gopath/src/github.com/fasthttp/websocket/conn.go:753 +0x252
main.(*Client).writePump(0xc000148030)
C:/sssr/test_socket.go:140 +0x19e
created by main.serveWebsocket.func1
C:/sssr/test_socket.go:169 +0x13d
code is:
...
buf, _ := json.Marshal(msgd)
err := c.conn.WriteMessage(websocket.TextMessage, buf)
Hi,
currently, within the handler function, each user will create a seperate goroutine as reader/writer.
Is it possibile for fasthttp/websocket to handle multiple user on a single goroutine with a kind of (may be) event loop?
my code is here:
https://pastebin.com/raw/hURatUqs
failed at
conn.WriteMessage(websocket.TextMessage []byte(s))
if will work if i comment
nc.Subscribe(subj, func(
so i don't understand why they cant work together
===================
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x75e3b2]
goroutine 61 [running]:
github.com/valyala/fasthttp.(*hijackConn).SetWriteDeadline(0xc007ef0a00, 0x0, 0x0, 0x0, 0xc0000aa000, 0x203001)
:1 +0x32
github.com/fasthttp/websocket.(*Conn).write(0xc0067dc420, 0x1, 0x0, 0x0, 0x0, 0xc007efa90c, 0x3, 0x402, 0xc0075156b1, 0x0, ...)
/go/src/github.com/fasthttp/websocket/conn.go:376 +0xfb
github.com/fasthttp/websocket.(*messageWriter).flushFrame(0xc00680fec8, 0xc007515601, 0xc0075156b1, 0x0, 0x7, 0x8, 0xc0075156b0)
/go/src/github.com/fasthttp/websocket/conn.go:601 +0x23d
github.com/fasthttp/websocket.(*Conn).WriteMessage(0xc0067dc420, 0x1, 0xc0075156b0, 0x1, 0x8, 0x8, 0x0)
/go/src/github.com/fasthttp/websocket/conn.go:753 +0x24b
main.requestHandler.func1.1(0xc007f2c000)
/usr/local/src/programs/r/r.go:496 +0xd1
github.com/nats-io/go-nats.(*Conn).waitForMsgs(0xc007f04000, 0xc0044003c0)
/go/src/github.com/nats-io/go-nats/nats.go:2024 +0x258
created by github.com/nats-io/go-nats.(*Conn).subscribe
/go/src/github.com/nats-io/go-nats/nats.go:2789 +0x460
After I close the client connection, I write message on it with conn.WriteJSON() and here is what happens:
So the question is why the very first write on a closed connection does not return error ?!
I've been reading about how you can increase WebSocket performance by creating an epoll to handle file descriptors. I've tried every possible way to obtain it from *websocket.Conn, but I couldn't find any solution. Has anyone tried this before?
Hi!
v1.4.3-beta.5
v1.4.3-beta.4
v1.4.3-beta.3
v1.4.3-beta.2
v1.4.3-beta.1
Why so many beta-releases?
Are they used to test something?If not - what's the purpose?
Could you please tag some stable release if they are good enough for production use? :)
When using fiber/websocket
, which in turn use fasthttp/websocket
, I get following errors:
panic: concurrent write to websocket connection
goroutine 31 [running]:
github.com/fasthttp/websocket.(*messageWriter).flushFrame(0xc00032c240, 0x1, {0x0?, 0x7f3a1327ca68?, 0x30?})
/home/eric/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:617 +0x52b
github.com/fasthttp/websocket.(*messageWriter).Close(0x0?)
/home/eric/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:731 +0x45
github.com/fasthttp/websocket.(*Conn).beginMessage(0xc0000bd080, 0xc00043f1a0, 0x1)
/home/eric/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:480 +0x42
github.com/fasthttp/websocket.(*Conn).NextWriter(0xc0000bd080, 0x1)
/home/eric/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:520 +0x45
github.com/fasthttp/websocket.(*Conn).WriteJSON(0xc000019a80?, {0xb34be0, 0xc000367c40})
/home/eric/go/pkg/mod/github.com/fasthttp/[email protected]/json.go:24 +0x45
github.com/deckarep/golang-set.(*threadSafeSet).Each(0xc0004445e0, 0xc00043f170)
/home/eric/go/pkg/mod/github.com/deckarep/[email protected]/threadsafe.go:157 +0xaf
github.com/gofiber/websocket/v2.New.func2.4(0x0?)
/home/eric/go/pkg/mod/github.com/gofiber/websocket/[email protected]/websocket.go:109 +0x75
github.com/fasthttp/websocket.(*FastHTTPUpgrader).Upgrade.func1({0xd52d50, 0xc0000a9770})
/home/eric/go/pkg/mod/github.com/fasthttp/[email protected]/server_fasthttp.go:201 +0x1ad
github.com/valyala/fasthttp.hijackConnHandler(0x0?, {0xd4b3c0?, 0xc000412cc0}, {0xd52f08, 0xc0001a2130}, 0xc0001cbd40, 0xc000367580)
/home/eric/go/pkg/mod/github.com/valyala/[email protected]/server.go:2479 +0x68
created by github.com/valyala/fasthttp.(*Server).serveConn
/home/eric/go/pkg/mod/github.com/valyala/[email protected]/server.go:2434 +0x1d85
exit status 2
There are 2 go-routines write to the websocket connection. I know cause of the issue is concurrent write on the connection.
I got 2 ideas on how this maybe fixed:
Any suggestion on which of the solution is better, or there are other solutions?
Using Fiber v2.38.1, which in turn use fasthttp/websocket v1.5.0.
App crashed with following error:
2022-10-15T04:05:42.983563+00:00 app[web.1]: time="2022-10-15T04:05:42Z" level=info msg="close msg received: &{418 close}"
2022-10-15T04:05:42.983564+00:00 app[web.1]: conn is nil: false, msg: &{418 close <nil> conn 0}
2022-10-15T04:05:42.986035+00:00 app[web.1]: panic: runtime error: invalid memory address or nil pointer dereference
2022-10-15T04:05:42.986035+00:00 app[web.1]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x78 pc=0x8db967]
2022-10-15T04:05:42.986036+00:00 app[web.1]:
2022-10-15T04:05:42.986037+00:00 app[web.1]: goroutine 86 [running]:
2022-10-15T04:05:42.986037+00:00 app[web.1]: github.com/fasthttp/websocket.(*Conn).beginMessage(0x0, 0xc000348f60, 0x1)
2022-10-15T04:05:42.986037+00:00 app[web.1]: /tmp/codon/tmp/cache/go-path/pkg/mod/github.com/fasthttp/[email protected]/conn.go:479 +0x27
2022-10-15T04:05:42.986038+00:00 app[web.1]: github.com/fasthttp/websocket.(*Conn).NextWriter(0x0, 0x1)
2022-10-15T04:05:42.986038+00:00 app[web.1]: /tmp/codon/tmp/cache/go-path/pkg/mod/github.com/fasthttp/[email protected]/conn.go:520 +0x45
2022-10-15T04:05:42.986039+00:00 app[web.1]: github.com/fasthttp/websocket.(*Conn).WriteJSON(0xe79a80?, {0xc143a0, 0xc00041e600})
2022-10-15T04:05:42.986039+00:00 app[web.1]: /tmp/codon/tmp/cache/go-path/pkg/mod/github.com/fasthttp/[email protected]/json.go:24 +0x45
2022-10-15T04:05:42.986039+00:00 app[web.1]: github.com/kuchaguangjie/go-fit/model.(*WsClient).WriteMsg(0xc00056a280, 0xc00041e600)
2022-10-15T04:05:42.986040+00:00 app[web.1]: /tmp/build_0b2fe533/model/ws_model.go:97 +0xf9
2022-10-15T04:05:42.986040+00:00 app[web.1]: github.com/kuchaguangjie/go-fit/controller.(*ContentController).Ws.func1.2()
2022-10-15T04:05:42.986041+00:00 app[web.1]: /tmp/build_0b2fe533/controller/content_ctl.go:623 +0x171
2022-10-15T04:05:42.986041+00:00 app[web.1]: created by github.com/kuchaguangjie/go-fit/controller.(*ContentController).Ws.func1
2022-10-15T04:05:42.986042+00:00 app[web.1]: /tmp/build_0b2fe533/controller/content_ctl.go:608 +0x10cc
2022-10-15T04:05:43.113930+00:00 heroku[web.1]: Process exited with status 2
It crashed at conn.go:479
, in function beginMessage()
:
if c.writer != nil {
I've checked that c
is not nil (as shown in log), so how it crashed ...
BTW, it's deployed on heroku, I saw following log on heroku:
2022-10-15T04:59:30.344791+00:00 heroku[router]: at=error code=H15 desc="Idle connection"
Not sure is it relevent.
Good day
I'd like to enquire about the project status?
Is this still necessary? What is the performance like compared to Gorilla websockets?
Kind regards
Justin
Hi,
Is it possibile for fasthttp/websocket to handle multiple user on a single goroutine with a kind of (may be) event loop?
Hi,
The value of "Connection: Upgrade" and "Upgrade: websocket" should be treated as case INSENSITIVE but this library treats them case-sensitive.
Look at this:
https://github.com/fasthttp/websocket/blob/7eb5830af7/server_fasthttp.go#L144-L150
In IETF RFC6455 Section 4.2.1, https://tools.ietf.org/html/rfc6455#section-4.2.1
This field should be treated as an ASCII case-insensitive value.
Results:
This library responses 400 Bad Request when running behind AWS ALB, which use "Connection: upgrade" for any upgrade request.
This issue be already created in #28 but closed by author.
I use Insomnia for test my API and when I trying to test WS connection I got failed on websocket.FastHTTPIsWebSocketUpgrade(...)
Insomnia sends Upgrade: websocket
header but FastHTTP is waiting for Upgrade: Websocket
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.