protobus

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

ProtoBus

ProtoBus borrows simple intra-process Message Bus from Grafana, but has a much better performance.

Publish Example
func ExampleInProcBus_Publish() {
	bus := protobus.ProvideBus()

	protobus.AddEventListener(bus, func(ctx context.Context, query *emptypb.Empty) error {
		fmt.Printf("event received: %T", query)
		return nil
	})

	bus.Publish(context.Background(), &emptypb.Empty{})
	// Output: event received: *emptypb.Empty
}
Error Handling Example
func main() {
	bus := protobus.ProvideBus()
	var ErrEmpty = errors.New("empty")

	protobus.AddEventListener(bus, func(ctx context.Context, query *emptypb.Empty) error {
		if query == nil {
			return ErrEmpty
		}
		fmt.Printf("event received: %T", query)
		return nil
	})
	if err := bus.Publish(context.Background(), (*emptypb.Empty)(nil)); err != nil {
		fmt.Println("error received:", err)
		fmt.Println("error is ErrEmpty?", errors.Is(err, ErrEmpty))
	}
	bus.Publish(context.Background(), &emptypb.Empty{})
	// Output:
	// error received: empty
	// error is ErrEmpty? true
	// event received: *emptypb.Empty
}
Benchmark
goos: linux
goarch: amd64
pkg: github.com/Myriad-Dreamin/protobus
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
BenchmarkEventCtxPublish-24         	 2662341	       458.2 ns/op	     176 B/op	       6 allocs/op
BenchmarkProtoEventCtxPublish-24    	34715662	        32.10 ns/op	      48 B/op	       1 allocs/op
PASS
	github.com/Myriad-Dreamin/protobus	coverage: 100% of statements
ok  	github.com/Myriad-Dreamin/protobus	2.830s

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddEventListener

func AddEventListener[T Msg](b *InProcBus, handler HandlerFuncT[T])
Example
bus := ProvideBus()
var ErrEmpty = errors.New("empty")

AddEventListener(bus, func(ctx context.Context, query *emptypb.Empty) error {
	if query == nil {
		return ErrEmpty
	}
	fmt.Printf("event received: %T", query)
	return nil
})
if err := bus.Publish(context.Background(), (*emptypb.Empty)(nil)); err != nil {
	fmt.Println("error received:", err)
	fmt.Println("error is ErrEmpty?", errors.Is(err, ErrEmpty))
}
bus.Publish(context.Background(), &emptypb.Empty{})
Output:

error received: empty
error is ErrEmpty? true
event received: *emptypb.Empty

Types

type Bus

type Bus interface {
	Publish(ctx context.Context, msg Msg) error
	AddEventListener(handler HandlerFunc)
}

Bus type defines the bus interface structure.

type HandlerFunc

type HandlerFunc func(ctx context.Context, msg Msg) error

HandlerFunc defines a handler function interface.

type HandlerFuncT

type HandlerFuncT[T Msg] func(ctx context.Context, msg T) error

type InProcBus

type InProcBus struct {
	// contains filtered or unexported fields
}

InProcBus defines the bus structure.

func ProvideBus

func ProvideBus() *InProcBus

func (*InProcBus) Publish

func (b *InProcBus) Publish(ctx context.Context, msg Msg) error

Publish function publish a message to the bus listener.

Example
bus := ProvideBus()

AddEventListener(bus, func(ctx context.Context, query *emptypb.Empty) error {
	fmt.Printf("event received: %T", query)
	return nil
})

bus.Publish(context.Background(), &emptypb.Empty{})
Output:

event received: *emptypb.Empty

type Msg

type Msg = proto.Message

Msg defines a message interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL