合理zGolang-使用nil Channels-nil,代码,Golang,Channels,channel常用示例代码片段-入门小站
这里一定要注意,以下问题导致gorounting阻塞
遇到的问题:
gorounting1:
func (c *Client) closeConn(ctx context.Context) context.Context {
defer utils.Recover(ctx)
conn := GetRawConnection(ctx)
if conn != nil {
err := conn.Close()
if err != nil {
Log.Errorf(ctx, "Close preCoon err:%v", err)
}
ctx = ClearRawConnectionInCtx(ctx)
}
connectionInfoID := GetConnectionInfoID(ctx)
if connectionInfoID == c.ConnectionInfoID {
if c.Send != nil {
close(c.Send)
c.Send = nil
c.ConnectionInfoID = ""
}
gorounting2:
func (c *Client) ClientWrite(ctx context.Context, args []chan struct{}, handlerIndex int) error {
ticker := time.NewTicker(time.Duration(config.HartIntervalInSecond) * time.Second)
clientEmptyTicker := time.NewTicker(24 * time.Hour)
c.WriteThreadStarted = true
utils.AsyncRun[chan struct{}](ctx, "clientWriteClose", false, [][]chan struct{}{args},
func(ctx context.Context, args []chan struct{}, handlerIndex int) error {
// 手动关闭
select {
case <-args[0]:
Log.Infof(ctx, "close the connection,maybe game over maybe new connection come")
c.closeConn(ctx)
}
return nil
})
for {
select {
// 这里比hub 发送时候有延迟时间,这段延迟时间是否会对结果产生影响
case commandSendSrc, ok := <-c.Send:
if !ok {
Log.Errorf(ctx, "c.Send is broken")
time.Sleep(time.Duration(config.WaiteBeforeCloseClient) * time.Second)
c.CloseConn(ctx)
return nil
}
if commandSendSrc == nil {
continue
}
// ctx = context.WithValue(ctx, config.TraceID, utils.NewUUID())
commandSend, err := commandSendSrc.DeepCopy(ctx)
if err != nil {