7days-golang icon indicating copy to clipboard operation
7days-golang copied to clipboard

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

Open chang-you-ren8 opened this issue 1 year ago • 0 comments

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
	}
}

chang-you-ren8 avatar Aug 04 '23 07:08 chang-you-ren8