Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the Consul API SDK support custom QueryOptions #20065

Open
shenqidebaozi opened this issue Dec 28, 2023 · 4 comments
Open

Can the Consul API SDK support custom QueryOptions #20065

shenqidebaozi opened this issue Dec 28, 2023 · 4 comments
Assignees

Comments

@shenqidebaozi
Copy link

shenqidebaozi commented Dec 28, 2023

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

type Plan struct {

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
}

func makeQueryOptionsWithContext(p *Plan, stale bool) consulapi.QueryOptions {

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
Copy link

shcw commented Jan 4, 2024

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

@huikang
Copy link
Collaborator

huikang commented Jan 4, 2024

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

@shenqidebaozi
Copy link
Author

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

@shenqidebaozi
Copy link
Author

shenqidebaozi commented Jan 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants