Bolt Pub/Sub

A pure Go key/value store.

Bolt Pub/Sub

Bolt is a pure Go key/value store which provides a simple, fast, and reliable database for projects that don’t require a full database server such as Postgres or MySQL.

Bolt backed Pub/Sub is good for simple applications which don’t need a more advanced Pub/Sub system with external dependencies or already use Bolt and want to publish messages in transaction when saving other data.

Bolt documentation: https://github.com/etcd-io/bbolt

Installation

go get github.com/ThreeDotsLabs/watermill-bolt

Characteristics

FeatureImplementsNote
ConsumerGroupsno
ExactlyOnceDeliveryno
GuaranteedOrderno
Persistentyes

Configuration

Full source: github.com/ThreeDotsLabs/watermill-bolt/pkg/bolt/bolt.go

// ...
type CommonConfig struct {
	// Bucket specifies the parent bucket in which topics and subscriptions
	// will be stored. The first element is the name of the parent bucket,
	// second is the name of its first child etc. It has to have at least
	// one element.
	Bucket []BucketName

	// Defaults to JSONMarshaler.
	Marshaler Marshaler

	// Defaults to watermill.NopLogger.
	Logger watermill.LoggerAdapter
}
// ...

Full source: github.com/ThreeDotsLabs/watermill-bolt/pkg/bolt/bolt.go

// ...
type PublisherConfig struct {
	Common CommonConfig
}
// ...

Full source: github.com/ThreeDotsLabs/watermill-bolt/pkg/bolt/bolt.go

// ...
type SubscriberConfig struct {
	Common CommonConfig

	// GenerateSubscriptionName is used to create a unique identifier for
	// subscriptions created by the subscriber. The names created by
	// multiple subscribers have to be unique within a single topic. Once
	// you set this function for a particular subscriber you should not
	// change it in the future to avoid accidentally abandoning your old
	// subscriptions.
	//
	// If only one subscriber is used to listen to various topics using
	// your database then it is perfectly fine to use the default value or
	// write a function that returns a contant string, for example the name
	// of your application.
	//
	// Defaults to topic + "_sub".
	GenerateSubscriptionName GenerateSubscriptionNameFn
}
// ...
Subscription name

To receive messages published to a topic, you must create a subscription to that topic. Only messages published to the topic after the subscription is created will be received by the subscriber.

A topic can have multiple subscriptions, but a given subscription belongs to a single topic.

In Watermill, the subscription is created automatically during calling Subscribe(). Subscription name is generated by calling the function set as SubscriberConfig.GenerateSubscriptionName. By default, it is the topic name with the string _sub appended to it.

Marshaler

Watermill’s messages cannot be directly saved in Bolt which operates on byte slices. Marshaller converts the messages to and from byte slices. The default implementation marshals messages as JSON, a format which is human-readable for easier debugging. The performance should be enough for most applications unless a very large messages are used within your system. If that is the case you may want to consider implementing a more efficient marshaler.

Full source: github.com/ThreeDotsLabs/watermill-bolt/pkg/bolt/marshaler.go

// ...
// Marshaler is responsible for marshalling and unmarshaling messages for
// storage. Implementations need to marshal and unmarshal all fields of the
// persisted message.
type Marshaler interface {
	Marshal(msg PersistedMessage) ([]byte, error)
	Unmarshal(b []byte) (PersistedMessage, error)
}
// ...