Aetherance commented on code in PR #3086: URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2632072972
########## protocol/triple/client_pool.go: ########## @@ -0,0 +1,312 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package triple + +import ( + "errors" + "sync" + "time" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" + tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol" +) + +type ClientPool interface { + Get(timeout time.Duration) (*tri.Client, error) + Put(client *tri.Client) + Close() + + MaxSize() int + Closed() bool +} + +var ( + ErrTriClientPoolClosed = errors.New("tri client pool is closed") + ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close") +) + +type TriClientPool struct { + clients chan *tri.Client + factory func() *tri.Client + mu sync.Mutex + maxSize int + curSize int + closed bool + getTimeouts int // recent timeout count, used to trigger expansion + lastScaleTime time.Time + + fallback *tri.Client + consecutiveHighIdle int +} + +// NewTriClientPool creates a new TriClientPool with optional warm-up. +// warmUpSize specifies how many clients to pre-create and add to the pool. +// If warmUpSize is 0, no warm-up is performed. +// If warmUpSize > maxSize, it will be capped to maxSize. +func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool { + if warmUpSize > maxSize { + warmUpSize = maxSize + } + if warmUpSize < 0 { + warmUpSize = 0 + } + + pool := &TriClientPool{ + clients: make(chan *tri.Client, maxSize), + factory: factory, + maxSize: maxSize, + curSize: warmUpSize, + } + + for i := 0; i < warmUpSize; i++ { + cli := factory() + select { + case pool.clients <- cli: + default: + pool.curSize-- + } + } + + return pool +} + +// TriClient Get method +// timeout means how long to wait for an available client. +// TriClientPool keeps a fallback client pointer. If Get() times out, +// and the pool cannot expand at that moment, the pool will return fallback. +// This ensures there is at least one usable client. +// Get tries a non-blocking receive first. If that fails, it tries to expand. +// If expansion is not allowed, it waits for a client up to timeout. +// After timeout, if still no client, Get() returns ErrTimeout with fallback. +func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) { + now := time.Now() + if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod { + p.autoScaler() + } + + select { + case cli := <-p.clients: + return cli, nil + default: + } + + p.mu.Lock() + if p.closed { + p.mu.Unlock() + return nil, ErrTriClientPoolClosed + } + // try to expand + 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, ErrTriClientPoolClosed + } + return cli, nil + case <-time.After(timeout): + p.recordTimeout() + p.mu.Lock() + if p.fallback == nil { + p.fallback = p.factory() + } + p.mu.Unlock() + return p.fallback, nil + } +} + +// TriClient Put method +// Put tries to put a tri.Client back into the pool. +// If it fails, Put will drop the client and notify the pool. +// Dropping a client is part of shrinking. +func (p *TriClientPool) Put(c *tri.Client) { + now := time.Now() + if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod { + p.autoScaler() + } + + if c == nil { + return + } + + p.mu.Lock() + closed := p.closed + p.mu.Unlock() + if closed { + return + } + + select { + case p.clients <- c: + default: + p.mu.Lock() + p.curSize-- + p.mu.Unlock() + } Review Comment: 这块看起来逻辑没什么问题 不过确实有很大优化的空间 已按照要求使用 defer 并且优化了代码 -- 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]
