Skip to content

Commit

Permalink
Change default behavior of Add to wait for the resource to be ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisuke Maki committed Oct 2, 2024
1 parent 2d1b7c6 commit 03d7210
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
27 changes: 18 additions & 9 deletions client_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,26 @@ func ExampleClient() {
return
}

// Add the resource to the controller, so that it starts fetching
ctrl.Add(ctx, r)
// Add the resource to the controller, so that it starts fetching.
// By default, a call to `Add()` will block until the first fetch
// succeeds, but you can skip this if you specify the `WithWaitReady(false)`
// option.
ctrl.Add(ctx, r, httprc.WithWaitReady(false))

{
tctx, tcancel := context.WithTimeout(ctx, time.Second)
defer tcancel()
if err := r.Ready(tctx); err != nil {
fmt.Println(err.Error())
return
// if you specified `httprc.WithWaitReady(false)` option, the fetch will happen
// "soon", but you're not guaranteed that it will happen before the next
// call to `Lookup()`. If you want to make sure that the resource is ready,
// you can call `Ready()` like so:
/*
{
tctx, tcancel := context.WithTimeout(ctx, time.Second)
defer tcancel()
if err := r.Ready(tctx); err != nil {
fmt.Println(err.Error())
return
}
}
}
*/
m := r.Resource()
fmt.Println(m.Hello)
// OUTPUT:
Expand Down
24 changes: 22 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Controller interface {
// Add adds a new `http.Resource` to the controller. If the resource already exists,
// it will return an error.
Add(context.Context, Resource) error
Add(context.Context, Resource, ...AddOption) error

// Lookup a `httprc.Resource` by its URL. If the resource does not exist, it
// will return an error.
Expand Down Expand Up @@ -127,7 +127,21 @@ func (c *controller) Lookup(ctx context.Context, u string) (Resource, error) {

// Add adds a new resource to the controller. If the resource already
// exists, it will return an error.
func (c *controller) Add(ctx context.Context, r Resource) error {
//
// By default this function will automatically wait for the resource to be
// fetched once (by calling `r.Ready()`). Note that the `r.Ready()` call will NOT
// timeout unless you configure your context object with `context.WithTimeout`.
// To disable waiting, you can specify the `WithWaitReady(false)` option.
func (c *controller) Add(ctx context.Context, r Resource, options ...AddOption) error {
var waitReady bool
//nolint:forcetypeassert
for _, option := range options {
switch option.Ident() {
case identWaitReady{}:
waitReady = option.(newAddOption).Value().(bool)
}
}

if !c.wl.IsAllowed(r.URL()) {
return fmt.Errorf(`httprc.Controller.AddResource: cannot add %q: %w`, r.URL(), errBlockedByWhitelist)
}
Expand All @@ -140,6 +154,12 @@ func (c *controller) Add(ctx context.Context, r Resource) error {
if _, err := sendBackend[addRequest, struct{}](ctx, c.incoming, req, reply); err != nil {
return err
}

if waitReady {
if err := r.Ready(ctx); err != nil {
return err
}
}
return nil
}

Expand Down
21 changes: 21 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,24 @@ type identConstantInterval struct{}
func WithConstantInterval(d time.Duration) NewResourceOption {
return newResourceOption{option.New(identConstantInterval{}, d)}
}

type AddOption interface {
option.Interface
newAddOption()
}

type newAddOption struct {
option.Interface
}

func (newAddOption) newAddOption() {}

type identWaitReady struct{}

// WithWaitReady specifies whether the client should wait for the resource to be
// ready before returning from the Add method.
//
// By default, the client will wait for the resource to be ready before returning.
func WithWaitReady(b bool) AddOption {
return newAddOption{option.New(identWaitReady{}, b)}
}

0 comments on commit 03d7210

Please sign in to comment.