commit e98ed8795fadc0e594d8b7b2fdec5b88cb9ffa13
parent 4c24389b9733f6966e66fb863329f5c9e4e4c2bf
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 7 Jan 2024 09:58:06 +0900
unuse context in transact
Diffstat:
3 files changed, 2 insertions(+), 54 deletions(-)
diff --git a/client/client.go b/client/client.go
@@ -235,16 +235,6 @@ func (c *Client) runMultiplexer(ctx context.Context, tmsgc chan<- lib9p.Msg, rms
// Tmsg
go func() {
defer func() {
- L:
- for {
- select {
- case r := <-txc:
- r.errc <- errors.New("client stopped")
- default:
- break L
- }
- }
- c.rPool.cancelAll(errors.New("client stopped"))
close(tmsgc)
c.wg.Done()
}()
@@ -271,11 +261,9 @@ func (c *Client) runMultiplexer(ctx context.Context, tmsgc chan<- lib9p.Msg, rms
// Transact send 9P lib9p.Msg of r to the multiplexer goroutines and recieves
// the reply.
-func (c *Client) transact(ctx context.Context, tmsg lib9p.Msg) (lib9p.Msg, error) {
+func (c *Client) transact(_ context.Context, tmsg lib9p.Msg) (lib9p.Msg, error) {
r := newReq(tmsg)
select {
- case <-ctx.Done():
- return nil, ctx.Err()
case c.txc <- r:
case <-c.done:
return nil, errors.New("client stopped")
@@ -285,10 +273,8 @@ func (c *Client) transact(ctx context.Context, tmsg lib9p.Msg) (lib9p.Msg, error
return r.rmsg, r.err
case err := <-r.errc: // Client side error.
return nil, err
- case <-ctx.Done():
- return nil, ctx.Err()
case <-c.done:
- return nil, <-r.errc
+ return nil, errors.New("client stopped")
}
}
diff --git a/client/client_test.go b/client/client_test.go
@@ -36,43 +36,8 @@ func TestClientCancel(t *testing.T) {
}
cancel()
wg.Wait()
- if len(c.rPool.m) != 0 {
- t.Errorf("req pool clogged: %v", c.rPool)
- }
}
-// TestReqCancel checks if the function transact cancels outstanding transactions
-// propperly when the requests are canceled.
-func TestReqCancel(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- ctx0, cancel0 := context.WithCancel(context.Background())
- cr, _ := io.Pipe()
- sr, cw := io.Pipe()
- defer func() { cr.Close(); sr.Close() }()
- c := NewClient(ctx, 1024, "", cr, cw)
- wg := new(sync.WaitGroup)
- wg.Add(10)
- for i := 0; i < 10; i++ {
- go func(i int) {
- defer wg.Done()
- c.Version(ctx0, uint16(i), 1024, "9P2000")
- }(i)
- }
- for i := 0; i < 5; i++ {
- _, err := lib9p.RecvMsg(sr)
- if err != nil {
- t.Fatal(err)
- }
- }
- cancel0()
- wg.Wait()
- if len(c.rPool.m) != 0 {
- t.Errorf("req pool clogged: %v", c.rPool)
- }
-}
-
-
func setupClientAndServer(fs lib9p.FS) (*Client, context.CancelFunc) {
cr, sw := io.Pipe()
sr, cw := io.Pipe()
diff --git a/client/req.go b/client/req.go
@@ -15,8 +15,6 @@ type req struct {
err error
errc chan error // To report any client side error to transact().
rxc chan *req
- // used to notify client goroutines that the request is canceled.
- done chan struct{}
}
// newReq allocates a req with msg.
@@ -26,7 +24,6 @@ func newReq(msg lib9p.Msg) *req {
tmsg: msg,
rxc: make(chan *req),
errc: make(chan error),
- done: make(chan struct{}),
}
}