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

[BUG] Socket missed a lot of msgs #58

Open
shixiaofeia opened this issue Jun 28, 2021 · 3 comments
Open

[BUG] Socket missed a lot of msgs #58

shixiaofeia opened this issue Jun 28, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@shixiaofeia
Copy link

Describe the bug
neffos.Conn.Socket().ReadData(0) There is a case of missing messages, for example, I send 1-10, but it only receives 2,3

To Reproduce

package main

import (
	"fmt"
	websocket2 "github.com/gorilla/websocket"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/iris/v12/websocket"
	"github.com/kataras/neffos"
	"github.com/kataras/neffos/gorilla"
	uuid "github.com/satori/go.uuid"
	"log"
	"net/http"
	"time"
)

var ws *neffos.Server

func main() {
	upgrade := websocket2.Upgrader{
		// 允许跨域
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}

	ws = neffos.New(gorilla.Upgrader(upgrade), neffos.Namespaces{})
	app := iris.New()
	app.Get("/websocket_endpoint", Hello)
	log.Fatal(app.Run(iris.Addr(":9999")))
}

// Hello
func Hello(ctx iris.Context) {
	conn := websocket.Upgrade(ctx, func(ctx context.Context) string {
		return uuid.NewV4().String()
	}, ws)
	go production(conn)
	go consumer(conn)
}

// production 生产
func production(conn *neffos.Conn) {
	for {
		time.Sleep(1 * time.Second)
		if err := conn.Socket().WriteText([]byte("111"), 0); err != nil {
			if conn.IsClosed() {
				return
			}
			fmt.Println("err: " + err.Error())
		}
	}
}

// consumer 消费 TODO 存在漏接消息的情况
func consumer(conn *neffos.Conn) {
	for {
		data, _, err := conn.Socket().ReadData(0)
		if err != nil {
			if conn.IsClosed() {
				return
			}
			fmt.Println("consumer err: " + err.Error())
			continue
		}
		fmt.Println(fmt.Sprintf("consumer data: %v", string(data)))
	}
}

Expected behavior

Screenshots
I missed a lot of msgs

@shixiaofeia shixiaofeia added the bug Something isn't working label Jun 28, 2021
@shixiaofeia shixiaofeia changed the title [BUG] [BUG] Socket missed a lot of msgs Jun 28, 2021
@trading-peter
Copy link

If SyncBroadcaster is set to true the bug seems to be circumvented. @kataras do you have any idea what is happening here?

@AlanTianx
Copy link

AlanTianx commented Dec 9, 2021

如果SyncBroadcaster设置成true这个bug似乎可以绕开。@kataras你知道这里发生了什么吗?
I read the source code and found that broadcaster.broadcast() and Server.waitMessages() will compete with each other for locks, so when a large number of broadcast messages and Server.waitMessages() are weak, some messages will be lost.

see Picture:

  1. server.Upgrade() open it
    image

image

  1. When broadcasting a message
    image

@kataras I hope you can value it! thanks

@shixiaofeia
Copy link
Author

Using the SyncBroadcaster parameter is also not possible, and now you can receive fewer messages, my version:
github.com/kataras/neffos v0.0.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants