如何将所有goroutine中的数据/事件发布到Web服务?

huangapple go评论80阅读模式
英文:

How can i publish data/events from all goroutines to a webservice?

问题

我的项目是一个TCP服务器(不是HTTP),工作原理如下:

main() {
    for {
        conn, err := listener.Accept()
        go handleClient(conn, &Client{})
    }
}

通常我会有数百个客户端同时连接。

handleconnection函数中,我的“事件”大致如下:

IP
1.2.3.4 客户端连接
1.2.3.4 客户端发送命令 XYZ
1.2.3.4 客户端发送数据
服务器发送数据给 1.2.3.4
1.2.3.4 客户端发送命令 XYZ
[重复 N 次]

对于每个事件,我想要创建一个“实时视图”,显示所有 goroutine 和所有客户端的事件。基本上,你可以说它是每个连接客户端的状态。

类似于以下的内容:

客户端IP	状态			时间		总KB	
1.2.3.4		连接			1		0
5.6.7.8		发送数据		14		22
10.10.10.10	接收数据		22		11

我在每个连接(handleconn)中都有所需的所有信息,但我不知道如何将其提供给一个 Web 服务。

问题:我如何使所有这些事件状态消息可用于一个函数、另一个 goroutine 或能够收集所有 goroutine 中的所有数据并发布的其他方法?

我需要使用通道吗?还是只需要启动一次的另一个 goroutine,并使用通道接收数据?

或者还有其他完全不同的方法来收集和发布数据吗?

免责声明:我对编程和 Golang 非常新手。只是一个试图学习的老家伙。

英文:

My Project is a TCP server (not http) and works something like this

main() {

for {
	conn, err := listener.Accept()
	go handleClient(conn, &Client{})
}

I usually have hundreds of clients connecting at the same time

inside handleconnection, my "events" look something like this

IP
1.2.3.4 client connect
1.2.3.4 client send command XYZ
1.2.3.4 client send data
server send data to 1.2.3.4
1.2.3.4 client send command XYZ
[repeated N times]

For each of these events, i want to create a "real time view" of events accross my goroutines and all the clients. Basically, you could say its a status for each connecting client

something that would looke like

client IP	status			time	total KB	
1.2.3.4		connect			1		0
5.6.7.8		send data		14		22
10.10.10.10	recieving data	22		11		

I have all the information i need in each connections (handleconn) but i have no idea how to make this available for a webservice.

Problem: How do i make all of these event status msg availble for a function, or another goroutine or something which can collect everything from all goroutines and publish it?

Do i need to use channels for this? or another go routine which is launched only one time and can recieve data using channels

Or a completely other approach to collect and publish the data?

disclaimer: Im very new to programming and golang. Just an old dude trying to learn

答案1

得分: 4

在Go库中有一个专门用于此用途的包,名为expvar。根据文档,它已经为您完成了所有的Web服务封装工作:

http://golang.org/pkg/expvar/

英文:

There is a package in the Go library that is designed for this use, expvar. According to the docs, it already does all the wrapping as web service for you:

http://golang.org/pkg/expvar/

答案2

得分: 1

你还可以拥有一个名为Supervisor的类型,它将保持对所有Connection的注册。

每个Connection将具有一些属性,例如ClientIPStatusTimeTotalKB

当有新的连接时,实例化一个新的Connection,将其添加到supervisor的注册表中,并生成一个Go协程。该Go协程将根据情况更新自己的信息。

通过Supervisor,您可以访问所有的Connections,因此可以在终端或Web上显示您想要的内容。这取决于您。

为了保持注册表的最新状态,您可以使用Tomb,当您需要跟踪Go协程时,它非常有用。

希望这能帮到您。

英文:

You can also have a type Supervisor which will keep a registry of all your Connection.

Each Connection will have some properties such as ClientIP, Status, Time and TotalKB.

When there is a new connection, instantiate a new Connection, add it to the supervisor's registry and spawn a Go routine. The go routine will update its own informations depending on what's going on.

From the Supervisor you have access to all the Connections, and you can therefore display what you want on the terminal or web. It's up to you.

To keep the registry up-to-date, you can use Tomb, which is really useful when you have to track go routines.

I hope it will help

huangapple
  • 本文由 发表于 2014年7月24日 14:07:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/24926251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定