github.com/cloudwego/kitex@v0.9.0/pkg/remote/connpool/short_pool.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package connpool
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net"
    23  
    24  	"github.com/cloudwego/kitex/pkg/remote"
    25  	"github.com/cloudwego/kitex/pkg/utils"
    26  )
    27  
    28  var _ remote.ConnPool = &ShortPool{}
    29  
    30  type shortConn struct {
    31  	net.Conn
    32  	closed bool
    33  }
    34  
    35  // Close closes the connection.
    36  func (sc *shortConn) Close() error {
    37  	if !sc.closed {
    38  		sc.closed = true
    39  		return sc.Conn.Close()
    40  	}
    41  	return nil
    42  }
    43  
    44  // RawConn returns the underlying net.Conn.
    45  func (sc *shortConn) RawConn() net.Conn {
    46  	if sc.closed {
    47  		return nil
    48  	}
    49  	return sc.Conn
    50  }
    51  
    52  // ShortPool is the conn pool for short connections.
    53  type ShortPool struct {
    54  	serviceName string
    55  	reporter    Reporter
    56  }
    57  
    58  // NewShortPool timeout is connection timeout.
    59  func NewShortPool(serviceName string) *ShortPool {
    60  	return &ShortPool{
    61  		serviceName: serviceName,
    62  		reporter:    &DummyReporter{},
    63  	}
    64  }
    65  
    66  // Get return a PoolConn instance which implement net.Conn interface.
    67  func (p *ShortPool) Get(ctx context.Context, network, address string, opt remote.ConnOption) (net.Conn, error) {
    68  	conn, err := opt.Dialer.DialTimeout(network, address, opt.ConnectTimeout)
    69  	addr := utils.NewNetAddr(network, address)
    70  	if err != nil {
    71  		p.reporter.ConnFailed(Short, p.serviceName, addr)
    72  		return nil, fmt.Errorf("dial connection err: %s, addr: %s", err, addr)
    73  	}
    74  	p.reporter.ConnSucceed(Short, p.serviceName, addr)
    75  	return &shortConn{Conn: conn}, nil
    76  }
    77  
    78  func (p *ShortPool) release(conn net.Conn) error {
    79  	return conn.Close()
    80  }
    81  
    82  // Put implements the ConnPool interface.
    83  func (p *ShortPool) Put(conn net.Conn) error {
    84  	return p.release(conn)
    85  }
    86  
    87  // Discard implements the ConnPool interface.
    88  func (p *ShortPool) Discard(conn net.Conn) error {
    89  	return p.release(conn)
    90  }
    91  
    92  // Close is to release resource of ConnPool, it is executed when client is closed.
    93  // ShortPool do nothing
    94  func (p *ShortPool) Close() error {
    95  	return nil
    96  }
    97  
    98  // EnableReporter enable reporter for short connection pool.
    99  func (p *ShortPool) EnableReporter() {
   100  	p.reporter = GetCommonReporter()
   101  }