-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question about BeforeRecv #28
Comments
没太懂你意思,是指 for 循环中每次 read 之前都去调用 BeforeRecv 吗?如果是这个意思,那这个不是bug,对 conn 的处理通常包括两个地方: |
这里再多说一下 keepalive: |
谢谢回复.
|
runtime 里的也是timer,但是估计只是超时后原来 Recv 阻塞的协程里直接失败、这是runtime调度就可控的,不需要额外创建一个协程来执行 Close 之类的,而 time.AfterFunc 在超时后的回调是异步、需要创建一个协程,这个成本是不划算的: 而且退一步讲,既然 TCPConn 已经提供了 SetDeadline 相关,go 这种一个连接一个协程的同步代码的便利性,也不需要time.AfterFunc那么麻烦 |
package main
import (
"log"
"net"
"time"
"github.com/lesismal/arpc"
)
func main() {
svr := arpc.NewServer()
svr.Handler.BeforeRecv(func(c net.Conn) error {
log.Printf("BeforeRecv 调用")
return nil
})
svr.Handler.Handle("/echo/sync", func(ctx *arpc.Context) {
str := ""
ctx.Bind(&str)
ctx.Write(str)
})
go func() {
time.Sleep(time.Second)
client, err := arpc.NewClient(func() (net.Conn, error) {
return net.DialTimeout("tcp", "localhost:8888", time.Second*3)
})
req := "hello"
rsp := ""
err = client.Call("/echo/sync", &req, &rsp, time.Second*500)
if err != nil {
log.Printf("Call /echo/sync failed: %v", err)
} else {
log.Printf("Call /echo/sync Response: \"%v\"", rsp)
}
}()
svr.Run("localhost:8888")
}
/*
2021/12/29 00:49:55.560 [INF] [ARPC SVR] Running On: "127.0.0.1:8888"
2021/12/29 00:49:56.560 [INF] [ARPC CLI] 127.0.0.1:8888 Connected
2021/12/29 00:49:56.560 [INF] [ARPC SVR] 127.0.0.1:34924 Connected
2021/12/29 00:49:56 BeforeRecv 调用
2021/12/29 00:49:56 BeforeRecv 调用
2021/12/29 00:49:56 Call /echo/sync Response: "hello"
*/ 比如上面的代码, 我觉得在recv结束之后, BeforeRecv 被再次调用了吧. Line 722 in c3f8d6d
当这个client.Conn不可读的时候, 这个循环会先调用BeforeRecv, 再被阻塞 |
这有什么问题吗?每次读取完整协议包之前都应该按照当前时间更新deadline,如果只设置一次,比如: setdeadline(15s)
for {
read
} 这样15s后、不管有没有数据过来,读都会失败。 |
逻辑上没问题,demo是日志打印顺序的问题,并不是执行顺序的问题,因为拿到Resp并打印比内部for循环跑的晚。 |
哦哦, 是我想错了, 打扰了 |
新年快乐!:smile: |
个人对BeforeRecv这个接口有点疑问, 这个接口在一次recv过程中会被调用两次, 类似:
会循环两次, 第二次先调用BeforeRecv, 然后读套接字, 这时才被阻塞。个人感觉这是个bug?
The text was updated successfully, but these errors were encountered: