-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: add options pattern #276
Conversation
denopink
commented
Jan 23, 2025
•
edited
Loading
edited
- client options
- get logger
- store base url
- store api path
- http client
- store bucket
- listener options
- set and get logger
- pull interval
- listeners (called at each pull interval)
- allow users to define their own db client instead of using argus
chrysom/fx.go
Outdated
// GetLogger returns a logger from the given context. | ||
type GetLogger func(context.Context) *zap.Logger | ||
|
||
// SetLogger embeds the `Listener.logger` in outgoing request contexts for `Listener.Update` calls. | ||
type SetLogger func(context.Context, *zap.Logger) context.Context | ||
|
||
type BasicClientIn struct { | ||
fx.In | ||
|
||
// Ancla Client config. | ||
Config BasicClientConfig | ||
// GetLogger returns a logger from the given context. | ||
GetLogger GetLogger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most code was moved to ancla/chrysom/listener/fx.go
while the ProvideBasicClient
related code was moved to ancla/fx.go
type Items []model.Item | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better home for Items
@@ -95,7 +92,7 @@ func (s *service) GetAll(ctx context.Context) ([]InternalWebhook, error) { | |||
} | |||
|
|||
// NewService returns an ancla client used to interact with an Argus database. | |||
func NewService(client *chrysom.BasicClient) *service { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the chrysom.PushReader
interface instead
// BasicClientConfig is the configuration for the Argus database client. | ||
BasicClientConfig chrysom.BasicClientConfig | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using client.Options
instead
fe581d2
to
180d7f6
Compare
type BasicClientConfig struct { | ||
// Address is the Argus URL (i.e. https://example-argus.io:8090) | ||
Address string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced by clientOptions
|
||
func validateBasicConfig(config *BasicClientConfig) error { | ||
if config.Address == "" { | ||
return ErrAddressEmpty | ||
} | ||
|
||
if config.Bucket == "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced by clientOptions' validation
chrysom/basicClientOptions.go
Outdated
url, err := url.JoinPath(c.storeBaseURL, c.storeAPIPath) | ||
if err != nil { | ||
return errors.Join(err, ErrMisconfiguredClient) | ||
} | ||
|
||
c.storeBaseURL = url |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
url.JoinPath
is safer than using fmt.Sprintf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi, validateStoreBaseURL is called after all options are applied
} | ||
|
||
return client, nil | ||
} | ||
|
||
func ProvideDefaultListenerReader(client *BasicClient) Reader { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed to ProvideReaderOption
type observerConfig struct { | ||
listener Listener |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary struct
} | ||
|
||
// Start begins listening for updates on an interval given that client configuration | ||
// is setup correctly. If a listener process is already in progress, calling Start() | ||
// is a NoOp. If you want to restart the current listener process, call Stop() first. | ||
func (c *ListenerClient) Start(ctx context.Context) error { | ||
logger := c.getLogger(ctx) | ||
if c.observer == nil || c.observer.listener == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.observer
struct was removed
@@ -22,7 +24,7 @@ type Pusher interface { | |||
RemoveItem(ctx context.Context, id, owner string) (model.Item, error) | |||
} | |||
|
|||
type Listener interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed to ListenerInterface
because it collided with the listener option Listener
} | ||
|
||
// ProvideBasicClient provides a new BasicClient. | ||
func ProvideBasicClient(in BasicClientIn) (*BasicClient, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code moved into ProvideService
, allowing users to define their own PushReader
client instead of using the default argus db client.
fx.go
Outdated
// ProvideService provides the Argus client service from the given configuration and client options. | ||
func ProvideService(in ServiceIn) (ProvideServiceOut, error) { | ||
// If the user provides a non-nil chrysom.PushReader (their own db client), then use that instead | ||
// of an Argus db client. | ||
// Otherwise, create and use a new Argus db client. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved the logic from ProvideBasicClient
into here, allowing users to define their own PushReader
client instead of using the default argus db client.
180d7f6
to
e8c26d7
Compare
- client options - get logger - store base url - store api path - http client - store bucket - listener options - set and get logger - pull interval - listeners (called at each pull interval) - allow users to define their own db client instead of using argus
e8c26d7
to
3e2d082
Compare
Client ListenerClient | ||
} | ||
|
||
tcs := []testCase{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests seem a bit overly explicit. I'd probably favor just passing an array of options and confirming the New(opts...) returns an error or not. That makes the code less brittle later.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #276 +/- ##
==========================================
+ Coverage 80.83% 83.83% +2.99%
==========================================
Files 20 23 +3
Lines 793 903 +110
==========================================
+ Hits 641 757 +116
+ Misses 134 131 -3
+ Partials 18 15 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
||
func (f DecoratorFunc) Decorate(ctx context.Context, req *http.Request) error { return f(ctx, req) } | ||
|
||
var Nop = DecoratorFunc(func(context.Context, *http.Request) error { return nil }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made Nop
public since it's used by our tests and I think it'll be useful for clients when they want to explicitly indicate their ancla client is not configured for auth.
@@ -19,7 +19,6 @@ func Provide() fx.Option { | |||
ancla.ProvideListener, | |||
ancla.ProvideDefaultListenerWatchers, | |||
chrysom.ProvideBasicClient, | |||
chrysom.ProvideDefaultListenerReader, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ancla will solely use the argus client until we discover a use case for swappable db clients