github.com/cloudwego/hertz@v0.9.3/pkg/app/client/discovery/discovery.go (about)

     1  /*
     2   * Copyright 2022 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 discovery
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  
    23  	"github.com/cloudwego/hertz/pkg/app/server/registry"
    24  	"github.com/cloudwego/hertz/pkg/common/utils"
    25  )
    26  
    27  type TargetInfo struct {
    28  	Host string
    29  	Tags map[string]string
    30  }
    31  
    32  type Resolver interface {
    33  	// Target should return a description for the given target that is suitable for being a key for cache.
    34  	Target(ctx context.Context, target *TargetInfo) string
    35  
    36  	// Resolve returns a list of instances for the given description of a target.
    37  	Resolve(ctx context.Context, desc string) (Result, error)
    38  
    39  	// Name returns the name of the resolver.
    40  	Name() string
    41  }
    42  
    43  // SynthesizedResolver synthesizes a Resolver using a resolve function.
    44  type SynthesizedResolver struct {
    45  	TargetFunc  func(ctx context.Context, target *TargetInfo) string
    46  	ResolveFunc func(ctx context.Context, key string) (Result, error)
    47  	NameFunc    func() string
    48  }
    49  
    50  func (sr SynthesizedResolver) Target(ctx context.Context, target *TargetInfo) string {
    51  	if sr.TargetFunc == nil {
    52  		return ""
    53  	}
    54  	return sr.TargetFunc(ctx, target)
    55  }
    56  
    57  func (sr SynthesizedResolver) Resolve(ctx context.Context, key string) (Result, error) {
    58  	return sr.ResolveFunc(ctx, key)
    59  }
    60  
    61  // Name implements the Resolver interface
    62  func (sr SynthesizedResolver) Name() string {
    63  	if sr.NameFunc == nil {
    64  		return ""
    65  	}
    66  	return sr.NameFunc()
    67  }
    68  
    69  // Instance contains information of an instance from the target service.
    70  type Instance interface {
    71  	Address() net.Addr
    72  	Weight() int
    73  	Tag(key string) (value string, exist bool)
    74  }
    75  
    76  type instance struct {
    77  	addr   net.Addr
    78  	weight int
    79  	tags   map[string]string
    80  }
    81  
    82  func (i *instance) Address() net.Addr {
    83  	return i.addr
    84  }
    85  
    86  func (i *instance) Weight() int {
    87  	if i.weight > 0 {
    88  		return i.weight
    89  	}
    90  	return registry.DefaultWeight
    91  }
    92  
    93  func (i *instance) Tag(key string) (value string, exist bool) {
    94  	value, exist = i.tags[key]
    95  	return
    96  }
    97  
    98  // NewInstance creates an Instance using the given network, address and tags
    99  func NewInstance(network, address string, weight int, tags map[string]string) Instance {
   100  	return &instance{
   101  		addr:   utils.NewNetAddr(network, address),
   102  		weight: weight,
   103  		tags:   tags,
   104  	}
   105  }
   106  
   107  // Result contains the result of service discovery process.
   108  // the instance list can/should be cached and CacheKey can be used to map the instance list in cache.
   109  type Result struct {
   110  	CacheKey  string
   111  	Instances []Instance
   112  }