-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[question] Is that gorilla websocket have limit the broadcast message in a second? #648
Comments
Is your client written to handle multiple JSON documents in a single WebSocket message? Show the code with x/net/websocket that worked. |
no |
|
The gorilla package does not have a limit the broadcast message in a second. If your client is not written to handle multiple JSON documents in a WebSocket message, then remove your code that writes multiple documents to a message. Your x/net/websocket code writes a single JSON document per WebSocket message. If you do the same in with the gorilla code, the application will probably work as expected. |
did you mean this?
|
Yes, that is your code that adds multiple documents to a websocket message. |
Ok, did gorilla have any variable or api that can make the client receive more message in a second? |
Gorilla does not have a variable or API that limits the number of messages per second. The code to pack multiple JSON documents into a single WebSocket message will trigger then sending messages at a very high rate. Given that your client is not designed to handle multiple JSON documents in a single WebSocket message, you can expect your application to lose messages when sending messages at a very high rate. Delete the code quoted a couple of comments back and test again. |
Describe the problem you're having
I use golang.org/x/net/websocket to broadcast about 100 message in a second is no problem, the client can receive all the message, but when I use gorilla websocket, only receive less than 10 message then websocket clientv will be corrupted.
…
Versions
…
"Show me the code!"
package main
import (
"encoding/json"
"github.com/gorilla/websocket"
"log"
"net/http"
"time"
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {return true},
}
var newline = []byte{'\n'}
type Client struct {
hub *Hub
conn *websocket.Conn
send chan msg
see string
}
type Hub struct {
clients map[*Client]bool
broadcast chan msg
register chan *Client
unregister chan *Client
}
func newHub() *Hub {
return &Hub{
broadcast: make(chan msg),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]bool),
}
}
func (h *Hub) run() {
for {
select {
case client := <-h.register:
h.clients[client] = true
case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
close(client.send)
}
case message := <-h.broadcast:
for client := range h.clients {
if client.see == message.LogName {
client.send <- message
}
//select {
//case client.send <- message:
//default:
// close(client.send)
// delete(h.clients, client)
//}
}
}
}
}
func (c *Client) readPump() {
defer func() {
c.hub.unregister <- c
_ = c.conn.Close()
}()
c.conn.SetReadLimit(maxMessageSize)
_ = c.conn.SetReadDeadline(time.Now().Add(pongWait))
c.conn.SetPongHandler(func(string) error { _ = c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
ErrorLogger.Printf("readPump failed with error: %v", err)
}
break
}
}
func (c *Client) writePump() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
_ = c.conn.Close()
}()
for {
select {
case message, ok := <-c.send:
_ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if !ok {
// The hub closed the channel.
_ = c.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
}
// serveWs handles websocket requests from the peer.
func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
return
}
client := &Client{hub: hub, conn: conn, send: make(chan msg, 1)}
client.hub.register <- client
}
The text was updated successfully, but these errors were encountered: