Git Product home page Git Product logo

Comments (7)

xuxueyun avatar xuxueyun commented on July 22, 2024

如果可以,我愿意提交 pr

from msgserver.

xiaogan18 avatar xiaogan18 commented on July 22, 2024

你说的也很有道理,但是我觉得在GC间隔时间内并不会有太多无效连接占用内存空间,而且接收每个客户端断开信息需要额外的增加线程,各有利弊吧。不过你可以详细说说你的看法,一起研讨下

from msgserver.

xuxueyun avatar xuxueyun commented on July 22, 2024

不需要增加线程:

在 handler 最后面增加:

	defer conn.Close()
	for {
		var buf = make([]byte, 512)
		_, err := conn.Read(buf)

		if err != nil {
			// 客户端离线
			ch := make(chan string)
			g.Pool.ReadChan(ch)
			ch <- conn.RemoteAddr().String()
			return
		}
	}

这里可以捕获到连接断开,然后就可将这个连接信息“立即”从 map 中删除,而不必要等下一个 GC 后才知道客户端离线了

from msgserver.

xuxueyun avatar xuxueyun commented on July 22, 2024

这样做还有一个好处就是可以知道在线的客户端,当然,这个在线信息也不是特别准确,对于没有收到客户端主动断开信令的,或者网络突然物理断开的,就只能靠 GC 了

from msgserver.

xiaogan18 avatar xiaogan18 commented on July 22, 2024

实际上我的设计是handler执行完这个goroutine就已经结束了,如果按你说的,那么这个goroutine一直是出于阻塞状态的,有多少个tcp连接就会多等量的goroutine

from msgserver.

xiaogan18 avatar xiaogan18 commented on July 22, 2024

我没有使用tcp通道来接收客户端消息也是这个原因,推送服务大多是推的动作,其他少量收的动作,使用http api的方式,这样基于事件驱动的设计能更多的减少性能消耗

from msgserver.

xuxueyun avatar xuxueyun commented on July 22, 2024

是的,是需要等量的 goroutine 在等待,这个代价在量高的情况下应该不小的…… 完成推送才是主要任务
这个讨论就到这里吧,Thanks♪(・ω・)ノ

from msgserver.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.