-
-
Save liu-cong/a132fe8629715419c7a58ee20001d071 to your computer and use it in GitHub Desktop.
Demo CE V2 client blocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"time" | |
cloudevents "github.com/cloudevents/sdk-go/v2" | |
) | |
func main() { | |
ctx := context.Background() | |
p, err := cloudevents.NewHTTP() | |
if err != nil { | |
log.Fatalf("failed to create protocol: %s", err.Error()) | |
} | |
c, err := cloudevents.NewClient(p) | |
if err != nil { | |
log.Fatalf("failed to create client, %v", err) | |
} | |
log.Printf("will listen on :8080\n") | |
log.Fatalf("failed to start receiver: %s", c.StartReceiver(ctx, receive)) | |
} | |
func receive(ctx context.Context, event cloudevents.Event) { | |
fmt.Printf("\n========received: %s", event) | |
sleep := event.Extensions()["sleep"] | |
if d, err := time.ParseDuration(sleep.(string)); err ==nil { | |
fmt.Printf("going to sleep for %v \n", d) | |
time.Sleep(d) | |
} | |
fmt.Printf("+++++++++++ Done for %v\n", event.ID()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment