trpc.group/trpc-go/trpc-go@v1.0.3/naming/selector/selector.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  // Package selector determines how client chooses a backend node by service name. It contains service
    15  // discovery, load balance and circuit breaker.
    16  package selector
    17  
    18  import (
    19  	"time"
    20  
    21  	"trpc.group/trpc-go/trpc-go/naming/registry"
    22  )
    23  
    24  // Selector is the interface that defines the selector.
    25  type Selector interface {
    26  	// Select gets a backend node by service name.
    27  	Select(serviceName string, opt ...Option) (*registry.Node, error)
    28  	// Report reports request status.
    29  	Report(node *registry.Node, cost time.Duration, err error) error
    30  }
    31  
    32  var (
    33  	selectors = make(map[string]Selector)
    34  )
    35  
    36  // Register registers a named Selector.
    37  func Register(name string, s Selector) {
    38  	selectors[name] = s
    39  }
    40  
    41  // Get gets a named Selector.
    42  func Get(name string) Selector {
    43  	s := selectors[name]
    44  	return s
    45  }
    46  
    47  func unregisterForTesting(name string) {
    48  	delete(selectors, name)
    49  }