# TiEventbus

TiEventbus is designed to simplify the development with MQ and let engineers focus on implementing business logic.

Design Doc: https://pingcap.feishu.cn/wiki/wikcnL57eAaqCvb6w9k2cP732Xb

Interface Design Doc: https://pingcap.feishu.cn/wiki/wikcnzA5A0GrqufOQWVxq9oIpDe

## How to use

There is a simple example.

```go
package main

import (
    "fmt"
    "time"

    "github.com/tidbcloud/tiinf/transport/tieventbus"
)

func main() {
    config := tieventbus.EventbusConfig{AppName: "app"}
    eventbus, err := tieventbus.NewEventbus(config)
    if err != nil {
        panic(err)
    }
    eventType := "event-type"
    group := "default"
    handlerConfig := tieventbus.HandlerConfig{
        EventType: eventType,
        Group:     group,
        HandleFunc: func(event *tieventbus.Event) error {
            payload := event.Data.(*map[string]string)
            fmt.Println(fmt.Sprintf("Event has been recieved, ID: %s payload: %v\n", event.ID, *payload))
            return nil
        },
        DataStruct: make(map[string]string),
    }
    eventbus.RegisterHandler(handlerConfig)
    err = eventbus.Run()
    if err != nil {
        panic(err)
    }
    event := tieventbus.Event{
        ID:   "1",
        Type: eventType,
        Data: map[string]string{"greeting": "Hi"},
    }
    err = eventbus.Publish(&event)
    if err != nil {
        panic(err)
    }
    time.Sleep(50 * time.Millisecond)
}
```

If you want to learn more about how to use tieventbus, following is more example.

[Basic Pub/Sub](https://github.com/tidbcloud/tiinf/blob/master/transport/tieventbus/example/subpub/main.go)

[Exactly Once Proccess](https://github.com/tidbcloud/tiinf/blob/master/transport/tieventbus/example/exactly_once_proccess/main.go)

[Fix DLQ](https://github.com/tidbcloud/tiinf/blob/master/transport/tieventbus/example/fix_dlq/main.go)

[Gorm integrated](https://github.com/tidbcloud/tiinf/blob/master/transport/tieventbus/example/gorm_tx_pub/main.go)

[Metrics on gin](https://github.com/tidbcloud/tiinf/blob/master/transport/tieventbus/example/metrics/main.go)

## Where Register the Event Data Structure

https://github.com/tidbcloud/eventbus-datastruct

## Support

If you have any question about TiEventbus, feel free to ask us directly!

supporter: Yitso Zhang / 张怡超