去大猩猩websocket写入截止时间

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

Go gorilla websocket write deadline

问题

我无法理解这部分代码,它使用了c.conn.SetWriteDeadline函数。

    // 允许写入消息到对等方的时间。
	writeWait = 10 * time.Second

	// 允许从对等方读取下一个pong消息的时间。
	pongWait = 60 * time.Second

	// 以此间隔向对等方发送ping。必须小于pongWait。
	pingPeriod = (pongWait * 9) / 10

// 为每个连接启动一个运行writePump的goroutine。应用程序通过从此goroutine执行所有写入来确保连接最多只有一个写入器。
func (c *Client) writePump() {
	ticker := time.NewTicker(pingPeriod)
	defer func() {
		ticker.Stop()
		c.conn.Close()
	}()
	for {
		select {
		case message, ok := <-c.send:
            // 写入消息到连接的逻辑
		case <-ticker.C: 
			c.conn.SetWriteDeadline(time.Now().Add(writeWait)) // 设置写入截止时间
			if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
				return
			}
		}
	}
}

我无法理解ticker通道的逻辑。我们在这里试图实现什么?

我参考了go/gorilla/websocket的官方文档这里

英文:

I am unable to understand this part of the code which uses c.conn.SetWriteDeadline function.


    // Time allowed to write a message to the peer.
	writeWait = 10 * time.Second

	// Time allowed to read the next pong message from the peer.
	pongWait = 60 * time.Second

	// Send pings to peer with this period. Must be less than pongWait.
	pingPeriod = (pongWait * 9) / 10

// A goroutine running writePump is started for each connection. The
// application ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (c *Client) writePump() {
	ticker := time.NewTicker(pingPeriod)
	defer func() {
		ticker.Stop()
		c.conn.Close()
	}()
	for {
		select {
		case message, ok := &lt;-c.send:
            // LOGIC TO WRITE MESSAGE TO THE CONNECTION
		case &lt;-ticker.C: 
			c.conn.SetWriteDeadline(time.Now().Add(writeWait)) // &#128072; ??
			if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
				return
			}
		}
	}
}

I am unable to wrap my head around the ticket channel logic. What are we trying to achieve here?

I am referring to the official docks for go/gorilla/websocket here

答案1

得分: 4

从SetWriteDeadline函数的文档中可以得知:

SetWriteDeadline在底层网络连接上设置写入截止时间。
在写入超时后,WebSocket状态会损坏,并且所有后续的写入操作都会返回错误。
t的零值表示写入不会超时。

因此,每个goroutine上的ticker都会将写入截止时间设置为当前时间加上你设置的writeWait常量值。随后的代码行会使用该截止时间发送ping消息。所有这些操作都在ticker间隔(即pingPeriod常量)内发生。另一个goroutine,readPump(),应该设置读取截止时间和pong消息的处理程序。根据你设置的等待时间,这个持续的ping-pong过程可以保持连接的活跃状态。

英文:

From the SetWriteDeadline function documentation:

SetWriteDeadline sets the write deadline on the underlying network connection. 
After a write has timed out, the websocket state is corrupt and
all future writes will return an error. 
A zero value for t means writes will not time out.

So every ticker on the goroutine it is setting the write deadline to the current time plus whatever you have the writeWait const set to. The subsequent line then sends a ping message, with that deadline. This all happens on the ticker interval which is the const pingPeriod. The other go-routine, readPump(), should be setting a read deadline and handler for the pong message. This constant ping-pong keeps the connection alive according to the wait times you have set.

huangapple
  • 本文由 发表于 2022年4月28日 19:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/72042772.html
匿名

发表评论

匿名网友

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

确定