hashicorp / consul

Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure.
https://www.consul.io
Other
28.39k stars 4.43k forks source link

Can the Consul API SDK support custom QueryOptions #20065

Open shenqidebaozi opened 10 months ago

shenqidebaozi commented 10 months ago

Feature Description

When I use the Watch package in the Consul API, I want to listen for instance update events of the peer cluster through Watch, but it does not support specifying the peer parameter, so I cannot listen for instance information of the peer cluster.

    parse, err := watch.Parse(map[string]interface{}{
        "type":        "service",
        "service":     "xxxxx",
        "passingonly": true,
        "peer":        "xxxxxx",
    })
    if err != nil {
        t.Fatal(err)
    }

    parse.HybridHandler = func(val watch.BlockingParamVal, i interface{}) {
        s := i.([]*api.ServiceEntry)
        for _, entry := range s {
            fmt.Println(entry.Service.Address, entry.Service.Port, entry.Service.Meta)
        }
    }

    parse.RunWithClientAndHclog(consul, hclog.L())
Invalid parameters: [peer]

Use Case(s)

I would like to add a configuration for QueryOptions in the plan to specify the default QueryOptions https://github.com/hashicorp/consul/blob/2f335113f885b0b0304ac1ae57ffcacfa5818b32/api/watch/watch.go#L24

type Plan struct {
    Datacenter  string
    Token       string
    Type        string
    HandlerType string
    Exempt      map[string]interface{}

    Watcher WatcherFunc
    // Handler is kept for backward compatibility but only supports watches based
    // on index param. To support hash based watches, set HybridHandler instead.
    Handler       HandlerFunc
    HybridHandler HybridHandlerFunc

    Logger hclog.Logger
    // Deprecated: use Logger
    LogOutput io.Writer

    address      string
    client       *consulapi.Client
    lastParamVal BlockingParamVal
    lastResult   interface{}
        QueryOtions  *consuapi.QueryOptions // used to specify default QueryOptions

    stop       bool
    stopCh     chan struct{}
    stopLock   sync.Mutex
    cancelFunc context.CancelFunc
}

https://github.com/hashicorp/consul/blob/2f335113f885b0b0304ac1ae57ffcacfa5818b32/api/watch/funcs.go#L340

func makeQueryOptionsWithContext(p *Plan, stale bool) consulapi.QueryOptions {
    ctx, cancel := context.WithCancel(context.Background())
    p.setCancelFunc(cancel)
    opts := p.QueryOtions
        opts.AllowStale = stale
    switch param := p.lastParamVal.(type) {
    case WaitIndexVal:
        opts.WaitIndex = uint64(param)
    case WaitHashVal:
        opts.WaitHash = string(param)
    }
    return *opts.WithContext(ctx)
}

If we think this is meaningful, please assign it to me

shcw commented 10 months ago

非常有用 大佬请做个demo 在kratos的example中

huikang commented 10 months ago

@shenqidebaozi , thanks for the proposal. Do you mind elaborating on how the QueryOptions can solve the peering parameter in your example?

shenqidebaozi commented 10 months ago

@huikang Of course, I will create a PR to implement this feature

shenqidebaozi commented 10 months ago

@huikang I have submitted a PR,through this feature, users can specify QueryOptions themselves without adding various watch parameters