Giter Site home page Giter Site logo

broker's Introduction

Broker

The "broker" package provides functionality for working with publish/subscribe messaging systems.

The main tasks that this package solves is to simplify the creation of business logic and unify the code for different implementations.

Usage example

Below are examples that demonstrate the general use of the package, without initialization and other details. See the examples directory for specific examples.

Message publishing

func Publish(publisher broker.Publisher) {
	message := &broker.Message{
		ID: "my-unique-id",
		Header: map[string]string{
			"additional": "data",
		},
		Body: []byte(`{"my":"event payload"}`),
	}

	err := publisher.Publish("topic-name", message)
	if err != nil {
		fmt.Println("cannot publish message: ", err)
		return
	}
	fmt.Println("message has been published")
	return
}

Message subscribing

func Subscribe(subscriber broker.Subscriber) {
	subscription, err := subscriber.Subscribe(
		"topic-name",
		eventHandler,
		broker.WithDefaultErrorHandler(subscriber, &stubLogger{}),
	)
	if err != nil {
		fmt.Println("cannot subscribe on topic: ", err)
		return
	}

	go func() {
		time.Sleep(15 * time.Second)
		fmt.Println("Unsubscribing ", subscription.Unsubscribe())
	}()
	
	<-subscription.Done()
}

func eventHandler(event broker.Event) error {
	fmt.Printf("[%s] received new message\n", event.Topic())
	msg := event.Message()
	fmt.Println("Headers: ")
	for k, v := range msg.Header {
		fmt.Printf("\t%s = %s\n", k, v)
	}
	fmt.Printf("BODY:\n%s\n", string(msg.Body))

	return nil
}

type stubLogger struct {
}

func (s *stubLogger) Debug(v ...interface{}) {
	fmt.Println("DEBUG:")
	fmt.Println(v...)
}

func (s *stubLogger) Error(v ...interface{}) {
	fmt.Println("ERROR:")
	fmt.Println(v...)
}

func (s *stubLogger) Info(v ...interface{}) {
	fmt.Println("INFO:")
	fmt.Println(v...)
}

Error handling

The package provides a convenient way to handle errors and contains several "out of the box" implementations that will be sufficient for most possible use cases.

// ErrorHandler is used in order to handle errors
type ErrorHandler func(err error, sub Subscription)

Consider using the broker.WithDefaultErrorHandler. This default handler does the following:

  • logs error message
  • waits 5 seconds
  • resubscribes on a topic
    • tries to subscribe every 10 seconds in case if subscription attempt failed

You may want to add additional error handlers. It is possible by combining them using broker.CombineErrorHandlers.

Message acknowledgment

By default, received messages are acknowledged automatically if the Handler run without errors and returned nil. If this behavior is undesirable, then it is possible to disable auto-acknowledge using the broker.DisableAutoAck option

If this option is applied, then the handler must explicitly "Ack" the message.

func eventHandler(event broker.Event) error {
    //... some event handling business logic
	
	err := event.Ack() // explicit message acknowledgment

	return err
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.