hashicorp / hashicorp/consul

Can the Consul API SDK support custom QueryOptions

Open
#20,065 4 comments 0 reactions 1 assignee Claimed by @shenqidebaozi View on GitHub
Dominant language
Go
Stars
30.1k
Forks
4.6k
Avg merge
1d 18h
Merged PRs (30d)
39

Description

#### 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.

```go
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())
```
```bash
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
```go
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.