2016-12-14 15:41:48 +00:00
|
|
|
// Package broker is an interface used for asynchronous messaging
|
2023-04-11 22:20:37 +03:00
|
|
|
package broker // import "go.unistack.org/micro/v4/broker"
|
2015-04-26 19:33:35 +01:00
|
|
|
|
2020-11-18 16:50:41 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2021-07-01 15:10:43 +03:00
|
|
|
"errors"
|
2020-11-18 16:50:41 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/metadata"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2020-11-18 16:50:41 +03:00
|
|
|
)
|
2020-10-16 09:38:57 +03:00
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
// DefaultBroker default memory broker
|
2023-07-29 00:40:58 +03:00
|
|
|
var DefaultBroker Broker = NewBroker()
|
2020-08-28 11:52:51 +03:00
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
var (
|
|
|
|
// ErrNotConnected returns when broker used but not connected yet
|
|
|
|
ErrNotConnected = errors.New("broker not connected")
|
|
|
|
// ErrDisconnected returns when broker disconnected
|
|
|
|
ErrDisconnected = errors.New("broker disconnected")
|
2023-05-09 20:04:15 +03:00
|
|
|
// ErrInvalidMessage returns when message has nvalid format
|
|
|
|
ErrInvalidMessage = errors.New("broker message has invalid format")
|
2021-07-22 22:53:44 +03:00
|
|
|
)
|
|
|
|
|
2016-01-30 21:18:57 +00:00
|
|
|
// Broker is an interface used for asynchronous messaging.
|
2015-04-26 19:33:35 +01:00
|
|
|
type Broker interface {
|
2021-07-22 22:53:44 +03:00
|
|
|
// Name returns broker instance name
|
2021-01-29 13:17:32 +03:00
|
|
|
Name() string
|
2021-07-22 22:53:44 +03:00
|
|
|
// Init initilize broker
|
2023-07-29 00:40:58 +03:00
|
|
|
Init(opts ...options.Option) error
|
2021-07-22 22:53:44 +03:00
|
|
|
// Options returns broker options
|
2015-12-31 18:11:46 +00:00
|
|
|
Options() Options
|
2021-07-22 22:53:44 +03:00
|
|
|
// Address return configured address
|
2019-07-10 19:58:30 +01:00
|
|
|
Address() string
|
2021-07-22 22:53:44 +03:00
|
|
|
// Connect connects to broker
|
|
|
|
Connect(ctx context.Context) error
|
|
|
|
// Disconnect disconnect from broker
|
|
|
|
Disconnect(ctx context.Context) error
|
2023-07-29 00:40:58 +03:00
|
|
|
// Publish message, msg can be single broker.Message or []broker.Message
|
|
|
|
Publish(ctx context.Context, msg interface{}, opts ...options.Option) error
|
2021-07-22 22:53:44 +03:00
|
|
|
// Subscribe subscribes to topic message via handler
|
2023-07-29 00:40:58 +03:00
|
|
|
Subscribe(ctx context.Context, topic string, handler interface{}, opts ...options.Option) (Subscriber, error)
|
2021-07-22 22:53:44 +03:00
|
|
|
// String type of broker
|
2015-12-19 21:56:14 +00:00
|
|
|
String() string
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
// Message is given to a subscription handler for processing
|
|
|
|
type Message interface {
|
|
|
|
// Context for the message
|
|
|
|
Context() context.Context
|
2023-07-29 00:40:58 +03:00
|
|
|
// Topic
|
2020-08-27 11:18:02 +03:00
|
|
|
Topic() string
|
2023-07-29 00:40:58 +03:00
|
|
|
// Header returns message headers
|
|
|
|
Header() metadata.Metadata
|
|
|
|
// Body returns broker message may be []byte slice or some go struct
|
2023-05-09 20:04:15 +03:00
|
|
|
Body() interface{}
|
2021-07-22 22:53:44 +03:00
|
|
|
// Ack acknowledge message
|
2020-08-27 11:18:02 +03:00
|
|
|
Ack() error
|
2021-07-22 22:53:44 +03:00
|
|
|
// Error returns message error (like decoding errors or some other)
|
2023-05-09 20:04:15 +03:00
|
|
|
// In this case Body contains raw []byte from broker
|
2020-08-27 11:18:02 +03:00
|
|
|
Error() error
|
|
|
|
}
|
2020-08-18 14:00:51 +01:00
|
|
|
|
2016-12-06 18:37:35 +00:00
|
|
|
// Subscriber is a convenience return type for the Subscribe method
|
2015-04-26 19:33:35 +01:00
|
|
|
type Subscriber interface {
|
2021-07-22 22:53:44 +03:00
|
|
|
// Options returns subscriber options
|
2015-12-31 18:11:46 +00:00
|
|
|
Options() SubscribeOptions
|
2021-07-22 22:53:44 +03:00
|
|
|
// Topic returns topic for subscription
|
2015-04-26 19:33:35 +01:00
|
|
|
Topic() string
|
2021-07-22 22:53:44 +03:00
|
|
|
// Unsubscribe from topic
|
|
|
|
Unsubscribe(ctx context.Context) error
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
2023-07-29 00:40:58 +03:00
|
|
|
|
|
|
|
// MessageHandler func signature for single message processing
|
|
|
|
type MessageHandler func(Message) error
|
|
|
|
|
|
|
|
// MessagesHandler func signature for batch message processing
|
|
|
|
type MessagesHandler func([]Message) error
|