github.com/DerekStrickland/consul@v1.4.5/agent/cache-types/catalog_services.go (about)

     1  package cachetype
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/consul/agent/cache"
     7  	"github.com/hashicorp/consul/agent/structs"
     8  )
     9  
    10  // Recommended name for registration.
    11  const CatalogServicesName = "catalog-services"
    12  
    13  // CatalogServices supports fetching discovering service instances via the
    14  // catalog.
    15  type CatalogServices struct {
    16  	RPC RPC
    17  }
    18  
    19  func (c *CatalogServices) Fetch(opts cache.FetchOptions, req cache.Request) (cache.FetchResult, error) {
    20  	var result cache.FetchResult
    21  
    22  	// The request should be a DCSpecificRequest.
    23  	reqReal, ok := req.(*structs.ServiceSpecificRequest)
    24  	if !ok {
    25  		return result, fmt.Errorf(
    26  			"Internal cache failure: request wrong type: %T", req)
    27  	}
    28  
    29  	// Set the minimum query index to our current index so we block
    30  	reqReal.QueryOptions.MinQueryIndex = opts.MinIndex
    31  	reqReal.QueryOptions.MaxQueryTime = opts.Timeout
    32  
    33  	// Always allow stale - there's no point in hitting leader if the request is
    34  	// going to be served from cache and end up arbitrarily stale anyway. This
    35  	// allows cached service-discover to automatically read scale across all
    36  	// servers too.
    37  	reqReal.AllowStale = true
    38  
    39  	// Fetch
    40  	var reply structs.IndexedServiceNodes
    41  	if err := c.RPC.RPC("Catalog.ServiceNodes", reqReal, &reply); err != nil {
    42  		return result, err
    43  	}
    44  
    45  	result.Value = &reply
    46  	result.Index = reply.QueryMeta.Index
    47  	return result, nil
    48  }
    49  
    50  func (c *CatalogServices) SupportsBlocking() bool {
    51  	return true
    52  }