components-contrib/pubsub
Yordan Pavlov 575b530bc7 Merge latest changes from upstream and resolve conflict
Signed-off-by: Yordan Pavlov <yordan.pavlov@dunnhumby.com>
2022-03-10 15:47:11 +00:00
..
aws/snssqs bugfixes: visibility timeout, dlq queue attributes (#1446) 2022-01-18 10:31:53 -08:00
azure Move errors to pseudo constant 2022-02-24 15:44:09 -08:00
gcp/pubsub Merge latest changes from upstream and resolve conflict 2022-03-10 15:47:11 +00:00
hazelcast update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
in-memory update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
jetstream Add support for nats jetstream wildcard subscriptions (#1465) 2022-02-15 09:46:33 -08:00
kafka Kafka PubSub: Use metadata as message headers. (#1409) 2022-01-06 10:10:37 -08:00
mqtt MQTT Pubsub Certification Testing + AutAckOff Fix for MQTT (#1420) 2022-01-07 10:33:54 -08:00
natsstreaming update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
pulsar feature/pulsar: add producer option disableBatching 2022-02-16 12:07:02 +08:00
rabbitmq supports rabbitmq pubsub set exchangekind (#1520) 2022-02-21 13:08:18 -08:00
redis update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
rocketmq update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
Readme.md TTL in PubSub. (#565) 2020-12-29 14:28:52 -08:00
concurrency.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
concurrency_test.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
envelope.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
envelope_test.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
feature.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
metadata.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
pubsub.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
requests.go Added ContentType to pubsub/binding/state request-response (#1376) 2022-01-28 10:17:04 -08:00
responses.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00

Readme.md

Pub Sub

Pub Subs provide a common way to interact with different message bus implementations to achieve reliable, high-scale scenarios based on event-driven async communications, while allowing users to opt-in to advanced capabilities using defined metadata.

Currently supported pub-subs are:

  • Hazelcast
  • Redis Streams
  • NATS
  • Kafka
  • Azure Service Bus
  • RabbitMQ
  • Azure Event Hubs
  • GCP Pub/Sub
  • MQTT

Implementing a new Pub Sub

A compliant pub sub needs to implement the following interface:

type PubSub interface {
	Init(metadata Metadata) error
	Publish(req *PublishRequest) error
	Subscribe(req SubscribeRequest, handler func(msg *NewMessage) error) error
}

Message TTL (or Time To Live)

Message Time to live is implemented by default in Dapr. A publishing application can set the expiration of individual messages by publishing it with the ttlInSeconds metadata. Components that support message TTL should parse this metadata attribute. For components that do not implement this feature in Dapr, the runtime will automatically populate the expiration attribute in the CloudEvent object if ttlInSeconds is present - in this case, Dapr will expire the message when a Dapr subscriber is about to consume an expired message. The expiration attribute is handled by Dapr runtime as a convenience to subscribers, dropping expired messages without invoking subscribers' endpoint. Subscriber applications that don't use Dapr, need to handle this attribute and implement the expiration logic.

If the pub sub component implementation can handle message TTL natively without relying on Dapr, consume the ttlInSeconds metadata in the component implementation for the Publish function. Also, implement the Features() function so the Dapr runtime knows that it should not add the expiration attribute to events.

Example:

import contrib_metadata "github.com/dapr/components-contrib/metadata"

//...

func (c *MyComponent) Publish(req *pubsub.PublishRequest) error {
	//...
	ttl, hasTTL, _ := contrib_metadata.TryGetTTL(req.Metadata)
	if hasTTL {
		//... handle ttl for component.
	}
	//...
	return nil
}

func (c *MyComponent) Features() []pubsub.Feature {
	// Tip: cache this list into a private property.
	// Simply return nil if component does not implement any addition features.
	return []pubsub.Feature{pubsub.FeatureMessageTTL}
}

For pub sub components that support TTL per topic or queue but not per message, there are some design choices:

  • Configure the TTL for the topic or queue as usual. Optionally, implement topic or queue provisioning in the Init() method, using the component configuration's metadata to determine the topic or queue TTL.
  • Let Dapr runtime handle ttlInSeconds for messages that want to expire earlier than the topic's or queue's TTL. So, applications can still benefit from TTL per message via Dapr for this scenario.

Note: as per the CloudEvent spec, timestamps (like expiration) are formatted using RFC3339.