github.com/DerekStrickland/consul@v1.4.5/agent/cache-types/prepared_query.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 PreparedQueryName = "prepared-query"
    12  
    13  // PreparedQuery supports fetching discovering service instances via prepared
    14  // queries.
    15  type PreparedQuery struct {
    16  	RPC RPC
    17  }
    18  
    19  func (c *PreparedQuery) Fetch(opts cache.FetchOptions, req cache.Request) (cache.FetchResult, error) {
    20  	var result cache.FetchResult
    21  
    22  	// The request should be a PreparedQueryExecuteRequest.
    23  	reqReal, ok := req.(*structs.PreparedQueryExecuteRequest)
    24  	if !ok {
    25  		return result, fmt.Errorf(
    26  			"Internal cache failure: request wrong type: %T", req)
    27  	}
    28  
    29  	// Allways allow stale - there's no point in hitting leader if the request is
    30  	// going to be served from cache and endup arbitrarily stale anyway. This
    31  	// allows cached service-discover to automatically read scale across all
    32  	// servers too.
    33  	reqReal.AllowStale = true
    34  
    35  	// Fetch
    36  	var reply structs.PreparedQueryExecuteResponse
    37  	if err := c.RPC.RPC("PreparedQuery.Execute", reqReal, &reply); err != nil {
    38  		return result, err
    39  	}
    40  
    41  	result.Value = &reply
    42  	result.Index = reply.QueryMeta.Index
    43  
    44  	return result, nil
    45  }
    46  
    47  func (c *PreparedQuery) SupportsBlocking() bool {
    48  	// Prepared queries don't support blocking.
    49  	return false
    50  }