Skip to content

Commit

Permalink
NewCall => BuildCall
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 20, 2024
1 parent 5c0638d commit 6e268d0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
20 changes: 10 additions & 10 deletions coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ func (c *Coroutine[I, O]) Run(ctx context.Context, req dispatchproto.Request) di
return yield.With(dispatchproto.CoroutineState(state))
}

// NewCall creates a Call for the function.
func (f *Coroutine[I, O]) NewCall(input I, opts ...dispatchproto.CallOption) (dispatchproto.Call, error) {
// BuildCall creates (but does not dispatch) a Call for the function.
func (f *Coroutine[I, O]) BuildCall(input I, opts ...dispatchproto.CallOption) (dispatchproto.Call, error) {
boxedInput, err := dispatchproto.NewAny(input)
if err != nil {
return dispatchproto.Call{}, fmt.Errorf("cannot serialize input: %v", err)
}
return f.PrimitiveFunction.NewCall(boxedInput, opts...)
return f.PrimitiveFunction.BuildCall(boxedInput, opts...)
}

// Dispatch dispatches a Call to the function.
func (f *Coroutine[I, O]) Dispatch(ctx context.Context, input I, opts ...dispatchproto.CallOption) (dispatchproto.ID, error) {
call, err := f.NewCall(input, opts...)
boxedInput, err := dispatchproto.NewAny(input)
if err != nil {
return "", err
return "", fmt.Errorf("cannot serialize input: %v", err)
}
return f.dispatchCall(ctx, call)
return f.PrimitiveFunction.Dispatch(ctx, boxedInput, opts...)
}

func (c *Coroutine[I, O]) setUp(req dispatchproto.Request) (coroutineID, dispatchcoro.Coroutine, error) {
Expand Down Expand Up @@ -241,7 +241,7 @@ func (c *Coroutine[I, O]) entrypoint(input I) func() dispatchproto.Response {
func (c *Coroutine[I, O]) Await(input I, opts ...dispatchproto.CallOption) (O, error) {
var output O

call, err := c.NewCall(input, opts...)
call, err := c.BuildCall(input, opts...)
if err != nil {
return output, err
}
Expand All @@ -258,7 +258,7 @@ func (c *Coroutine[I, O]) Await(input I, opts ...dispatchproto.CallOption) (O, e
func (c *Coroutine[I, O]) Gather(inputs []I, opts ...dispatchproto.CallOption) ([]O, error) {
calls := make([]dispatchproto.Call, len(inputs))
for i, input := range inputs {
call, err := c.NewCall(input, opts...)
call, err := c.BuildCall(input, opts...)
if err != nil {
return nil, err
}
Expand All @@ -271,7 +271,7 @@ func (c *Coroutine[I, O]) Gather(inputs []I, opts ...dispatchproto.CallOption) (
//
// Await should only be called within a Dispatch coroutine (created via Func).
func (f *PrimitiveFunction) Await(input dispatchproto.Any, opts ...dispatchproto.CallOption) (dispatchproto.Any, error) {
call, err := f.NewCall(input, opts...)
call, err := f.BuildCall(input, opts...)
if err != nil {
return dispatchproto.Any{}, err
}
Expand All @@ -295,7 +295,7 @@ func (f *PrimitiveFunction) Await(input dispatchproto.Any, opts ...dispatchproto
func (f *PrimitiveFunction) Gather(inputs []dispatchproto.Any, opts ...dispatchproto.CallOption) ([]dispatchproto.Any, error) {
calls := make([]dispatchproto.Call, len(inputs))
for i, input := range inputs {
call, err := f.NewCall(input, opts...)
call, err := f.BuildCall(input, opts...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ func TestDispatchCallsBatch(t *testing.T) {
endpoint.Register(fn1)
endpoint.Register(fn2)

call1, err := fn1.NewCall(dispatchproto.Int(11), dispatchproto.Expiration(10*time.Second))
call1, err := fn1.BuildCall(dispatchproto.Int(11), dispatchproto.Expiration(10*time.Second))
if err != nil {
t.Fatal(err)
}
call2, err := fn2.NewCall("foo", dispatchproto.Version("xyzzy"))
call2, err := fn2.BuildCall("foo", dispatchproto.Version("xyzzy"))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion dispatchtest/integration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func run() error {
functions.Register(stringify, double, doubleAndRepeat)
defer functions.Close()

call, err := doubleAndRepeat.NewCall(4)
call, err := doubleAndRepeat.BuildCall(4)
if err != nil {
return fmt.Errorf("new call failed: %v", err)
}
Expand Down
10 changes: 3 additions & 7 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (f *PrimitiveFunction) bind(endpoint *Dispatch) {
f.endpoint = endpoint
}

// NewCall creates a Call for the function.
func (f *PrimitiveFunction) NewCall(input dispatchproto.Any, opts ...dispatchproto.CallOption) (dispatchproto.Call, error) {
// BuildCall creates (but does not dispatch) a Call for the function.
func (f *PrimitiveFunction) BuildCall(input dispatchproto.Any, opts ...dispatchproto.CallOption) (dispatchproto.Call, error) {
var url string
if f.endpoint != nil {
url = f.endpoint.URL()
Expand All @@ -132,14 +132,10 @@ func (f *PrimitiveFunction) NewCall(input dispatchproto.Any, opts ...dispatchpro

// Dispatch dispatches a call to the function.
func (f *PrimitiveFunction) Dispatch(ctx context.Context, input dispatchproto.Any, opts ...dispatchproto.CallOption) (dispatchproto.ID, error) {
call, err := f.NewCall(input, opts...)
call, err := f.BuildCall(input, opts...)
if err != nil {
return "", err
}
return f.dispatchCall(ctx, call)
}

func (f *PrimitiveFunction) dispatchCall(ctx context.Context, call dispatchproto.Call) (dispatchproto.ID, error) {
if f.endpoint == nil {
return "", fmt.Errorf("cannot dispatch function call: function has not been registered with a Dispatch endpoint")
}
Expand Down
14 changes: 6 additions & 8 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestFunctionRunError(t *testing.T) {
return "", errors.New("oops")
})

call, err := fn.NewCall("hello")
call, err := fn.BuildCall("hello")
if err != nil {
t.Fatal(err)
}
Expand All @@ -36,7 +36,7 @@ func TestFunctionRunResult(t *testing.T) {
return "world", nil
})

call, err := fn.NewCall("hello")
call, err := fn.BuildCall("hello")
if err != nil {
t.Fatal(err)
}
Expand All @@ -59,7 +59,7 @@ func TestPrimitiveFunctionNewCallAndDispatchWithoutEndpoint(t *testing.T) {
panic("not implemented")
})

_, err := fn.NewCall(dispatchproto.String("bar")) // allowed
_, err := fn.BuildCall(dispatchproto.String("bar")) // allowed
if err != nil {
t.Fatal(err)
}
Expand All @@ -74,7 +74,7 @@ func TestFunctionNewCallAndDispatchWithoutEndpoint(t *testing.T) {
panic("not implemented")
})

_, err := fn.NewCall("bar") // allowed
_, err := fn.BuildCall("bar") // allowed
if err != nil {
t.Fatal(err)
}
Expand All @@ -98,8 +98,7 @@ func TestPrimitiveFunctionDispatchWithoutClient(t *testing.T) {
})
endpoint.Register(fn)

// It's possible to create a call since an endpoint URL is available.
if _, err := fn.NewCall(dispatchproto.String("bar")); err != nil {
if _, err := fn.BuildCall(dispatchproto.String("bar")); err != nil { // allowed
t.Fatal(err)
}

Expand All @@ -126,8 +125,7 @@ func TestFunctionDispatchWithoutClient(t *testing.T) {
})
endpoint.Register(fn)

// It's possible to create a call since an endpoint URL is available.
if _, err := fn.NewCall("bar"); err != nil {
if _, err := fn.BuildCall("bar"); err != nil { // allowed
t.Fatal(err)
}

Expand Down

0 comments on commit 6e268d0

Please sign in to comment.