broker: refactor #396

Merged
vtolstov merged 7 commits from broker into v4 2025-01-30 23:26:46 +03:00
4 changed files with 38 additions and 23 deletions
Showing only changes of commit 77558d7ec7 - Show all commits

View File

@ -210,8 +210,15 @@ func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.M
}
case func(broker.Message) error:
for _, message := range messages {
if err = s(message); err == nil && sub.opts.AutoAck {
err = message.Ack()
msg, ok := message.(*memoryMessage)
if !ok {
if b.opts.Logger.V(logger.ErrorLevel) {
b.opts.Logger.Error(ctx, "broker handler error", broker.ErrInvalidMessage)
}
}
msg.topic = topic
if err = s(msg); err == nil && sub.opts.AutoAck {
err = msg.Ack()
}
if err != nil {
if b.opts.Logger.V(logger.ErrorLevel) {

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/textproto"
"sort"
"strings"
)
@ -464,27 +465,29 @@ type Iterator struct {
cnt int
}
/*
// Next advance iterator to next element
func (iter *Iterator) Next(k, v *string) bool {
func (iter *Iterator) Next(k *string, v *[]string) bool {
if iter.cur+1 > iter.cnt {
return false
}
*k = iter.keys[iter.cur]
*v = iter.Metadata[*k]
iter.cur++
if k != nil && v != nil {
*k = iter.keys[iter.cur]
vv := iter.md[*k]
*v = make([]string, len(vv))
copy(*v, vv)
iter.cur++
}
return true
}
// Iterator returns the itarator for metadata in sorted order
func (Metadata Metadata) Iterator() *Iterator {
iter := &Iterator{Metadata: Metadata, cnt: len(Metadata)}
func (md Metadata) Iterator() *Iterator {
iter := &Iterator{md: md, cnt: len(md)}
iter.keys = make([]string, 0, iter.cnt)
for k := range Metadata {
for k := range md {
iter.keys = append(iter.keys, k)
}
sort.Strings(iter.keys)
return iter
}
*/

View File

@ -102,22 +102,27 @@ func TestPassing(t *testing.T) {
}
}
/*
func TestIterator(_ *testing.T) {
md := Metadata{
"1Last": "last",
"2First": "first",
"3Second": "second",
}
func TestIterator(t *testing.T) {
md := Pairs(
"1Last", "last",
"2First", "first",
"3Second", "second",
)
iter := md.Iterator()
var k, v string
var k string
var v []string
chk := New(3)
for iter.Next(&k, &v) {
// fmt.Printf("k: %s, v: %s\n", k, v)
chk[k] = v
}
for k, v := range chk {
if cv, ok := md[k]; !ok || len(cv) != len(v) || cv[0] != v[0] {
t.Fatalf("XXXX %#+v %#+v", chk, md)
}
}
}
*/
func TestMedataCanonicalKey(t *testing.T) {
md := New(1)

View File

@ -7,7 +7,7 @@ import (
"testing"
"time"
"go.unistack.org/micro/v3/register"
"go.unistack.org/micro/v4/register"
)
var testData = map[string][]*register.Service{