lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit 33cded2928328a4d8c53e6b776947473af316d95
parent e8bd1e3c76ceab722f1be69198c3789443da13ed
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Mon, 25 Dec 2023 12:10:30 +0900

change the flow of context

Diffstat:
Mserver.go | 13++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/server.go b/server.go @@ -119,7 +119,7 @@ func (s *Server) runListener(ctx context.Context) { defer close(rc) for { select { - case rc <- s.getReq(): + case rc <- s.getReq(ctx): case <-ctx.Done(): return } @@ -157,7 +157,8 @@ func (s *Server) runSpeaker(ctx context.Context) { // Any error it encounters is embedded into the Req struct. // This function is called only by the server's listener goroutine, // and does not need to lock r. -func (s *Server)getReq() *Req { +// The argument ctx is used to set the request's context. +func (s *Server)getReq(ctx context.Context) *Req { ifcall, err := RecvMsg(s.r) if err != nil { if err == io.EOF { @@ -165,6 +166,7 @@ func (s *Server)getReq() *Req { } return &Req{listenErr: fmt.Errorf("readMsg: %v", err)} } + ctx, cancel := context.WithCancel(ctx) req, err := s.rPool.add(ifcall.GetTag()) if err != nil { // duplicate tag: cons up a fake Req @@ -172,6 +174,8 @@ func (s *Server)getReq() *Req { req.Srv = s req.Ifcall = ifcall req.listenErr = ErrDupTag + req.Cancel = cancel + req.Done = ctx.Done() if s.chatty9P { fmt.Fprintf(os.Stderr, "<-- %v\n", req.Ifcall) } @@ -180,6 +184,8 @@ func (s *Server)getReq() *Req { req.Srv = s req.Tag = ifcall.GetTag() req.Ifcall = ifcall + req.Cancel = cancel + req.Done = ctx.Done() if ifcall, ok := req.Ifcall.(*TFlush); ok { req.Oldreq, _ = req.Srv.rPool.lookup(ifcall.Oldtag) } @@ -1372,9 +1378,6 @@ L: } continue L } - ctx1, cancel := context.WithCancel(ctx) - r.Cancel = cancel - r.Done = ctx1.Done() go func() { switch r.Ifcall.(type) { default: