2016-12-14 15:41:48 +00:00
|
|
|
// Package broker is an interface used for asynchronous messaging
|
2024-09-20 17:54:17 +03:00
|
|
|
package 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"
|
2024-04-07 20:53:01 +03:00
|
|
|
"time"
|
2020-11-18 16:50:41 +03:00
|
|
|
|
2025-01-25 15:48:10 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/metadata"
|
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
|
2024-03-06 18:45:32 +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")
|
2025-01-30 23:26:45 +03:00
|
|
|
// ErrInvalidMessage returns when invalid Message passed
|
|
|
|
ErrInvalidMessage = errors.New("invalid message")
|
|
|
|
// ErrInvalidHandler returns when subscriber passed to Subscribe
|
|
|
|
ErrInvalidHandler = errors.New("invalid handler")
|
2024-04-07 20:53:01 +03:00
|
|
|
// DefaultGracefulTimeout
|
|
|
|
DefaultGracefulTimeout = 5 * time.Second
|
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
|
|
|
|
Init(opts ...Option) error
|
|
|
|
// 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
|
2025-01-30 23:26:45 +03:00
|
|
|
// NewMessage create new broker message to publish.
|
|
|
|
NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...PublishOption) (Message, error)
|
2021-07-22 22:53:44 +03:00
|
|
|
// Publish message to broker topic
|
2025-01-30 23:26:45 +03:00
|
|
|
Publish(ctx context.Context, topic string, messages ...Message) error
|
2021-07-22 22:53:44 +03:00
|
|
|
// Subscribe subscribes to topic message via handler
|
2025-01-30 23:26:45 +03:00
|
|
|
Subscribe(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error)
|
2021-07-22 22:53:44 +03:00
|
|
|
// String type of broker
|
2015-12-19 21:56:14 +00:00
|
|
|
String() string
|
2024-12-02 13:20:13 +03:00
|
|
|
// Live returns broker liveness
|
|
|
|
Live() bool
|
|
|
|
// Ready returns broker readiness
|
|
|
|
Ready() bool
|
|
|
|
// Health returns broker health
|
|
|
|
Health() bool
|
2015-04-26 19:33:35 +01:00
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
type (
|
2025-01-30 23:26:45 +03:00
|
|
|
FuncPublish func(ctx context.Context, topic string, messages ...Message) error
|
|
|
|
HookPublish func(next FuncPublish) FuncPublish
|
|
|
|
FuncSubscribe func(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error)
|
|
|
|
HookSubscribe func(next FuncSubscribe) FuncSubscribe
|
2024-04-22 08:47:50 +03:00
|
|
|
)
|
|
|
|
|
2025-01-30 23:26:45 +03:00
|
|
|
// Message is given to a subscription handler for processing
|
|
|
|
type Message interface {
|
|
|
|
// Context for the message.
|
2024-05-05 16:22:06 +03:00
|
|
|
Context() context.Context
|
2025-01-30 23:26:45 +03:00
|
|
|
// Topic returns message destination topic.
|
2020-08-27 11:18:02 +03:00
|
|
|
Topic() string
|
2025-01-30 23:26:45 +03:00
|
|
|
// Header returns message headers.
|
|
|
|
Header() metadata.Metadata
|
|
|
|
// Body returns broker message []byte slice
|
|
|
|
Body() []byte
|
|
|
|
// Unmarshal try to decode message body to dst.
|
|
|
|
// This is helper method that uses codec.Unmarshal.
|
|
|
|
Unmarshal(dst interface{}, opts ...codec.Option) error
|
|
|
|
// Ack acknowledge message if supported.
|
2020-08-27 11:18:02 +03:00
|
|
|
Ack() error
|
2021-07-23 12:03:18 +03: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
|
|
|
}
|