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

Expose DialStrategy function to user for custom connection routing #855

Merged

Conversation

jkaflik
Copy link
Contributor

@jkaflik jkaflik commented Dec 16, 2022

Expose a way to have a customized way how connections created: (the result of dial function)

opts.DialStrategy = func(ctx context.Context, connID int, opts *clickhouse.Options, dial clickhouse.Dial) (clickhouse.DialResult, error) {
	return dial(ctx, "server.host:5678", opts)
}

Default DialStrategy has following implementation clickhouse.DefaultDialStrategy:

func DefaultDialStrategy(ctx context.Context, connID int, opt *Options, dial Dial) (r DialResult, err error) {
	for i := range opt.Addr {
		var num int
		switch opt.ConnOpenStrategy {
		case ConnOpenInOrder:
			num = i
		case ConnOpenRoundRobin:
			num = (int(connID) + i) % len(opt.Addr)
		}

		if r, err = dial(ctx, opt.Addr[num], opt); err == nil {
			return r, nil
		}
	}

	if err == nil {
		err = ErrAcquireConnNoAddress
	}

	return r, err
}

clickhouse.DialStrategy function receives a clickhouse.Dial function that needs to be triggered by the user if a new connection is needed. It returns a dedicated DialResult that wraps over internal and coupled connect struct.
It's a hacky solution, as connection and pool management are highly coupled in the library. Until we find a better design in v3, we need to have a trade-off.

Resolves #772

@jkaflik jkaflik closed this Dec 23, 2022
@jkaflik jkaflik reopened this Dec 23, 2022
@jkaflik jkaflik marked this pull request as ready for review December 23, 2022 17:55
@jkaflik jkaflik requested a review from gingerwizard December 23, 2022 17:55
@jkaflik jkaflik self-assigned this Dec 23, 2022
@jkaflik jkaflik merged commit 197b589 into main Jan 10, 2023
@jkaflik jkaflik deleted the 772-Expose-acquire-function-to-user-for-custom-connection-routing branch May 5, 2023 16:18
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

Successfully merging this pull request may close these issues.

Expose acquire function to user for custom connection routing
1 participant