publish

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Example (Kafka)
var pub = NewKafkaPublisher(context.Background(), KafkaConfig{
	Hosts: []string{"kafka.service.net"},
	Topic: "topic-test",
})
_ = pub.Run()
defer pub.Close()

var message = Message{
	EventName: "domain_system_module_componentA_run_fail",
	Body:      []byte("error: "),
}

body, _ := json.Marshal(message)

_ = pub.Send(body)
Output:

Example (Merge)
var pub = NewKafkaPublisher(context.Background(), KafkaConfig{
	Hosts: []string{"kafka.service.net"},
	Topic: "topic-test",
})
_ = pub.Run()
defer pub.Close()

var m = merge.NewMerge(context.Background(), time.Millisecond*500)
m.Run()
defer m.Close()

for i := 0; i < 1000000; i++ {
	var randNumber = rand.Intn(50)

	var k = fmt.Sprint("domain_system_module_componentA_run_fail", randNumber)

	if !m.Allowed(merge.Key(k)) {
		continue
	}

	var message = Message{
		EventName: k,
		Body:      []byte("error: "),
	}
	body, _ := json.Marshal(message)
	_ = pub.Send(body)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMiddlewareFunc

func GetMiddlewareFunc(config MiddlewareConfig) func(http.ResponseWriter, *http.Request)

func InstallStd

func InstallStd(config MiddlewareConfig, handler http.Handler) func(http.ResponseWriter, *http.Request)

Types

type KafkaConfig

type KafkaConfig struct {
	Hosts []string
	Topic string
}

type MiddlewareConfig

type MiddlewareConfig struct {
	Topic string

	// CreateMessage defines a format of a message.
	CreateMessage func(*http.Request) []byte

	// SendCondition judge sending condition.
	SendCondition func(*http.Request) bool

	// Publisher can be assigned to NewKafkaPublisher for default and NewFakeInformer for test, or your customization.
	// Note that you must call Publish.Run and it must be closed when no longer in use.
	Publisher Publish

	// Fail will call when error occurs.
	Fail func(error)
}

MiddlewareConfig includes essential info must be used.

type Publish

type Publish interface {
	// Send sends data to Broker.
	// Note that data is recommended to design into a struct include a string key and a bytes type data.
	//
	// Suggestion: Use merge.Merge to merge same events in a tiny interval.
	Send(data []byte) error
	Run() error
	Close() error
}

func NewFakePublisher

func NewFakePublisher() Publish

func NewKafkaPublisher

func NewKafkaPublisher(ctx context.Context, config KafkaConfig) Publish

Jump to

Keyboard shortcuts

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