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

Optimization: Is it possible to add a profile-based constructor? #24

Closed
yuetchn opened this issue Jan 26, 2024 · 2 comments · Fixed by #25
Closed

Optimization: Is it possible to add a profile-based constructor? #24

yuetchn opened this issue Jan 26, 2024 · 2 comments · Fixed by #25
Assignees

Comments

@yuetchn
Copy link
Contributor

yuetchn commented Jan 26, 2024

When using etcd, user password authentication is used, but in the current version, I don't see Settings for this part of the configuration, so my current solution is to add a new profile constructor.

The following code is shown:

1.New configuration

type WatcherConfig struct {
	Hosts                []string
	Key                  string `json:",default=casbin_watcher"`
	User                 string
	Pass                 string
	DialKeepAliveTimeout time.Duration `json:",default=10"`
	DialTimeout          time.Duration `json:",default=30"`
}

2.Added configuration-based constructors

// NewWatcherWithConfig is a configurable Watcher constructor
func NewWatcherWithConfig(config WatcherConfig) (persist.Watcher, error) {
	w := &Watcher{}
	w.running = true
	w.callback = nil
	w.conf = &config

	// Create the client.
	err := w.createClient()
	if err != nil {
		return nil, err
	}

	// Call the destructor when the object is released.
	runtime.SetFinalizer(w, finalizer)

	go func() {
		_ = w.startWatch()
	}()

	return w, nil
}

3.Adjust the createClient method

func (w *Watcher) createClient() error {
	cfg := client.Config{
		Endpoints: w.endpoints,
		// set timeout per request to fail fast when the target endpoints is unavailable
		DialKeepAliveTimeout: time.Second * 10,
		DialTimeout:          time.Second * 30,
		Password:             w.password,
	}

	if w.conf != nil {
		cfg = client.Config{
			Endpoints: w.conf.Hosts,
			// set timeout per request to fail fast when the target endpoints is unavailable
			DialTimeout:          time.Second * w.conf.DialTimeout,
			DialKeepAliveTimeout: time.Second * w.conf.DialKeepAliveTimeout,
			Username:             w.conf.User,
			Password:             w.conf.Pass,
		}
	}

	c, err := client.New(cfg)
	if err != nil {
		return err
	}
	w.client = c
	return nil
}

@hsluoyz
Copy link
Member

hsluoyz commented Jan 26, 2024

@yuetchn sounds good to me. Can you make a PR to add it?

@yuetchn
Copy link
Contributor Author

yuetchn commented Jan 26, 2024

@yuetchn sounds good to me. Can you make a PR to add it?
the relevant PR has been submitted.
#25

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

Successfully merging a pull request may close this issue.

3 participants