Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

geerpc day4服务端超时处理bug一种可能的解决方案 #73

Open
chang-you-ren8 opened this issue Aug 4, 2023 · 0 comments
Open

Comments

@chang-you-ren8
Copy link

func (server *Server) handleRequest(cc codec.Codec, req *request, sendMutex *sync.Mutex, wg *sync.WaitGroup, timeout time.Duration) {
	defer wg.Done()

	finished := make(chan struct{})
	var timeoutFlag int32

	go func() {
		defer func() {
			close(finished)
		}()
		err := req.svc.call(req.mtype, req.argv, req.replyv)
		// 执行出错
		if err != nil {
			req.h.Error = err.Error()
			server.sendResponse(cc, req.h, invalidRequest, sendMutex)
			return
		}
		// 超时
		if atomic.LoadInt32(&timeoutFlag) == 1 {
			req.h.Error = "server handle timeout"
			server.sendResponse(cc, req.h, invalidRequest, sendMutex)
			return
		}
		server.sendResponse(cc, req.h, req.replyv.Interface(), sendMutex)
	}()

	// 没有超时控制
	if timeout == 0 {
		<-finished
		return
	}

	// 有超时控制
	select {
	case <-time.After(timeout):
		atomic.StoreInt32(&timeoutFlag, 1)
		return
	case <-finished:
		return
	}
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant