forked from cloudevents/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabling Pub/Sub to send and receive from multiple topics and subscri…
…ptions. (cloudevents#171) Signed-off-by: Scott Nichols <nicholss@google.com>
- Loading branch information
Showing
13 changed files
with
559 additions
and
164 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/cloudevents/sdk-go" | ||
"github.com/cloudevents/sdk-go/pkg/cloudevents/client" | ||
cepubsub "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/pubsub" | ||
pscontext "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/pubsub/context" | ||
"github.com/kelseyhightower/envconfig" | ||
) | ||
|
||
/* | ||
To setup: | ||
gcloud pubsub topics create demo_cloudevents | ||
gcloud pubsub subscriptions create foo --topic=demo_cloudevents | ||
To test: | ||
gcloud pubsub topics publish demo_cloudevents --message='{"Hello": "world"}' | ||
To fix a bad message: | ||
gcloud pubsub subscriptions pull --auto-ack foo | ||
*/ | ||
|
||
type envConfig struct { | ||
ProjectID string `envconfig:"GOOGLE_CLOUD_PROJECT"` | ||
|
||
SubscriptionIDs string `envconfig:"PUBSUB_SUBSCRIPTIONS" default:"foo" required:"true"` | ||
} | ||
|
||
type Example struct { | ||
Sequence int `json:"id"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func receive(ctx context.Context, event cloudevents.Event, resp *cloudevents.EventResponse) error { | ||
fmt.Printf("Event Context: %+v\n", event.Context) | ||
|
||
fmt.Printf("Transport Context: %+v\n", pscontext.TransportContextFrom(ctx)) | ||
|
||
data := &Example{} | ||
if err := event.DataAs(data); err != nil { | ||
fmt.Printf("Got Data Error: %s\n", err.Error()) | ||
} | ||
fmt.Printf("Data: %+v\n", data) | ||
|
||
fmt.Printf("----------------------------\n") | ||
return nil | ||
} | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
var env envConfig | ||
if err := envconfig.Process("", &env); err != nil { | ||
log.Printf("[ERROR] Failed to process env var: %s", err) | ||
os.Exit(1) | ||
} | ||
|
||
var opts []cepubsub.Option | ||
opts = append(opts, cepubsub.WithProjectID(env.ProjectID)) | ||
for _, subscription := range strings.Split(env.SubscriptionIDs, ",") { | ||
opts = append(opts, cepubsub.WithSubscriptionID(subscription)) | ||
} | ||
|
||
t, err := cepubsub.New(context.Background(), opts...) | ||
if err != nil { | ||
log.Fatalf("failed to create pubsub transport, %s", err.Error()) | ||
} | ||
c, err := client.New(t) | ||
if err != nil { | ||
log.Fatalf("failed to create client, %s", err.Error()) | ||
} | ||
|
||
log.Println("Created client, listening...") | ||
|
||
if err := c.StartReceiver(ctx, receive); err != nil { | ||
log.Fatalf("failed to start pubsub receiver, %s", err.Error()) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/kelseyhightower/envconfig" | ||
|
||
"github.com/cloudevents/sdk-go" | ||
cecontext "github.com/cloudevents/sdk-go/pkg/cloudevents/context" | ||
cepubsub "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/pubsub" | ||
) | ||
|
||
/* | ||
gcloud pubsub topics create ce1 | ||
gcloud pubsub topics create ce2 | ||
gcloud pubsub topics create ce3 | ||
gcloud pubsub subscriptions create ce1_sub --topic=ce1 | ||
gcloud pubsub subscriptions create ce2_sub --topic=ce2 | ||
gcloud pubsub subscriptions create ce3_sub --topic=ce3 | ||
*/ | ||
|
||
type envConfig struct { | ||
ProjectID string `envconfig:"GOOGLE_CLOUD_PROJECT" required:"true"` | ||
|
||
TopicIDs string `envconfig:"PUBSUB_TOPICS" default:"demo_cloudevents" required:"true"` | ||
} | ||
|
||
// Basic data struct. | ||
type Example struct { | ||
Sequence int `json:"id"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func main() { | ||
var env envConfig | ||
if err := envconfig.Process("", &env); err != nil { | ||
log.Printf("[ERROR] Failed to process env var: %s", err) | ||
os.Exit(1) | ||
} | ||
|
||
t, err := cepubsub.New(context.Background(), | ||
cepubsub.WithProjectID(env.ProjectID)) | ||
if err != nil { | ||
log.Printf("failed to create pubsub transport, %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
c, err := cloudevents.NewClient(t, cloudevents.WithTimeNow(), cloudevents.WithUUIDs()) | ||
if err != nil { | ||
log.Printf("failed to create client, %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
for i, topic := range strings.Split(env.TopicIDs, ",") { | ||
ctx := cecontext.WithTopic(context.Background(), topic) | ||
event := cloudevents.NewEvent(cloudevents.VersionV03) | ||
event.SetType("com.cloudevents.sample.sent") | ||
event.SetSource("github.com/cloudevents/sdk-go/cmd/samples/pubsub/multisender/") | ||
_ = event.SetData(&Example{ | ||
Sequence: i, | ||
Message: "HELLO " + topic, | ||
}) | ||
|
||
_, err = c.Send(ctx, event) | ||
|
||
if err != nil { | ||
log.Printf("failed to send: %v", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
os.Exit(0) | ||
} |
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
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
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
2 changes: 1 addition & 1 deletion
2
pkg/cloudevents/transport/pubsub/context.go → ...vents/transport/pubsub/context/context.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pubsub | ||
package context | ||
|
||
import ( | ||
"context" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package context_test |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.