模拟阻塞操作
(request ziface.IRequest)
| 25 | |
| 26 | // 模拟阻塞操作 |
| 27 | func (r *blockRouter) Handle(request ziface.IRequest) { |
| 28 | //read client data |
| 29 | zlog.Infof("recv from client:%s, msgId=%d, data=%s\n", request.GetConnection().RemoteAddr(), request.GetMsgID(), string(request.GetData())) |
| 30 | |
| 31 | // 第一次处理时,模拟任务阻塞操作, Hash 模式下,后面的连接的任务得不到处理 |
| 32 | // DynamicBind 模式下,看后面的连接的任务会得到即使处理,不会因为前面连接的任务阻塞而得不到处理 |
| 33 | // 这里只模拟一次阻塞操作。 |
| 34 | if atomic.CompareAndSwapInt32(&Block, 1, 0) { |
| 35 | zlog.Infof("blockRouter handle start, msgId=%d, remote:%v\n", request.GetMsgID(), request.GetConnection().RemoteAddr()) |
| 36 | time.Sleep(time.Second * 10) |
| 37 | //阻塞操作结束 |
| 38 | zlog.Infof("blockRouter handle end, msgId=%d, remote:%v\n", request.GetMsgID(), request.GetConnection().RemoteAddr()) |
| 39 | } |
| 40 | |
| 41 | err := request.GetConnection().SendMsg(2, []byte("pong from server")) |
| 42 | if err != nil { |
| 43 | zlog.Error(err) |
| 44 | return |
| 45 | } |
| 46 | zlog.Infof("send pong over, client:%s\n", request.GetConnection().RemoteAddr()) |
| 47 | } |
| 48 | |
| 49 | func main() { |
| 50 | s := znet.NewServer() |
nothing calls this directly
no test coverage detected