CAICAIIs commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2554667934
##########
protocol/triple/client.go:
##########
@@ -431,3 +426,121 @@ func (dt *dualTransport) RoundTrip(req *http.Request)
(*http.Response, error) {
return resp, nil
}
+
+var (
+ ErrPoolClosed = errors.New("tri client pool is closed")
+ ErrTimeout = errors.New("tri client pool get timeout")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+}
+
+func NewTriClientPool(maxSize int, factory func() *tri.Client) *TriClientPool {
+ triClientPool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ maxSize: maxSize,
+ factory: factory,
+ }
+ return triClientPool
+}
+
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ // Try to get a client from the pool immediately
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ // pool is empty continue to check if can create a new client
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrPoolClosed // pool closed
+ }
+
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrPoolClosed
+ } else {
+ p.mu.Unlock()
+ return nil, ErrTimeout
+ }
+ }
+}
+
+func (p *TriClientPool) Put(c *tri.Client) {
Review Comment:
Please add comments to some important functions/methods.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]