From 172e8532cf8107b2049594f172e98b2588b43e68 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Mon, 9 Nov 2020 18:17:04 +0100 Subject: [PATCH 1/2] Proposal for redesigning the client interface Signed-off-by: Francesco Guardiani --- v2/client/client.go | 58 ++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/v2/client/client.go b/v2/client/client.go index c5d9056cd..756216dd8 100644 --- a/v2/client/client.go +++ b/v2/client/client.go @@ -17,32 +17,52 @@ import ( ) // Client interface defines the runtime contract the CloudEvents client supports. -type Client interface { +type Sender interface { // Send will transmit the given event over the client's configured transport. Send(ctx context.Context, event event.Event) protocol.Result // Request will transmit the given event over the client's configured // transport and return any response event. Request(ctx context.Context, event event.Event) (*event.Event, protocol.Result) +} + +type ReceiverEvent interface { + Event() event.Event + Ack() + Reply(event *event.Event) + ReplyWithResult(*event.Event, protocol.Result) +} + +// Client interface defines the runtime contract the CloudEvents client supports. +type Receiver interface { + Channel() <-chan ReceiverEvent +} + +type Client interface { + protocol.Closer + Sender + Receiver +} + +// StartHandler will register the provided function for callback on receipt +// of a cloudevent. It will also start the underlying protocol as it has +// been configured. +// This call is blocking. +// Valid fn signatures are: +// * func() +// * func() error +// * func(context.Context) +// * func(context.Context) protocol.Result +// * func(event.Event) +// * func(event.Event) protocol.Result +// * func(context.Context, event.Event) +// * func(context.Context, event.Event) protocol.Result +// * func(event.Event) *event.Event +// * func(event.Event) (*event.Event, protocol.Result) +// * func(context.Context, event.Event) *event.Event +// * func(context.Context, event.Event) (*event.Event, protocol.Result) +func StartHandler(ctx context.Context, fn interface{}) error { - // StartReceiver will register the provided function for callback on receipt - // of a cloudevent. It will also start the underlying protocol as it has - // been configured. - // This call is blocking. - // Valid fn signatures are: - // * func() - // * func() error - // * func(context.Context) - // * func(context.Context) protocol.Result - // * func(event.Event) - // * func(event.Event) protocol.Result - // * func(context.Context, event.Event) - // * func(context.Context, event.Event) protocol.Result - // * func(event.Event) *event.Event - // * func(event.Event) (*event.Event, protocol.Result) - // * func(context.Context, event.Event) *event.Event - // * func(context.Context, event.Event) (*event.Event, protocol.Result) - StartReceiver(ctx context.Context, fn interface{}) error } // New produces a new client with the provided transport object and applied From 43b2e85e106ab5735a07ae57b13bfade3f1e62c0 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Mon, 9 Nov 2020 18:21:35 +0100 Subject: [PATCH 2/2] Now It's more clean Signed-off-by: Francesco Guardiani --- v2/client/client.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/v2/client/client.go b/v2/client/client.go index 756216dd8..055319f28 100644 --- a/v2/client/client.go +++ b/v2/client/client.go @@ -17,34 +17,22 @@ import ( ) // Client interface defines the runtime contract the CloudEvents client supports. -type Sender interface { +type Client interface { // Send will transmit the given event over the client's configured transport. Send(ctx context.Context, event event.Event) protocol.Result // Request will transmit the given event over the client's configured // transport and return any response event. Request(ctx context.Context, event event.Event) (*event.Event, protocol.Result) -} -type ReceiverEvent interface { - Event() event.Event - Ack() - Reply(event *event.Event) - ReplyWithResult(*event.Event, protocol.Result) -} + Receive(ctx context.Context) (event.Event, error) -// Client interface defines the runtime contract the CloudEvents client supports. -type Receiver interface { - Channel() <-chan ReceiverEvent + Respond(ctx context.Context) (event.Event, ResponseFn, error) } -type Client interface { - protocol.Closer - Sender - Receiver -} +type ResponseFn func(ctx context.Context, e *event.Event, r protocol.Result) error -// StartHandler will register the provided function for callback on receipt +// StartEventHandler will register the provided function for callback on receipt // of a cloudevent. It will also start the underlying protocol as it has // been configured. // This call is blocking. @@ -61,7 +49,7 @@ type Client interface { // * func(event.Event) (*event.Event, protocol.Result) // * func(context.Context, event.Event) *event.Event // * func(context.Context, event.Event) (*event.Event, protocol.Result) -func StartHandler(ctx context.Context, fn interface{}) error { +func StartEventHandler(ctx context.Context, fn interface{}) error { }