forked from micro/go-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove unnecessary dependencies between plugins
remove unnecessary dependencies between plugins upgrade go-micro.dev/v4 to v4.2.1
- Loading branch information
Showing
206 changed files
with
740 additions
and
671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
// Package memory provides a memory broker | ||
package broker | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"go-micro.dev/v4/logger" | ||
maddr "go-micro.dev/v4/util/addr" | ||
mnet "go-micro.dev/v4/util/net" | ||
) | ||
|
||
type memoryBroker struct { | ||
opts Options | ||
|
||
addr string | ||
sync.RWMutex | ||
connected bool | ||
Subscribers map[string][]*memorySubscriber | ||
} | ||
|
||
type memoryEvent struct { | ||
opts Options | ||
topic string | ||
err error | ||
message interface{} | ||
} | ||
|
||
type memorySubscriber struct { | ||
id string | ||
topic string | ||
exit chan bool | ||
handler Handler | ||
opts SubscribeOptions | ||
} | ||
|
||
func (m *memoryBroker) Options() Options { | ||
return m.opts | ||
} | ||
|
||
func (m *memoryBroker) Address() string { | ||
return m.addr | ||
} | ||
|
||
func (m *memoryBroker) Connect() error { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
if m.connected { | ||
return nil | ||
} | ||
|
||
// use 127.0.0.1 to avoid scan of all network interfaces | ||
addr, err := maddr.Extract("127.0.0.1") | ||
if err != nil { | ||
return err | ||
} | ||
i := rand.Intn(20000) | ||
// set addr with port | ||
addr = mnet.HostPort(addr, 10000+i) | ||
|
||
m.addr = addr | ||
m.connected = true | ||
|
||
return nil | ||
} | ||
|
||
func (m *memoryBroker) Disconnect() error { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
if !m.connected { | ||
return nil | ||
} | ||
|
||
m.connected = false | ||
|
||
return nil | ||
} | ||
|
||
func (m *memoryBroker) Init(opts ...Option) error { | ||
for _, o := range opts { | ||
o(&m.opts) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *memoryBroker) Publish(topic string, msg *Message, opts ...PublishOption) error { | ||
m.RLock() | ||
if !m.connected { | ||
m.RUnlock() | ||
return errors.New("not connected") | ||
} | ||
|
||
subs, ok := m.Subscribers[topic] | ||
m.RUnlock() | ||
if !ok { | ||
return nil | ||
} | ||
|
||
var v interface{} | ||
if m.opts.Codec != nil { | ||
buf, err := m.opts.Codec.Marshal(msg) | ||
if err != nil { | ||
return err | ||
} | ||
v = buf | ||
} else { | ||
v = msg | ||
} | ||
|
||
p := &memoryEvent{ | ||
topic: topic, | ||
message: v, | ||
opts: m.opts, | ||
} | ||
|
||
for _, sub := range subs { | ||
if err := sub.handler(p); err != nil { | ||
p.err = err | ||
if eh := m.opts.ErrorHandler; eh != nil { | ||
eh(p) | ||
continue | ||
} | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *memoryBroker) Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) { | ||
m.RLock() | ||
if !m.connected { | ||
m.RUnlock() | ||
return nil, errors.New("not connected") | ||
} | ||
m.RUnlock() | ||
|
||
var options SubscribeOptions | ||
for _, o := range opts { | ||
o(&options) | ||
} | ||
|
||
sub := &memorySubscriber{ | ||
exit: make(chan bool, 1), | ||
id: uuid.New().String(), | ||
topic: topic, | ||
handler: handler, | ||
opts: options, | ||
} | ||
|
||
m.Lock() | ||
m.Subscribers[topic] = append(m.Subscribers[topic], sub) | ||
m.Unlock() | ||
|
||
go func() { | ||
<-sub.exit | ||
m.Lock() | ||
var newSubscribers []*memorySubscriber | ||
for _, sb := range m.Subscribers[topic] { | ||
if sb.id == sub.id { | ||
continue | ||
} | ||
newSubscribers = append(newSubscribers, sb) | ||
} | ||
m.Subscribers[topic] = newSubscribers | ||
m.Unlock() | ||
}() | ||
|
||
return sub, nil | ||
} | ||
|
||
func (m *memoryBroker) String() string { | ||
return "memory" | ||
} | ||
|
||
func (m *memoryEvent) Topic() string { | ||
return m.topic | ||
} | ||
|
||
func (m *memoryEvent) Message() *Message { | ||
switch v := m.message.(type) { | ||
case *Message: | ||
return v | ||
case []byte: | ||
msg := &Message{} | ||
if err := m.opts.Codec.Unmarshal(v, msg); err != nil { | ||
if logger.V(logger.ErrorLevel, logger.DefaultLogger) { | ||
logger.Errorf("[memory]: failed to unmarshal: %v\n", err) | ||
} | ||
return nil | ||
} | ||
return msg | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *memoryEvent) Ack() error { | ||
return nil | ||
} | ||
|
||
func (m *memoryEvent) Error() error { | ||
return m.err | ||
} | ||
|
||
func (m *memorySubscriber) Options() SubscribeOptions { | ||
return m.opts | ||
} | ||
|
||
func (m *memorySubscriber) Topic() string { | ||
return m.topic | ||
} | ||
|
||
func (m *memorySubscriber) Unsubscribe() error { | ||
m.exit <- true | ||
return nil | ||
} | ||
|
||
func NewMemoryBroker(opts ...Option) Broker { | ||
options := Options{ | ||
Context: context.Background(), | ||
} | ||
|
||
rand.Seed(time.Now().UnixNano()) | ||
for _, o := range opts { | ||
o(&options) | ||
} | ||
|
||
return &memoryBroker{ | ||
opts: options, | ||
Subscribers: make(map[string][]*memorySubscriber), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package broker_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"go-micro.dev/v4/broker" | ||
) | ||
|
||
func TestMemoryBroker(t *testing.T) { | ||
b := broker.NewMemoryBroker() | ||
|
||
if err := b.Connect(); err != nil { | ||
t.Fatalf("Unexpected connect error %v", err) | ||
} | ||
|
||
topic := "test" | ||
count := 10 | ||
|
||
fn := func(p broker.Event) error { | ||
return nil | ||
} | ||
|
||
sub, err := b.Subscribe(topic, fn) | ||
if err != nil { | ||
t.Fatalf("Unexpected error subscribing %v", err) | ||
} | ||
|
||
for i := 0; i < count; i++ { | ||
message := &broker.Message{ | ||
Header: map[string]string{ | ||
"foo": "bar", | ||
"id": fmt.Sprintf("%d", i), | ||
}, | ||
Body: []byte(`hello world`), | ||
} | ||
|
||
if err := b.Publish(topic, message); err != nil { | ||
t.Fatalf("Unexpected error publishing %d", i) | ||
} | ||
} | ||
|
||
if err := sub.Unsubscribe(); err != nil { | ||
t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err) | ||
} | ||
|
||
if err := b.Disconnect(); err != nil { | ||
t.Fatalf("Unexpected connect error %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.