github.com/cloudwego/kitex@v0.9.0/pkg/remote/connpool/reporter.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  	"net"
    21  	"sync"
    22  )
    23  
    24  var (
    25  	isSet          bool
    26  	rwLock         sync.RWMutex
    27  	commonReporter Reporter = &DummyReporter{}
    28  )
    29  
    30  // ConnectionPoolType is the type of connection pool.
    31  type ConnectionPoolType int8
    32  
    33  // Short and Long flags define which indicates type of connection pool..
    34  const (
    35  	Short ConnectionPoolType = iota
    36  	Long
    37  )
    38  
    39  // Reporter report status of connection pool.
    40  type Reporter interface {
    41  	ConnSucceed(poolType ConnectionPoolType, serviceName string, addr net.Addr)
    42  	ConnFailed(poolType ConnectionPoolType, serviceName string, addr net.Addr)
    43  	ReuseSucceed(poolType ConnectionPoolType, serviceName string, addr net.Addr)
    44  }
    45  
    46  // SetReporter set the common reporter of connection pool, that can only be set once.
    47  func SetReporter(r Reporter) {
    48  	rwLock.Lock()
    49  	defer rwLock.Unlock()
    50  	if !isSet {
    51  		isSet = true
    52  		commonReporter = r
    53  	}
    54  }
    55  
    56  // GetCommonReporter returns the current Reporter used by connection pools.
    57  func GetCommonReporter() Reporter {
    58  	rwLock.RLock()
    59  	defer rwLock.RUnlock()
    60  	return commonReporter
    61  }