metadata: add default headers

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-07-23 12:03:18 +03:00
parent d357fb1e0d
commit 675e121049
6 changed files with 33 additions and 13 deletions

View File

@@ -112,6 +112,13 @@ type Message struct {
Body RawMessage
}
// NewMessage create broker message with topic filled
func NewMessage(topic string) *Message {
m := &Message{Header: metadata.New(2)}
m.Header.Set(metadata.HeaderTopic, topic)
return m
}
// Subscriber is a convenience return type for the Subscribe method
type Subscriber interface {
// Options returns subscriber options

View File

@@ -6,6 +6,7 @@ import (
"github.com/google/uuid"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/metadata"
maddr "github.com/unistack-org/micro/v3/util/addr"
mnet "github.com/unistack-org/micro/v3/util/net"
"github.com/unistack-org/micro/v3/util/rand"
@@ -113,14 +114,14 @@ func (m *memoryBroker) BatchPublish(ctx context.Context, msgs []*Message, opts .
if m.opts.Codec == nil {
m.RLock()
for _, msg := range msgs {
topic, _ := msg.Header.Get("Micro-Topic")
topic, _ := msg.Header.Get(metadata.HeaderTopic)
vs = append(vs, msgWrapper{topic: topic, body: m})
}
m.RUnlock()
} else {
m.RLock()
for _, msg := range msgs {
topic, _ := msg.Header.Get("Micro-Topic")
topic, _ := msg.Header.Get(metadata.HeaderTopic)
buf, err := m.opts.Codec.Marshal(msg)
if err != nil {
m.RUnlock()

View File

@@ -4,6 +4,8 @@ import (
"context"
"fmt"
"testing"
"github.com/unistack-org/micro/v3/metadata"
)
func TestMemoryBatchBroker(t *testing.T) {
@@ -30,9 +32,9 @@ func TestMemoryBatchBroker(t *testing.T) {
for i := 0; i < count; i++ {
message := &Message{
Header: map[string]string{
"Micro-Topic": topic,
"foo": "bar",
"id": fmt.Sprintf("%d", i),
metadata.HeaderTopic: topic,
"foo": "bar",
"id": fmt.Sprintf("%d", i),
},
Body: []byte(`"hello world"`),
}
@@ -75,9 +77,9 @@ func TestMemoryBroker(t *testing.T) {
for i := 0; i < count; i++ {
message := &Message{
Header: map[string]string{
"Micro-Topic": topic,
"foo": "bar",
"id": fmt.Sprintf("%d", i),
metadata.HeaderTopic: topic,
"foo": "bar",
"id": fmt.Sprintf("%d", i),
},
Body: []byte(`"hello world"`),
}