package mempubsub
import "gocloud.dev/pubsub/mempubsub"
Package mempubsub provides an in-memory pubsub implementation. Use NewTopic to construct a *pubsub.Topic, and/or NewSubscription to construct a *pubsub.Subscription.
mempubsub should not be used for production: it is intended for local development and testing.
URLs
For pubsub.OpenTopic and pubsub.OpenSubscription, mempubsub registers for the scheme "mem". To customize the URL opener, or for more details on the URL format, see URLOpener. See https://godoc.org/gocloud.dev#hdr-URLs for background information.
Message Delivery Semantics
mempubsub supports at-least-once semantics; applications must call Message.Ack after processing a message, or it will be redelivered. See https://godoc.org/gocloud.dev/pubsub#hdr-At_most_once_and_At_least_once_Delivery for more background.
As
mempubsub does not support any types for As.
Code:play
Output: Code:play
Example¶
package main
import (
"context"
"fmt"
"log"
"time"
"gocloud.dev/pubsub"
"gocloud.dev/pubsub/mempubsub"
)
func main() {
// Construct a *pubsub.Topic.
ctx := context.Background()
t := mempubsub.NewTopic()
defer t.Shutdown(ctx)
// Construct a *pubsub.Subscription for the topic.
s := mempubsub.NewSubscription(t, 1*time.Minute /* ack deadline */)
defer s.Shutdown(ctx)
// Now we can use t to send messages and s will receive them.
err := t.Send(ctx, &pubsub.Message{Body: []byte("Hello World")})
if err != nil {
log.Fatal(err)
}
msg, err := s.Receive(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(msg.Body))
msg.Ack()
}
Hello World
Example (OpenFromURL)¶
package main
import (
"context"
"gocloud.dev/pubsub"
)
func main() {
ctx := context.Background()
// OpenTopic creates a *pubsub.Topic from a URL.
// This URL will open the topic "mytopic".
t, err := pubsub.OpenTopic(ctx, "mem://mytopic")
// Similarly, OpenSubscription creates a *pubsub.Subscription from a URL.
// This URL will open a subscription to the topic "mytopic", which must
// have been previously opened using OpenTopic.
s, err := pubsub.OpenSubscription(ctx, "mem://mytopic")
_, _, _ = t, s, err
}
Index ¶
- Constants
- func NewSubscription(pstopic *pubsub.Topic, ackDeadline time.Duration) *pubsub.Subscription
- func NewTopic() *pubsub.Topic
- type URLOpener
Examples ¶
Constants ¶
const Scheme = "mem"
Scheme is the URL scheme mempubsub registers its URLOpeners under on pubsub.DefaultMux.
Functions ¶
func NewSubscription ¶
NewSubscription creates a new subscription for the given topic. It panics if the given topic did not come from mempubsub. If a message is not acked within in the given ack deadline from when it is received, then it will be redelivered.
func NewTopic ¶
NewTopic creates a new in-memory topic.
Types ¶
type URLOpener ¶
type URLOpener struct {
// contains filtered or unexported fields
}
URLOpener opens mempubsub URLs like "mem://topic".
The URL's host+path is used as the topic to create or subscribe to.
Query parameters:
- ackdeadline: The ack deadline for OpenSubscription, in time.ParseDuration formats. Defaults to 1m.
func (*URLOpener) OpenSubscriptionURL ¶
func (o *URLOpener) OpenSubscriptionURL(ctx context.Context, u *url.URL) (*pubsub.Subscription, error)
OpenSubscriptionURL opens a pubsub.Subscription based on u.
func (*URLOpener) OpenTopicURL ¶
OpenTopicURL opens a pubsub.Topic based on u.
Source Files ¶
mem.go
- Version
- v0.13.0
- Published
- Apr 24, 2019
- Platform
- js/wasm
- Imports
- 10 packages
- Last checked
- 4 days ago –
Tools for package owners.