lib9p

Go 9P library.
Log | Files | Refs

commit 8fe6e6254b377abbf4d8fb4be9b43b56da21e204
parent bf6cc12d2755fafe0f3fb4967fc88f04b1ebff4d
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 21 Oct 2023 08:17:47 +0900

delete clientReqPool and add tagPool

Diffstat:
Mclient.go | 11++---------
Mreq.go | 71+++++++++++++++++++++++++++--------------------------------------------
2 files changed, 29 insertions(+), 53 deletions(-)

diff --git a/client.go b/client.go @@ -12,20 +12,13 @@ type Client struct { mSizeLock *sync.Mutex uname string fPool *clientFidPool - rPool *clientReqPool + tPool *tagPool txc chan<- *clientReq errc chan error cancel context.CancelFunc rootFid *clientFid } -type LibErr string - -func (e LibErr) Error() string { return string(e) } -func newLibErrMsg(format string, a ...any) *RError { - return &RError{tag: NOTAG, ename: fmt.Errorf(format, a...)} -} - func NewClient(mSize uint32, uname string, r io.Reader, w io.Writer) *Client { ctx, cancel := context.WithCancel(context.Background()) c := &Client{ @@ -33,7 +26,7 @@ func NewClient(mSize uint32, uname string, r io.Reader, w io.Writer) *Client { mSizeLock: new(sync.Mutex), uname: uname, fPool: allocClientFidPool(), - rPool: newClientReqPool(), + tPool: newTagPool(), errc: make(chan error), cancel: cancel, } diff --git a/req.go b/req.go @@ -73,7 +73,6 @@ func (rp *ReqPool) delete(tag uint16) { type clientReq struct { tag uint16 - pool *clientReqPool tmsg Msg rmsg Msg err error @@ -90,72 +89,56 @@ func newClientReq(ctx context.Context, msg Msg) *clientReq { } } -type clientReqPool struct { - m map[uint16]*clientReq +type tagPool struct { + m map[uint16]bool lock *sync.Mutex } -func newClientReqPool() *clientReqPool { - return &clientReqPool{ - m: make(map[uint16]*clientReq), +func newTagPool() *tagPool { + return &tagPool{ + m: make(map[uint16]bool), lock: new(sync.Mutex), } } -func (rp *clientReqPool) nextTag() (uint16, error) { +func (tp *tagPool) nextTag() (uint16, error) { // TODO: optimize - rp.lock.Lock() - defer rp.lock.Unlock() + tp.lock.Lock() + defer tp.lock.Unlock() for i := uint16(0); i < i+1; i++ { - if _, ok := rp.m[i]; !ok { + if _, ok := tp.m[i]; !ok { return i, nil } } return 0, fmt.Errorf("run out of tag") } -func (rp *clientReqPool) add(ctx context.Context, msg Msg) (*clientReq, error) { - var tag uint16 - if _, ok := msg.(*TVersion); ok { - tag = NOTAG - } else { - var err error - tag, err = rp.nextTag() - if err != nil { - return nil, fmt.Errorf("nextTag: %v", err) - } - } - rp.lock.Lock() - defer rp.lock.Unlock() - - msg.SetTag(tag) - req := &clientReq{ - tag: tag, - pool: rp, - tmsg: msg, - rxc: make(chan *clientReq), - ctxDone: ctx.Done(), +func (tp *tagPool) add(tag uint16) error { + tp.lock.Lock() + defer tp.lock.Unlock() + if _, ok := tp.m[tag]; ok { + return fmt.Errorf("duplicate tag") } - rp.m[tag] = req - return req, nil + tp.m[tag] = true + return nil } -func (rp *clientReqPool) lookup(tag uint16) (*clientReq, bool) { - rp.lock.Lock() - defer rp.lock.Unlock() - r, ok := rp.m[tag] - return r, ok +func (tp *tagPool) lookup(tag uint16) bool { + tp.lock.Lock() + defer tp.lock.Unlock() + _, ok := tp.m[tag] + return ok } -func (rp *clientReqPool) delete(tag uint16) { - rp.lock.Lock() - defer rp.lock.Unlock() - delete(rp.m, tag) +func (tp *tagPool) delete(tag uint16) { + tp.lock.Lock() + defer tp.lock.Unlock() + delete(tp.m, tag) } -func (rp *clientReqPool) String() string { +func (tp *tagPool) String() string { s := "[" - for tag := range rp.m { + for tag := range tp.m { s += fmt.Sprintf("%d ", tag) } if len(s) > 1 {