├── CNAME ├── .gitignore ├── docs └── static │ └── kawa.png ├── .github ├── release.yml └── workflows │ ├── release.yml │ └── ci.yml ├── test ├── bench.txt ├── suite_test.go └── stream_test.go ├── .goreleaser.public.yaml ├── go.mod ├── x ├── multi │ ├── multidest.go │ └── multisrc.go ├── memory │ └── memory.go ├── printer │ ├── printer.go │ └── printer_test.go ├── poller │ └── poller.go ├── scanner │ └── scanner.go ├── s3 │ └── s3.go ├── mqtt │ └── mqtt.go └── batcher │ ├── batcher.go │ └── batcher_test.go ├── processor_test.go ├── Makefile ├── processor.go ├── go.sum ├── docker-compose.yml ├── types.go ├── README.md └── LICENSE /CNAME: -------------------------------------------------------------------------------- 1 | www.gokawa.dev 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .coverprofile 2 | dist 3 | -------------------------------------------------------------------------------- /docs/static/kawa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/runreveal/kawa/HEAD/docs/static/kawa.png -------------------------------------------------------------------------------- /.github/release.yml: -------------------------------------------------------------------------------- 1 | changelog: 2 | categories: 3 | - title: 🌱 What's new! 4 | labels: 5 | - feature 6 | - title: 🐆 Optimizations 7 | labels: 8 | - performance 9 | - title: 🛠️ Bugfixes 10 | labels: 11 | - bug 12 | -------------------------------------------------------------------------------- /test/bench.txt: -------------------------------------------------------------------------------- 1 | goos: darwin 2 | goarch: arm64 3 | pkg: github.com/runreveal/kawa/test 4 | BenchmarkMem-10 3 479789639 ns/op 5 | BenchmarkMem-10 3 470265097 ns/op 6 | BenchmarkMem-10 3 468312402 ns/op 7 | BenchmarkMem-10 3 441723403 ns/op 8 | BenchmarkMem-10 3 478304306 ns/op 9 | BenchmarkMem-10 3 481978250 ns/op 10 | BenchmarkMem-10 3 464515333 ns/op 11 | BenchmarkMem-10 3 478364931 ns/op 12 | BenchmarkMem-10 3 478826166 ns/op 13 | BenchmarkMem-10 3 483086972 ns/op 14 | PASS 15 | ok github.com/runreveal/kawa/test 28.571s 16 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | # run only against tags 6 | tags: 7 | - '*' 8 | 9 | permissions: 10 | contents: write 11 | # packages: write 12 | # issues: write 13 | 14 | jobs: 15 | public: 16 | runs-on: macos-latest 17 | steps: 18 | - uses: actions/checkout@v3 19 | with: 20 | fetch-depth: 0 21 | - run: git fetch --force --tags 22 | - uses: actions/setup-go@v4 23 | with: 24 | go-version: stable 25 | # More assembly might be required: Docker logins, GPG, etc. It all depends 26 | # on your needs. 27 | - uses: goreleaser/goreleaser-action@v5 28 | with: 29 | # either 'goreleaser' (default) or 'goreleaser-pro': 30 | distribution: goreleaser 31 | version: latest 32 | args: release --debug --clean -f .goreleaser.public.yaml 33 | env: 34 | GITHUB_TOKEN: ${{ secrets.GO_RELEASER }} 35 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: kawa 5 | 6 | on: 7 | push: 8 | branches: [ main ] 9 | tags: 10 | - '*' 11 | pull_request: 12 | branches: [ main ] 13 | 14 | jobs: 15 | 16 | build: 17 | runs-on: 18 | labels: OSS-Runner 19 | steps: 20 | - uses: actions/checkout@v3 21 | 22 | - name: Set up Go 23 | uses: actions/setup-go@v3 24 | with: 25 | go-version: "1.21" 26 | cache: true 27 | 28 | - name: Start containers 29 | run: docker compose -f "docker-compose.yml" up -d 30 | 31 | - name: Test 32 | run: make test 33 | 34 | - name: Fmtcheck 35 | run: make fmtcheck 36 | 37 | - name: Lint 38 | run: make lint 39 | 40 | - name: Stop containers 41 | run: docker compose -f "docker-compose.yml" down -v 42 | if: always() 43 | -------------------------------------------------------------------------------- /.goreleaser.public.yaml: -------------------------------------------------------------------------------- 1 | # ⚠️ Find delightfully thorough documentation at https://goreleaser.com 2 | 3 | before: 4 | hooks: 5 | # You may remove this if you don't use go modules. 6 | - go mod tidy 7 | # you may remove this if you don't need go generate 8 | # - go generate ./... 9 | 10 | builds: 11 | - skip: true 12 | 13 | 14 | changelog: 15 | use: github-native 16 | 17 | # https://goreleaser.com/customization/release/ 18 | release: 19 | # Repo in which the release will be created. 20 | # Default is extracted from the origin remote URL or empty if its private hosted. 21 | github: 22 | owner: runreveal 23 | name: kawa 24 | 25 | snapshot: 26 | name_template: "{{ incpatch .Version }}-{{ .ShortCommit }}-{{ .Branch }}" 27 | 28 | # The lines beneath this are called `modelines`. See `:help modeline` 29 | # Feel free to remove those if you don't want/use them. 30 | # yaml-language-server: $schema=https://goreleaser.com/static/schema.json 31 | # vim: set ts=2 sw=2 tw=0 fo=cnqoj 32 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/runreveal/kawa 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/aws/aws-sdk-go v1.44.313 7 | github.com/eclipse/paho.mqtt.golang v1.4.3 8 | github.com/pkg/errors v0.9.1 9 | github.com/runreveal/lib/await v0.0.0-20231125014632-fb732b616d27 10 | github.com/segmentio/ksuid v1.0.4 11 | github.com/stretchr/testify v1.8.4 12 | go.opentelemetry.io/otel v1.19.0 13 | go.opentelemetry.io/otel/trace v1.19.0 14 | golang.org/x/sys v0.21.0 15 | ) 16 | 17 | require ( 18 | github.com/davecgh/go-spew v1.1.1 // indirect 19 | github.com/go-logr/logr v1.2.4 // indirect 20 | github.com/go-logr/stdr v1.2.2 // indirect 21 | github.com/gorilla/websocket v1.5.0 // indirect 22 | github.com/jmespath/go-jmespath v0.4.0 // indirect 23 | github.com/pmezard/go-difflib v1.0.0 // indirect 24 | go.opentelemetry.io/otel/metric v1.19.0 // indirect 25 | golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 // indirect 26 | golang.org/x/net v0.26.0 // indirect 27 | golang.org/x/sync v0.3.0 // indirect 28 | gopkg.in/yaml.v3 v3.0.1 // indirect 29 | ) 30 | -------------------------------------------------------------------------------- /x/multi/multidest.go: -------------------------------------------------------------------------------- 1 | package multi 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/runreveal/kawa" 7 | ) 8 | 9 | type MultiDestination[T any] struct { 10 | wrapped []kawa.Destination[T] 11 | } 12 | 13 | // TODO: options for ack behavior? 14 | func NewMultiDestination[T any](dests []kawa.Destination[T]) MultiDestination[T] { 15 | return MultiDestination[T]{ 16 | wrapped: dests, 17 | } 18 | } 19 | 20 | func (md MultiDestination[T]) Send(ctx context.Context, ack func(), msgs ...kawa.Message[T]) error { 21 | if ack != nil { 22 | ack = ackFn(ack, len(md.wrapped)) 23 | } 24 | for _, d := range md.wrapped { 25 | err := d.Send(ctx, ack, msgs...) 26 | if err != nil { 27 | return err 28 | } 29 | } 30 | return nil 31 | } 32 | 33 | // only call ack on last message acknowledgement 34 | func ackFn(ack func(), num int) func() { 35 | ackChu := make(chan struct{}, num-1) 36 | for i := 0; i < num-1; i++ { 37 | ackChu <- struct{}{} 38 | } 39 | // bless you 40 | return func() { 41 | select { 42 | case <-ackChu: 43 | default: 44 | if ack != nil { 45 | ack() 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /processor_test.go: -------------------------------------------------------------------------------- 1 | package kawa_test 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "testing" 7 | 8 | "github.com/runreveal/kawa" 9 | "github.com/runreveal/kawa/x/memory" 10 | ) 11 | 12 | type BinString string 13 | 14 | func (bs *BinString) MarshalBinary() ([]byte, error) { 15 | return []byte(*bs), nil 16 | } 17 | 18 | func (bs *BinString) UnmarshalBinary(bts []byte) error { 19 | *bs = BinString(bts[:]) 20 | return nil 21 | } 22 | 23 | func TestProcessor(t *testing.T) { 24 | inC, outC := make(chan *BinString), make(chan *BinString) 25 | memSrc := memory.MemorySource[*BinString]{ 26 | MsgC: inC, 27 | } 28 | memDst := memory.MemoryDestination[*BinString]{ 29 | MsgC: outC, 30 | } 31 | 32 | countMessages := kawa.HandlerFunc[*BinString, *BinString]( 33 | func(c context.Context, m kawa.Message[*BinString]) ([]kawa.Message[*BinString], error) { 34 | return []kawa.Message[*BinString]{m}, nil 35 | }) 36 | 37 | p, _ := kawa.New[*BinString, *BinString](kawa.Config[*BinString, *BinString]{ 38 | Source: memSrc, 39 | Destination: memDst, 40 | Handler: (countMessages), 41 | }) 42 | 43 | ctx, cancel := context.WithCancel(context.Background()) 44 | defer cancel() 45 | go func() { 46 | err := p.Run(ctx) 47 | fmt.Println(err) 48 | }() 49 | 50 | for i := 0; i < 10; i++ { 51 | bs := BinString("hi") 52 | inC <- &bs 53 | fmt.Println(<-outC) 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | VERSION := $(shell git describe --tags --always --dirty="-dev") 2 | LDFLAGS := -ldflags='-X "main.version=$(VERSION)"' 3 | 4 | GOTESTFLAGS = -race 5 | GOTAGS = testing 6 | 7 | GO ?= $(shell which go) 8 | 9 | export GOEXPERIMENT=nocoverageredesign 10 | 11 | .PHONY: test 12 | test: compose 13 | $(GO) test -vet=off -tags='$(GOTAGS)' $(GOTESTFLAGS) -coverpkg="./..." -coverprofile=.coverprofile ./... 14 | grep -v 'cmd' < .coverprofile > .covprof && mv .covprof .coverprofile 15 | $(GO) tool cover -func=.coverprofile 16 | 17 | .PHONY: coverage 18 | coverage: 19 | $(GO) tool cover -html=.coverprofile 20 | 21 | .PHONY: version 22 | version: 23 | @echo $(VERSION) 24 | 25 | .PHONY: dist 26 | dist: 27 | goreleaser release --config .goreleaser.public.yaml --clean --snapshot 28 | 29 | .PHONY: compose 30 | compose: 31 | docker compose up -d 32 | 33 | .PHONY: lint 34 | lint: $(GOPATH)/bin/golangci-lint 35 | golangci-lint run --timeout 5m ./... 36 | 37 | $(GOPATH)/bin/golangci-lint: 38 | $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2 39 | 40 | $(GOPATH)/bin/golines: 41 | $(GO) install github.com/segmentio/golines@latest 42 | 43 | .PHONY: fmtcheck 44 | fmtcheck: $(GOPATH)/bin/golines 45 | exit $(shell golines -m 128 -l . | wc -l) 46 | 47 | .PHONY: fmtfix 48 | fmtfix: $(GOPATH)/bin/golines 49 | golines -m 128 -w . 50 | 51 | .PHONY: clean 52 | clean: 53 | rm -rf dist 54 | -------------------------------------------------------------------------------- /x/memory/memory.go: -------------------------------------------------------------------------------- 1 | package memory 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/runreveal/kawa" 7 | ) 8 | 9 | // MemorySource is a [kawa.Source] that wraps a channel. 10 | type MemorySource[T any] struct { 11 | MsgC <-chan T 12 | } 13 | 14 | // NewMemSource returns a new [MemorySource]. 15 | func NewMemSource[T any](in <-chan T) MemorySource[T] { 16 | return MemorySource[T]{ 17 | MsgC: in, 18 | } 19 | } 20 | 21 | // Recv implements [kawa.Source] by receiving a message from the channel. 22 | // If ctx.Done() is closed before a value is received, 23 | // then Recv returns ctx.Err(). 24 | func (ms MemorySource[T]) Recv(ctx context.Context) (kawa.Message[T], func(), error) { 25 | select { 26 | case <-ctx.Done(): 27 | return kawa.Message[T]{}, nil, ctx.Err() 28 | case v := <-ms.MsgC: 29 | return kawa.Message[T]{Value: v}, func() {}, nil 30 | } 31 | } 32 | 33 | // MemoryDestination is a [kawa.Destination] that wraps a channel. 34 | type MemoryDestination[T any] struct { 35 | MsgC chan<- T 36 | } 37 | 38 | // NewMemDestination returns a new [MemoryDestination]. 39 | func NewMemDestination[T any](out chan<- T) MemoryDestination[T] { 40 | return MemoryDestination[T]{ 41 | MsgC: out, 42 | } 43 | } 44 | 45 | // Send implements [kawa.Destination] by sending the messages sequentially on the channel. 46 | // If ctx.Done() is closed before all the messages are sent. 47 | // then Send returns ctx.Err(). 48 | // If ack is not nil, then it will be called after all the messages are sent 49 | // but before Send returns. 50 | func (ms MemoryDestination[T]) Send(ctx context.Context, ack func(), msgs ...kawa.Message[T]) error { 51 | for _, msg := range msgs { 52 | select { 53 | case <-ctx.Done(): 54 | return ctx.Err() 55 | case ms.MsgC <- msg.Value: 56 | } 57 | } 58 | kawa.Ack(ack) 59 | return nil 60 | } 61 | -------------------------------------------------------------------------------- /x/printer/printer.go: -------------------------------------------------------------------------------- 1 | package printer 2 | 3 | import ( 4 | "context" 5 | "io" 6 | "slices" 7 | 8 | "github.com/runreveal/kawa" 9 | ) 10 | 11 | // A Printer is a [kawa.Destination] that serializes []byte messages 12 | // to an [io.Writer]. 13 | type Printer struct { 14 | delim []byte 15 | 16 | mu chan struct{} // send for lock, receive for unlock 17 | buf []byte 18 | writer io.Writer 19 | } 20 | 21 | // Option is an optional argument to [NewPrinter]. 22 | type Option func(*Printer) 23 | 24 | // WithDelim changes the sequence written after every message 25 | // from the default newline ("\n") to the given byte sequence. 26 | func WithDelim(delim []byte) Option { 27 | return func(p *Printer) { 28 | // Defensive copy. 29 | p.delim = slices.Clone(delim) 30 | } 31 | } 32 | 33 | // NewPrinter returns a new [Printer] that writes to the given writer. 34 | func NewPrinter(writer io.Writer, opts ...Option) *Printer { 35 | ret := &Printer{ 36 | writer: writer, 37 | delim: []byte("\n"), 38 | mu: make(chan struct{}, 1), 39 | } 40 | for _, opt := range opts { 41 | opt(ret) 42 | } 43 | return ret 44 | } 45 | 46 | // Send implements [kawa.Destination] by writing each message value followed by the printer's delimiter. 47 | // A call to Send will call Write at most once on the underlying writer. 48 | func (p *Printer) Send(ctx context.Context, ack func(), msg ...kawa.Message[[]byte]) error { 49 | n := len(p.delim) * len(msg) 50 | for _, m := range msg { 51 | n += len(m.Value) 52 | } 53 | 54 | select { 55 | case p.mu <- struct{}{}: 56 | case <-ctx.Done(): 57 | return ctx.Err() 58 | } 59 | p.buf = slices.Grow(p.buf[:0], n) 60 | for _, m := range msg { 61 | p.buf = append(p.buf, m.Value...) 62 | p.buf = append(p.buf, p.delim...) 63 | } 64 | _, err := p.writer.Write(p.buf) 65 | <-p.mu // Release mutex as soon as possible after Write. 66 | 67 | if err == nil { 68 | kawa.Ack(ack) 69 | } 70 | return err 71 | } 72 | -------------------------------------------------------------------------------- /x/poller/poller.go: -------------------------------------------------------------------------------- 1 | package poller 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | 7 | "github.com/runreveal/kawa" 8 | ) 9 | 10 | type msgAck[T any] struct { 11 | msg kawa.Message[T] 12 | ack func() 13 | } 14 | 15 | type Poller[T any] interface { 16 | Poll(context.Context, int) ([]kawa.Message[T], func(), error) 17 | } 18 | 19 | type Source[T any] struct { 20 | msgChan chan msgAck[T] 21 | batchSize int 22 | 23 | poller Poller[T] 24 | } 25 | 26 | func WithBatchSize(size int) func(*Opts) { 27 | return func(opts *Opts) { 28 | opts.BatchSize = size 29 | } 30 | } 31 | 32 | type Opts struct { 33 | BatchSize int 34 | } 35 | 36 | type Option func(*Opts) 37 | 38 | func New[T any](p Poller[T], opts ...Option) *Source[T] { 39 | var cfg Opts 40 | for _, o := range opts { 41 | o(&cfg) 42 | } 43 | ret := &Source[T]{ 44 | poller: p, 45 | msgChan: make(chan msgAck[T], cfg.BatchSize), 46 | batchSize: cfg.BatchSize, 47 | } 48 | return ret 49 | } 50 | 51 | func (s *Source[T]) Run(ctx context.Context) error { 52 | return s.recvLoop(ctx) 53 | } 54 | 55 | func (s *Source[T]) recvLoop(ctx context.Context) error { 56 | for { 57 | msgs, ack, err := s.poller.Poll(ctx, s.batchSize) 58 | if err != nil { 59 | return err 60 | } 61 | 62 | ackFn := ackLast(ack, len(msgs)) 63 | 64 | for _, m := range msgs { 65 | select { 66 | case <-ctx.Done(): 67 | return ctx.Err() 68 | case s.msgChan <- msgAck[T]{msg: m, ack: ackFn}: 69 | } 70 | } 71 | } 72 | } 73 | 74 | func (s *Source[T]) Recv(ctx context.Context) (kawa.Message[T], func(), error) { 75 | select { 76 | case <-ctx.Done(): 77 | return kawa.Message[T]{}, nil, ctx.Err() 78 | case ma := <-s.msgChan: 79 | return ma.msg, ma.ack, errors.New("not implemented") 80 | } 81 | } 82 | 83 | // only call ack on last message acknowledgement 84 | func ackLast(ack func(), num int) func() { 85 | ackChu := make(chan struct{}, num-1) 86 | for i := 0; i < num-1; i++ { 87 | ackChu <- struct{}{} 88 | } 89 | // bless you 90 | return func() { 91 | select { 92 | case <-ackChu: 93 | default: 94 | if ack != nil { 95 | ack() 96 | } 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /x/multi/multisrc.go: -------------------------------------------------------------------------------- 1 | package multi 2 | 3 | import ( 4 | "context" 5 | "sync" 6 | 7 | "github.com/runreveal/kawa" 8 | ) 9 | 10 | type msgAck[T any] struct { 11 | msg kawa.Message[T] 12 | ack func() 13 | } 14 | 15 | // MultiSource multiplexes multiple sources into one. It was thrown together 16 | // quickly and may have some performance issues. It definitely needs some work 17 | // on proper error handling, and concurrency issues on closing. 18 | type MultiSource[T any] struct { 19 | wrapped []kawa.Source[T] 20 | msgAckC chan msgAck[T] 21 | } 22 | 23 | // TODO: options for ack behavior? 24 | func NewMultiSource[T any](sources []kawa.Source[T]) MultiSource[T] { 25 | return MultiSource[T]{ 26 | wrapped: sources, 27 | msgAckC: make(chan msgAck[T]), 28 | } 29 | } 30 | 31 | // Run assumes the wrapped sources are already running, it spawns a go-routine 32 | // for each source being wrapped, and in a loop reads its Recv method, then 33 | // makes that message available on the Recv method for the multi source. 34 | // Sources will be "competing" to send events on the shared channel, which 35 | // means that faster sources have the potential to "starve" slower ones. At 36 | // our current scale, this shouldn't be an issue. 37 | func (ms MultiSource[T]) Run(ctx context.Context) error { 38 | var wg sync.WaitGroup 39 | 40 | errc := make(chan error, len(ms.wrapped)) 41 | 42 | for _, src := range ms.wrapped { 43 | wg.Add(1) 44 | go func(src kawa.Source[T]) { 45 | defer wg.Done() 46 | for { 47 | msg, ack, err := src.Recv(ctx) 48 | if err != nil { 49 | select { 50 | case errc <- err: 51 | case <-ctx.Done(): 52 | return 53 | } 54 | } 55 | select { 56 | case ms.msgAckC <- msgAck[T]{msg: msg, ack: ack}: 57 | case <-ctx.Done(): 58 | return 59 | } 60 | } 61 | }(src) 62 | } 63 | 64 | var err error 65 | select { 66 | case <-ctx.Done(): 67 | err = ctx.Err() 68 | case err = <-errc: 69 | } 70 | wg.Wait() 71 | return err 72 | } 73 | 74 | func (ms MultiSource[T]) Recv(ctx context.Context) (kawa.Message[T], func(), error) { 75 | select { 76 | case ma := <-ms.msgAckC: 77 | return ma.msg, ma.ack, nil 78 | case <-ctx.Done(): 79 | return kawa.Message[T]{}, nil, ctx.Err() 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /x/printer/printer_test.go: -------------------------------------------------------------------------------- 1 | package printer 2 | 3 | import ( 4 | "context" 5 | "strings" 6 | "testing" 7 | 8 | "github.com/runreveal/kawa" 9 | ) 10 | 11 | func TestPrinter(t *testing.T) { 12 | tests := []struct { 13 | name string 14 | delim string 15 | sends [][]string 16 | want string 17 | }{ 18 | { 19 | name: "NoSends", 20 | delim: "\n", 21 | sends: [][]string{}, 22 | want: "", 23 | }, 24 | { 25 | name: "SingleMessageDefaultDelimiter", 26 | delim: "\n", 27 | sends: [][]string{{"Hello, World"}}, 28 | want: "Hello, World\n", 29 | }, 30 | { 31 | name: "SingleMessageCustomDelimiter", 32 | delim: "|||", 33 | sends: [][]string{{"Hello, World"}}, 34 | want: "Hello, World|||", 35 | }, 36 | { 37 | name: "MultipleMessagesSingleBatch", 38 | delim: "\n", 39 | sends: [][]string{{"abc", "def"}}, 40 | want: "abc\ndef\n", 41 | }, 42 | { 43 | name: "MultipleBatchesWithSingleMessage", 44 | delim: "\n", 45 | sends: [][]string{{"abc"}, {"def"}}, 46 | want: "abc\ndef\n", 47 | }, 48 | { 49 | name: "MultipleBatchesWithMultipleMessages", 50 | delim: "\n", 51 | sends: [][]string{{"abc", "def"}, {"ghi", "jkl"}}, 52 | want: "abc\ndef\nghi\njkl\n", 53 | }, 54 | } 55 | 56 | for _, test := range tests { 57 | t.Run(test.name, func(t *testing.T) { 58 | ctx := context.Background() 59 | var options []Option 60 | if test.delim != "\n" { 61 | options = append(options, WithDelim([]byte(test.delim))) 62 | } 63 | buf := new(strings.Builder) 64 | p := NewPrinter(buf, options...) 65 | 66 | for i, batch := range test.sends { 67 | batchMessages := make([]kawa.Message[[]byte], len(batch)) 68 | for i, s := range batch { 69 | batchMessages[i] = kawa.Message[[]byte]{ 70 | Value: []byte(s), 71 | } 72 | } 73 | callCount := 0 74 | err := p.Send(ctx, func() { callCount++ }, batchMessages...) 75 | if err != nil { 76 | t.Errorf("Error sending batch #%d: %v", i+1, err) 77 | } 78 | if callCount != 1 { 79 | t.Errorf("ack function called %d times during batch #%d; want 1", callCount, i+1) 80 | } 81 | } 82 | 83 | if got := buf.String(); got != test.want { 84 | t.Errorf("wrote %q; want %q", got, test.want) 85 | } 86 | }) 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /x/scanner/scanner.go: -------------------------------------------------------------------------------- 1 | package scanner 2 | 3 | import ( 4 | "bufio" 5 | "bytes" 6 | "context" 7 | "fmt" 8 | "io" 9 | "sync" 10 | 11 | "github.com/runreveal/kawa" 12 | ) 13 | 14 | type Scanner struct { 15 | scanner *bufio.Scanner 16 | msgC chan kawa.MsgAck[[]byte] 17 | delim []byte 18 | } 19 | 20 | func WithDelim(delim []byte) func(*Scanner) { 21 | return func(s *Scanner) { 22 | s.delim = delim 23 | } 24 | } 25 | 26 | func NewScanner(reader io.Reader, opts ...func(*Scanner)) *Scanner { 27 | ret := &Scanner{ 28 | scanner: bufio.NewScanner(reader), 29 | msgC: make(chan kawa.MsgAck[[]byte]), 30 | delim: []byte("\n"), 31 | } 32 | for _, opt := range opts { 33 | opt(ret) 34 | } 35 | return ret 36 | } 37 | 38 | func (s *Scanner) Run(ctx context.Context) error { 39 | return s.recvLoop(ctx) 40 | } 41 | 42 | func (s *Scanner) recvLoop(ctx context.Context) error { 43 | var wg sync.WaitGroup 44 | s.scanner.Split(delimFunc(s.delim)) 45 | for s.scanner.Scan() { 46 | bts := s.scanner.Bytes() 47 | val := make([]byte, len(bts)) 48 | copy(val, bts) 49 | wg.Add(1) 50 | select { 51 | case s.msgC <- kawa.MsgAck[[]byte]{ 52 | Msg: kawa.Message[[]byte]{Value: val}, 53 | Ack: func() { 54 | wg.Done() 55 | }, 56 | }: 57 | case <-ctx.Done(): 58 | return ctx.Err() 59 | } 60 | } 61 | 62 | if err := s.scanner.Err(); err != nil { 63 | return fmt.Errorf("scanning: %+w", err) 64 | } 65 | 66 | c := make(chan struct{}) 67 | go func() { 68 | wg.Wait() 69 | close(c) 70 | }() 71 | 72 | select { 73 | case <-c: 74 | case <-ctx.Done(): 75 | return ctx.Err() 76 | } 77 | 78 | return nil 79 | } 80 | 81 | func (s *Scanner) Recv(ctx context.Context) (kawa.Message[[]byte], func(), error) { 82 | select { 83 | case <-ctx.Done(): 84 | return kawa.Message[[]byte]{}, nil, ctx.Err() 85 | case pass := <-s.msgC: 86 | return pass.Msg, pass.Ack, nil 87 | } 88 | } 89 | 90 | func delimFunc(delim []byte) func(data []byte, atEOF bool) (advance int, token []byte, err error) { 91 | return func(data []byte, atEOF bool) (advance int, token []byte, err error) { 92 | if atEOF && len(data) == 0 { 93 | return 0, nil, nil 94 | } 95 | if i := bytes.Index(data, delim); i >= 0 { 96 | return i + len(delim), data[0:i], nil 97 | } 98 | // If we're at EOF, we have a final, non-terminated line. Return it. 99 | if atEOF { 100 | return len(data), data, nil 101 | } 102 | // Request more data. 103 | return 0, nil, nil 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /test/suite_test.go: -------------------------------------------------------------------------------- 1 | package kawa_test 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "errors" 7 | "math/rand" 8 | "testing" 9 | "time" 10 | 11 | "github.com/runreveal/kawa" 12 | "github.com/runreveal/lib/await" 13 | "github.com/stretchr/testify/assert" 14 | ) 15 | 16 | func SuiteTest(t *testing.T, src kawa.Source[[]byte], dst kawa.Destination[[]byte]) { 17 | wait := await.New() 18 | want := make([][]byte, 25) 19 | seen := make([]bool, 25) 20 | rng := rand.New(rand.NewSource(time.Now().UnixNano())) 21 | for i := range want { 22 | want[i] = make([]byte, 20) 23 | _, err := rng.Read(want[i]) 24 | assert.NoError(t, err) 25 | } 26 | 27 | if runnner, ok := src.(await.Runner); ok { 28 | wait.Add(runnner) 29 | } 30 | if runnner, ok := dst.(await.Runner); ok { 31 | wait.Add(runnner) 32 | } 33 | 34 | // stdoutDumper := hex.Dumper(os.Stdout) 35 | // defer stdoutDumper.Close() 36 | // for _, line := range want { 37 | // stdoutDumper.Write([]byte(line)) 38 | // } 39 | 40 | wait.Add(await.RunFunc(func(ctx context.Context) error { 41 | count := 0 42 | for { 43 | msg, ack, err := src.Recv(ctx) 44 | if !errors.Is(err, context.Canceled) { 45 | assert.NoError(t, err) 46 | } 47 | if ack != nil { 48 | ack() 49 | } 50 | // fmt.Println("received:") 51 | // stdoutDumper.Write([]byte(msg.Value)) 52 | // fmt.Printf("\n") 53 | mark(t, msg.Value, want, seen) 54 | count++ 55 | if count == len(want) { 56 | break 57 | } 58 | } 59 | return nil 60 | })) 61 | 62 | wait.Add(await.RunFunc(func(ctx context.Context) error { 63 | for i := range want { 64 | toSend := make([]byte, len(want[i])) 65 | copy(toSend, want[i]) 66 | // fmt.Println("sent:") 67 | // stdoutDumper.Write(toSend) 68 | // fmt.Printf("\n") 69 | err := dst.Send(ctx, nil, kawa.Message[[]byte]{Value: toSend}) 70 | if !errors.Is(err, context.Canceled) { 71 | assert.NoError(t, err) 72 | } 73 | } 74 | // Wait until the source exits. 75 | <-ctx.Done() 76 | return nil 77 | })) 78 | 79 | ctx, cncl := context.WithTimeout(context.Background(), 5*time.Second) 80 | defer cncl() 81 | 82 | err := wait.Run(ctx) 83 | assert.NoError(t, err) 84 | 85 | for i := range seen { 86 | assert.True(t, seen[i], "we should have seen all messages") 87 | } 88 | } 89 | 90 | type aide interface { 91 | assert.TestingT 92 | Helper() 93 | } 94 | 95 | func mark(t aide, actual []byte, sent [][]byte, seen []bool) { 96 | t.Helper() 97 | for i, want := range sent { 98 | if bytes.Equal(actual, want) { 99 | assert.False(t, seen[i], "we shouldn't see duplicates") 100 | seen[i] = true 101 | return 102 | } 103 | } 104 | } 105 | 106 | func BuildBench(b *testing.B, count int, src kawa.Source[[]byte], dst kawa.Destination[[]byte]) await.Runner { 107 | wait := await.New() 108 | want := make([][]byte, 25) 109 | rng := rand.New(rand.NewSource(time.Now().UnixNano())) 110 | for i := range want { 111 | want[i] = make([]byte, 20) 112 | _, err := rng.Read(want[i]) 113 | assert.NoError(b, err) 114 | } 115 | 116 | if runnner, ok := src.(await.Runner); ok { 117 | wait.Add(runnner) 118 | } 119 | if runnner, ok := dst.(await.Runner); ok { 120 | wait.Add(runnner) 121 | } 122 | 123 | wait.Add(await.RunFunc(func(ctx context.Context) error { 124 | seen := 0 125 | for { 126 | _, ack, err := src.Recv(ctx) 127 | if !errors.Is(err, context.Canceled) { 128 | assert.NoError(b, err) 129 | } 130 | if ack != nil { 131 | ack() 132 | } 133 | seen++ 134 | if seen == count { 135 | break 136 | } 137 | } 138 | return nil 139 | })) 140 | 141 | wait.Add(await.RunFunc(func(ctx context.Context) error { 142 | for i := 0; i < count; i++ { 143 | toSend := want[i%len(want)] 144 | err := dst.Send(ctx, nil, kawa.Message[[]byte]{Value: toSend}) 145 | if !errors.Is(err, context.Canceled) { 146 | assert.NoError(b, err) 147 | } 148 | } 149 | // Wait until the source exits. 150 | <-ctx.Done() 151 | return nil 152 | })) 153 | 154 | return wait 155 | } 156 | -------------------------------------------------------------------------------- /x/s3/s3.go: -------------------------------------------------------------------------------- 1 | package s3 2 | 3 | import ( 4 | "bytes" 5 | "compress/gzip" 6 | "context" 7 | "errors" 8 | "fmt" 9 | "time" 10 | 11 | "github.com/aws/aws-sdk-go/aws" 12 | "github.com/aws/aws-sdk-go/aws/credentials" 13 | "github.com/aws/aws-sdk-go/aws/session" 14 | "github.com/aws/aws-sdk-go/service/s3/s3manager" 15 | "github.com/runreveal/kawa" 16 | batch "github.com/runreveal/kawa/x/batcher" 17 | "github.com/segmentio/ksuid" 18 | ) 19 | 20 | type Option func(*S3) 21 | 22 | func WithBucketName(bucketName string) Option { 23 | return func(s *S3) { 24 | s.bucketName = bucketName 25 | } 26 | } 27 | 28 | func WithPathPrefix(pathPrefix string) Option { 29 | return func(s *S3) { 30 | s.pathPrefix = pathPrefix 31 | } 32 | } 33 | 34 | func WithBucketRegion(bucketRegion string) Option { 35 | return func(s *S3) { 36 | s.bucketRegion = bucketRegion 37 | } 38 | } 39 | 40 | func WithCustomEndpoint(customEndpoint string) Option { 41 | return func(s *S3) { 42 | s.customEndpoint = customEndpoint 43 | } 44 | } 45 | 46 | func WithAccessKeyID(accessKeyID string) Option { 47 | return func(s *S3) { 48 | s.accessKeyID = accessKeyID 49 | } 50 | } 51 | 52 | func WithSecretAccessKey(secretAccessKey string) Option { 53 | return func(s *S3) { 54 | s.secretAccessKey = secretAccessKey 55 | } 56 | } 57 | 58 | func WithBatchSize(batchSize int) Option { 59 | return func(s *S3) { 60 | s.batchSize = batchSize 61 | } 62 | } 63 | 64 | type S3 struct { 65 | batcher *batch.Destination[[]byte] 66 | 67 | bucketName string 68 | bucketRegion string 69 | pathPrefix string 70 | 71 | customEndpoint string 72 | accessKeyID string 73 | secretAccessKey string 74 | 75 | batchSize int 76 | } 77 | 78 | func New(opts ...Option) *S3 { 79 | ret := &S3{} 80 | for _, o := range opts { 81 | o(ret) 82 | } 83 | if ret.batchSize == 0 { 84 | ret.batchSize = 100 85 | } 86 | ret.batcher = batch.NewDestination[[]byte](ret, 87 | batch.Raise[[]byte](), 88 | batch.FlushLength(ret.batchSize), 89 | batch.FlushFrequency(5*time.Second), 90 | ) 91 | return ret 92 | } 93 | 94 | func (s *S3) Run(ctx context.Context) error { 95 | if s.bucketName == "" { 96 | return errors.New("missing bucket name") 97 | } 98 | 99 | return s.batcher.Run(ctx) 100 | } 101 | 102 | func (s *S3) Send(ctx context.Context, ack func(), msgs ...kawa.Message[[]byte]) error { 103 | return s.batcher.Send(ctx, ack, msgs...) 104 | } 105 | 106 | // Flush sends the given messages of type kawa.Message[type.Event] to an s3 bucket 107 | func (s *S3) Flush(ctx context.Context, msgs []kawa.Message[[]byte]) error { 108 | 109 | // We need a handle a variety of arguments specifically the way 110 | // they are presented within the config file. If we don't some 111 | // s3 compatible services will not work correctly, like R2. 112 | var config = &aws.Config{} 113 | if s.customEndpoint != "" { 114 | config.Endpoint = aws.String(s.customEndpoint) 115 | } 116 | if s.accessKeyID != "" && s.secretAccessKey != "" { 117 | config.Credentials = credentials.NewStaticCredentials(s.accessKeyID, s.secretAccessKey, "") 118 | } 119 | if s.bucketRegion != "" { 120 | config.Region = aws.String(s.bucketRegion) 121 | } 122 | sess, err := session.NewSession(config) 123 | if err != nil { 124 | return err 125 | } 126 | uploader := s3manager.NewUploader(sess) 127 | 128 | var buf bytes.Buffer 129 | gzipBuffer := gzip.NewWriter(&buf) 130 | for _, msg := range msgs { 131 | _, err := gzipBuffer.Write(msg.Value) 132 | if err != nil { 133 | return err 134 | } 135 | _, err = gzipBuffer.Write([]byte("\n")) 136 | if err != nil { 137 | return err 138 | } 139 | } 140 | if err := gzipBuffer.Close(); err != nil { 141 | return err 142 | } 143 | key := fmt.Sprintf("%s/%s/%s_%d.gz", 144 | s.pathPrefix, 145 | time.Now().UTC().Format("2006/01/02/15"), 146 | ksuid.New().String(), 147 | time.Now().Unix(), 148 | ) 149 | 150 | uploadInput := &s3manager.UploadInput{ 151 | Bucket: aws.String(s.bucketName), 152 | Key: aws.String(key), 153 | Body: bytes.NewReader(buf.Bytes()), 154 | } 155 | 156 | // Upload the file to S3 157 | _, err = uploader.Upload(uploadInput) 158 | if err != nil { 159 | return err 160 | } 161 | return nil 162 | } 163 | -------------------------------------------------------------------------------- /processor.go: -------------------------------------------------------------------------------- 1 | package kawa 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | "sync" 8 | 9 | "go.opentelemetry.io/otel" 10 | "go.opentelemetry.io/otel/trace" 11 | ) 12 | 13 | var tracer trace.Tracer 14 | 15 | func init() { 16 | tracer = otel.Tracer("kawa/processor") 17 | } 18 | 19 | type Processor[T1, T2 any] struct { 20 | src Source[T1] 21 | dst Destination[T2] 22 | handler Handler[T1, T2] 23 | parallelism int 24 | tracing bool 25 | metrics bool 26 | } 27 | 28 | type Config[T1, T2 any] struct { 29 | Source Source[T1] 30 | Destination Destination[T2] 31 | Handler Handler[T1, T2] 32 | } 33 | 34 | type Option func(*Options) 35 | 36 | type Options struct { 37 | Parallelism int 38 | Tracing bool 39 | Metrics bool 40 | } 41 | 42 | func Parallelism(n int) func(*Options) { 43 | return func(o *Options) { 44 | o.Parallelism = n 45 | } 46 | } 47 | 48 | func Tracing(b bool) func(*Options) { 49 | return func(o *Options) { 50 | o.Tracing = b 51 | } 52 | } 53 | 54 | func Metrics(b bool) func(*Options) { 55 | return func(o *Options) { 56 | o.Metrics = b 57 | } 58 | } 59 | 60 | // New instantiates a new Processor. `Processor.Run` must be called after calling `New` 61 | // before events will be processed. 62 | func New[T1, T2 any](c Config[T1, T2], opts ...Option) (*Processor[T1, T2], error) { 63 | if c.Source == nil || c.Destination == nil { 64 | return nil, errors.New("both Source and Destination required") 65 | } 66 | if c.Handler == nil { 67 | return nil, errors.New("handler required. Have you considered kawa.Pipe?") 68 | } 69 | var op Options 70 | for _, o := range opts { 71 | o(&op) 72 | } 73 | p := &Processor[T1, T2]{ 74 | src: c.Source, 75 | dst: c.Destination, 76 | handler: c.Handler, 77 | parallelism: op.Parallelism, 78 | tracing: op.Tracing, 79 | metrics: op.Metrics, 80 | } 81 | 82 | if p.parallelism < 1 { 83 | p.parallelism = 1 84 | } 85 | return p, nil 86 | } 87 | 88 | // handle runs the loop to receive, process and send messages. 89 | func (p *Processor[T1, T2]) handle(ctx context.Context) error { 90 | for { 91 | ctx, handleSpan := tracer.Start(ctx, "kawa.processor.full") 92 | 93 | rctx, recvSpan := tracer.Start(ctx, "kawa.processor.src.recv") 94 | msg, ack, err := p.src.Recv(rctx) 95 | if err != nil { 96 | return fmt.Errorf("source: %w", err) 97 | } 98 | recvSpan.End() 99 | 100 | hctx, hdlSpan := tracer.Start(ctx, "kawa.processor.handler.handle") 101 | msgs, err := p.handler.Handle(hctx, msg) 102 | if err != nil { 103 | return fmt.Errorf("handler: %w", err) 104 | } 105 | hdlSpan.End() 106 | 107 | // If there are no messages, we don't need to send nil to destination 108 | if len(msgs) == 0 { 109 | handleSpan.End() 110 | Ack(ack) 111 | continue 112 | } 113 | 114 | sctx, sendSpan := tracer.Start(ctx, "kawa.processor.dst.send") 115 | err = p.dst.Send(sctx, ack, msgs...) 116 | if err != nil { 117 | return fmt.Errorf("destination: %w", err) 118 | } 119 | sendSpan.End() 120 | handleSpan.End() 121 | } 122 | } 123 | 124 | // Run is a blocking call, and runs until either the ctx is canceled, or an 125 | // unrecoverable error is encountered. If any error is returned from a source, 126 | // destination or the handler func, then it's wrapped and returned. If the 127 | // passed-in context is canceled, this will not return the context.Canceled 128 | // error to indicate a clean shutdown was successful. Run will return 129 | // ctx.Err() in other cases where context termination leads to shutdown of the 130 | // processor. 131 | func (p *Processor[T1, T2]) Run(ctx context.Context) error { 132 | var wg sync.WaitGroup 133 | wg.Add(p.parallelism) 134 | ctx, cancel := context.WithCancel(ctx) 135 | errc := make(chan error, p.parallelism) 136 | 137 | for i := 0; i < p.parallelism; i++ { 138 | go func(c context.Context) { 139 | if e := p.handle(c); e != nil { 140 | errc <- e 141 | } 142 | wg.Done() 143 | }(ctx) 144 | } 145 | 146 | var err error 147 | select { 148 | case <-ctx.Done(): 149 | // context was stopped by parent's cancel or parent timeout. 150 | // set err = ctx.Err() *only if* error is *not* context.Canceled, 151 | // because our contract defines that to be the way callers should stop 152 | // a worker cleanly. 153 | if !errors.Is(ctx.Err(), context.Canceled) { 154 | err = ctx.Err() 155 | } 156 | case err = <-errc: 157 | // All errors are fatal to this worker 158 | err = fmt.Errorf("worker: %w", err) 159 | } 160 | // Stop all the workers on shutdown. 161 | cancel() 162 | // TODO: capture errors thrown during shutdown? if we do this, write local 163 | // err first. it represents first seen 164 | wg.Wait() 165 | close(errc) 166 | return err 167 | } 168 | -------------------------------------------------------------------------------- /test/stream_test.go: -------------------------------------------------------------------------------- 1 | package kawa_test 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "io" 7 | "sync" 8 | "testing" 9 | "time" 10 | 11 | "github.com/runreveal/kawa" 12 | "github.com/runreveal/kawa/x/memory" 13 | "github.com/runreveal/kawa/x/mqtt" 14 | 15 | "github.com/runreveal/kawa/x/printer" 16 | "github.com/runreveal/kawa/x/scanner" 17 | "github.com/segmentio/ksuid" 18 | "github.com/stretchr/testify/assert" 19 | "github.com/stretchr/testify/require" 20 | ) 21 | 22 | func TestMem(t *testing.T) { 23 | schan := make(chan []byte) 24 | src := memory.NewMemSource((<-chan []byte)(schan)) 25 | dst := memory.NewMemDestination[[]byte]((chan<- []byte)(schan)) 26 | SuiteTest(t, src, dst) 27 | } 28 | 29 | func BenchmarkMem(b *testing.B) { 30 | schan := make(chan []byte) 31 | b.ResetTimer() 32 | for i := 0; i < b.N; i++ { 33 | b.StopTimer() 34 | src := memory.NewMemSource((<-chan []byte)(schan)) 35 | dst := memory.NewMemDestination[[]byte]((chan<- []byte)(schan)) 36 | runner := BuildBench(b, 1000000, src, dst) 37 | b.StartTimer() 38 | err := runner.Run(context.Background()) 39 | assert.NoError(b, err) 40 | } 41 | } 42 | 43 | func TestIO(t *testing.T) { 44 | reader, writer := io.Pipe() 45 | // Delim should be a string of bytes which is unlikely occur randomly 46 | scansrc := scanner.NewScanner(reader, scanner.WithDelim([]byte("0x0x0x0x0"))) 47 | printdst := printer.NewPrinter(writer, printer.WithDelim([]byte("0x0x0x0x0"))) 48 | time.AfterFunc(100*time.Millisecond, func() { 49 | // close the writer to signal the end of the stream 50 | // there's no easy way to do this implicitly without making 51 | // readers/writers into closers. Maybe that's worth it? 52 | writer.Close() 53 | }) 54 | SuiteTest(t, scansrc, printdst) 55 | } 56 | 57 | func TestMQTT(t *testing.T) { 58 | mqttOpts := []mqtt.Option{ 59 | mqtt.WithBroker("mqtt://localhost:1883"), 60 | mqtt.WithTopic("kawa/topic"), 61 | mqtt.WithKeepAlive(5 * time.Second), 62 | mqtt.WithQOS(2), 63 | } 64 | 65 | // mqttSrc.ERROR = log.New(os.Stdout, "[ERROR] ", 0) 66 | // mqttSrc.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0) 67 | // mqttSrc.WARN = log.New(os.Stdout, "[WARN] ", 0) 68 | // mqttSrc.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0) 69 | 70 | mqttsrc, err := mqtt.NewSource(append(mqttOpts, mqtt.WithClientID(ksuid.New().String()))...) 71 | require.NoError(t, err, "source create should not error") 72 | mqttdst, err := mqtt.NewDestination(append(mqttOpts, mqtt.WithClientID(ksuid.New().String()))...) 73 | require.NoError(t, err, "dest create should not error") 74 | SuiteTest(t, mqttsrc, mqttdst) 75 | } 76 | 77 | // Old tests 78 | 79 | type BinString string 80 | 81 | func (b *BinString) UnmarshalBinary(in []byte) error { 82 | *b = BinString(in) 83 | return nil 84 | } 85 | 86 | func (b BinString) MarshalBinary() ([]byte, error) { 87 | return []byte(b), nil 88 | } 89 | 90 | func TestSmokeHappyPath(t *testing.T) { 91 | schan := make(chan *BinString) 92 | src := memory.NewMemSource((<-chan *BinString)(schan)) 93 | dst := memory.NewMemDestination[*BinString]((chan<- *BinString)(schan)) 94 | var wg sync.WaitGroup 95 | wg.Add(10) 96 | 97 | go func() { 98 | for i := 0; i < 10; i++ { 99 | x := BinString(fmt.Sprintf("hi-%d", i)) 100 | err := dst.Send(context.TODO(), nil, kawa.Message[*BinString]{Value: &x}) 101 | if err != nil { 102 | t.Log(err) 103 | } 104 | } 105 | }() 106 | go func(t *testing.T) { 107 | t.Helper() 108 | for i := 0; i < 10; i++ { 109 | msg, _, err := src.Recv(context.TODO()) 110 | if err != nil { 111 | t.Errorf("%v", err) 112 | } 113 | fmt.Printf("%s\n", *msg.Value) 114 | wg.Done() 115 | } 116 | }(t) 117 | wg.Wait() 118 | } 119 | 120 | // TestWow shows an example of creating a source without having to consume a 121 | // generic implementation of a source this of course assumes the source is 122 | // aware of the type that you're looking to surface to the pipeline 123 | func TestWow(t *testing.T) { 124 | schan := make(chan *BinString) 125 | src := NewMemSource((<-chan *BinString)(schan)) 126 | dst := memory.NewMemDestination[*BinString]((chan<- *BinString)(schan)) 127 | var wg sync.WaitGroup 128 | wg.Add(10) 129 | 130 | go func() { 131 | for i := 0; i < 10; i++ { 132 | x := BinString(fmt.Sprintf("wow-%d", i)) 133 | err := dst.Send(context.TODO(), nil, kawa.Message[*BinString]{Value: &x}) 134 | if err != nil { 135 | t.Log(err) 136 | } 137 | } 138 | }() 139 | go func(t *testing.T) { 140 | t.Helper() 141 | for i := 0; i < 10; i++ { 142 | msg, _, err := src.Recv(context.TODO()) 143 | if err != nil { 144 | t.Errorf("%v", err) 145 | } 146 | fmt.Printf("%s\n", *msg.Value) 147 | wg.Done() 148 | } 149 | }(t) 150 | wg.Wait() 151 | } 152 | 153 | type MemorySource struct { 154 | MsgC <-chan *BinString 155 | } 156 | 157 | func NewMemSource(in <-chan *BinString) MemorySource { 158 | return MemorySource{ 159 | MsgC: in, 160 | } 161 | } 162 | 163 | func (ms MemorySource) Recv(ctx context.Context) (kawa.Message[*BinString], func(), error) { 164 | select { 165 | case <-ctx.Done(): 166 | return kawa.Message[*BinString]{}, nil, ctx.Err() 167 | case v := <-ms.MsgC: 168 | return kawa.Message[*BinString]{Value: v}, nil, nil 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/aws/aws-sdk-go v1.44.313 h1:u6EuNQqgAmi09GEZ5g/XGHLF0XV31WcdU5rnHyIBHBc= 2 | github.com/aws/aws-sdk-go v1.44.313/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= 3 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 5 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 6 | github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik= 7 | github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE= 8 | github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= 9 | github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= 10 | github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= 11 | github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= 12 | github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= 13 | github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= 14 | github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 15 | github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= 16 | github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 17 | github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= 18 | github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= 19 | github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= 20 | github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= 21 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 22 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 23 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 24 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 25 | github.com/runreveal/lib/await v0.0.0-20231125014632-fb732b616d27 h1:utfCzMxw9cJZqrpbunKCa/0epOGT2eb+UyoCSBQOnh8= 26 | github.com/runreveal/lib/await v0.0.0-20231125014632-fb732b616d27/go.mod h1:Gyj90y+175aa23yMbbml4zvGuIZj32GOe3qx7wMRgoo= 27 | github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c= 28 | github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE= 29 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 30 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 31 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 32 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 33 | go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs= 34 | go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY= 35 | go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE= 36 | go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8= 37 | go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg= 38 | go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo= 39 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 40 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 41 | golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 h1:/yRP+0AN7mf5DkD3BAI6TOFnd51gEoDEb8o35jIFtgw= 42 | golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= 43 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 44 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 45 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 46 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 47 | golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= 48 | golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= 49 | golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= 50 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 51 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 52 | golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= 53 | golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= 54 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 55 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 56 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 57 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 58 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 59 | golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 60 | golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= 61 | golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 62 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 63 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 64 | golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 65 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 66 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 67 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 68 | golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 69 | golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= 70 | golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= 71 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 72 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 73 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 74 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 75 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 76 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 77 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= 78 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 79 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 80 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 81 | -------------------------------------------------------------------------------- /x/mqtt/mqtt.go: -------------------------------------------------------------------------------- 1 | // Package mqtt provides [MQTT] integrations for kawa. 2 | // 3 | // [MQTT]: https://mqtt.org/ 4 | package mqtt 5 | 6 | import ( 7 | "context" 8 | "errors" 9 | "fmt" 10 | "strconv" 11 | "time" 12 | 13 | MQTT "github.com/eclipse/paho.mqtt.golang" 14 | "github.com/runreveal/kawa" 15 | ) 16 | 17 | // Option is an argument to [NewSource] or [NewDestination]. 18 | type Option func(*options) 19 | 20 | type options struct { 21 | broker string 22 | clientID string 23 | topic string 24 | 25 | userName string 26 | password string 27 | 28 | qos byte 29 | retained bool 30 | keepAlive time.Duration 31 | } 32 | 33 | // WithBroker specifies the broker URI to connect to. 34 | func WithBroker(broker string) Option { 35 | return func(opts *options) { 36 | opts.broker = broker 37 | } 38 | } 39 | 40 | // WithClientID specifies client ID to advertise when connecting to the MQTT broker. 41 | // This string must be no more than 23 bytes in length. 42 | func WithClientID(clientID string) Option { 43 | return func(opts *options) { 44 | opts.clientID = clientID 45 | } 46 | } 47 | 48 | // WithTopic specifies the topic to publish or subscribe to. 49 | // By default, this is "#". 50 | func WithTopic(topic string) Option { 51 | return func(opts *options) { 52 | if topic == "" { 53 | opts.topic = "#" 54 | } else { 55 | opts.topic = topic 56 | } 57 | } 58 | } 59 | 60 | // WithKeepAlive sets the amount of time before sending a PING message to the broker. 61 | func WithKeepAlive(keepAlive time.Duration) Option { 62 | return func(opts *options) { 63 | opts.keepAlive = keepAlive 64 | } 65 | } 66 | 67 | // WithQOS sets the quality-of-service (QoS) option for publishing or subscribing. 68 | func WithQOS(qos byte) Option { 69 | return func(opts *options) { 70 | opts.qos = qos 71 | } 72 | } 73 | 74 | // WithRetained sets whether messages sent to the topic will have the retained bit set. 75 | // WithRetained is only applicable for [NewDestination]. 76 | func WithRetained(retained bool) Option { 77 | return func(opts *options) { 78 | opts.retained = retained 79 | } 80 | } 81 | 82 | // WithUserName sets the username to authenticate to the broker with. 83 | func WithUserName(userName string) Option { 84 | return func(opts *options) { 85 | opts.userName = userName 86 | } 87 | } 88 | 89 | // WithPassword sets the password to authenticate to the broker with. 90 | func WithPassword(password string) Option { 91 | return func(opts *options) { 92 | opts.password = password 93 | } 94 | } 95 | 96 | // Destination is a [kawa.Destination] that publishes to an MQTT topic. 97 | type Destination struct { 98 | client MQTT.Client 99 | cfg options 100 | errc chan error 101 | } 102 | 103 | type msgAck struct { 104 | msg kawa.Message[[]byte] 105 | ack func() 106 | } 107 | 108 | func loadOpts(opts []Option) options { 109 | cfg := options{ 110 | topic: "#", 111 | retained: false, 112 | qos: 1, 113 | } 114 | 115 | for _, o := range opts { 116 | o(&cfg) 117 | } 118 | return cfg 119 | } 120 | 121 | // NewDestination returns a new [Destination] with the given options. 122 | // NewDestination returns an error if the caller does not provide [WithBroker] or [WithClientID]. 123 | func NewDestination(opts ...Option) (*Destination, error) { 124 | cfg := loadOpts(opts) 125 | ret := &Destination{ 126 | cfg: cfg, 127 | errc: make(chan error), 128 | } 129 | 130 | connLost := func(client MQTT.Client, err error) { 131 | ret.errc <- err 132 | } 133 | 134 | var err error 135 | ret.client, err = clientConnect(cfg, connLost) 136 | if err != nil { 137 | return nil, err 138 | } 139 | 140 | return ret, nil 141 | } 142 | 143 | func clientConnect(opts options, onLost MQTT.ConnectionLostHandler) (MQTT.Client, error) { 144 | if opts.broker == "" { 145 | return nil, errors.New("mqtt: missing broker") 146 | } 147 | if opts.clientID == "" { 148 | return nil, errors.New("mqtt: missing clientID") 149 | } 150 | 151 | clientOpts := MQTT.NewClientOptions(). 152 | AddBroker(opts.broker). 153 | SetClientID(opts.clientID). 154 | SetConnectionLostHandler(onLost). 155 | SetKeepAlive(opts.keepAlive) 156 | 157 | if opts.userName != "" { 158 | clientOpts = clientOpts.SetUsername(opts.userName) 159 | } 160 | if opts.password != "" { 161 | clientOpts = clientOpts.SetPassword(opts.password) 162 | } 163 | 164 | fmt.Printf("Opts: %+v\n", clientOpts) 165 | client := MQTT.NewClient(clientOpts) 166 | 167 | if token := client.Connect(); token.Wait() && token.Error() != nil { 168 | return nil, fmt.Errorf("mqtt connect error: %s", token.Error()) 169 | } 170 | 171 | return client, nil 172 | } 173 | 174 | // Run waits until ctx.Done() is closed or the MQTT connection is lost, 175 | // then disconnects after a grace period. 176 | func (dest *Destination) Run(ctx context.Context) error { 177 | var err error 178 | select { 179 | case err = <-dest.errc: 180 | case <-ctx.Done(): 181 | err = ctx.Err() 182 | } 183 | dest.client.Disconnect(1000) 184 | return err 185 | } 186 | 187 | // Send publishes each message in the batch to the configured topic. 188 | func (dest *Destination) Send(ctx context.Context, ack func(), msgs ...kawa.Message[[]byte]) error { 189 | for _, msg := range msgs { 190 | token := dest.client.Publish(dest.cfg.topic, dest.cfg.qos, dest.cfg.retained, string(msg.Value)) 191 | token.Wait() 192 | if token.Error() != nil { 193 | return token.Error() 194 | } 195 | } 196 | kawa.Ack(ack) 197 | return nil 198 | } 199 | 200 | // Source is a [kawa.Source] that subscribes to an MQTT topic. 201 | type Source struct { 202 | msgC chan msgAck 203 | cfg options 204 | errc chan error 205 | client MQTT.Client 206 | } 207 | 208 | // NewSource returns a new [Source] with the given options. 209 | // NewSource returns an error if the caller does not provide [WithBroker] or [WithClientID]. 210 | // 211 | // The caller is responsible for calling [*Source.Run] 212 | // or else [*Source.Recv] will block indefinitely. 213 | func NewSource(opts ...Option) (*Source, error) { 214 | cfg := loadOpts(opts) 215 | 216 | ret := &Source{ 217 | msgC: make(chan msgAck), 218 | cfg: cfg, 219 | errc: make(chan error), 220 | } 221 | 222 | connLost := func(client MQTT.Client, err error) { 223 | ret.errc <- err 224 | } 225 | 226 | var err error 227 | ret.client, err = clientConnect(cfg, connLost) 228 | if err != nil { 229 | return nil, err 230 | } 231 | 232 | return ret, nil 233 | } 234 | 235 | // Run receives messages until ctx.Done() is closed. 236 | func (src *Source) Run(ctx context.Context) error { 237 | return src.recvLoop(ctx) 238 | } 239 | 240 | func (src *Source) recvLoop(ctx context.Context) error { 241 | newMessage := func(client MQTT.Client, message MQTT.Message) { 242 | select { 243 | case src.msgC <- msgAck{ 244 | msg: kawa.Message[[]byte]{ 245 | Value: message.Payload(), 246 | Key: strconv.FormatUint(uint64(message.MessageID()), 10), 247 | Topic: message.Topic(), 248 | }, 249 | ack: message.Ack, 250 | }: 251 | case <-ctx.Done(): 252 | return 253 | } 254 | } 255 | 256 | token := src.client.Subscribe(src.cfg.topic, src.cfg.qos, newMessage) 257 | token.Wait() 258 | if token.Error() != nil { 259 | return fmt.Errorf("mqtt subscribe error: %s", token.Error()) 260 | } 261 | 262 | defer func() { 263 | src.client.Unsubscribe(src.cfg.topic) 264 | src.client.Disconnect(250) 265 | }() 266 | 267 | select { 268 | case err := <-src.errc: 269 | return err 270 | case <-ctx.Done(): 271 | return ctx.Err() 272 | } 273 | } 274 | 275 | // Recv waits for the next message from the topic. 276 | // 277 | // Note that Recv will block indefinitely unless [*Source.Run] is active. 278 | func (src *Source) Recv(ctx context.Context) (kawa.Message[[]byte], func(), error) { 279 | select { 280 | case <-ctx.Done(): 281 | return kawa.Message[[]byte]{}, nil, ctx.Err() 282 | case pass := <-src.msgC: 283 | return pass.msg, pass.ack, nil 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: '2' 3 | services: 4 | mosquitto: 5 | image: eclipse-mosquitto:latest 6 | container_name: mosquitto 7 | command: mosquitto -c /mosquitto-no-auth.conf 8 | ports: 9 | - "1883:1883" # Unencrypted MQTT 10 | - "9001:9001" # WebSocket 11 | volumes: 12 | # - /mosquitto/config 13 | - /mosquitto/data 14 | - /mosquitto/log 15 | 16 | zookeeper: 17 | image: confluentinc/cp-zookeeper:6.2.1 18 | hostname: zookeeper 19 | container_name: zookeeper 20 | ports: 21 | - "2181:2181" 22 | environment: 23 | ZOOKEEPER_CLIENT_PORT: 2181 24 | ZOOKEEPER_TICK_TIME: 2000 25 | 26 | broker: 27 | image: confluentinc/cp-server:6.2.1 28 | hostname: broker 29 | container_name: broker 30 | depends_on: 31 | - zookeeper 32 | ports: 33 | - "9092:9092" 34 | - "9101:9101" 35 | environment: 36 | KAFKA_BROKER_ID: 1 37 | KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181' 38 | KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT 39 | KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092 40 | KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter 41 | KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 42 | KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0 43 | KAFKA_CONFLUENT_LICENSE_TOPIC_REPLICATION_FACTOR: 1 44 | KAFKA_CONFLUENT_BALANCER_TOPIC_REPLICATION_FACTOR: 1 45 | KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 46 | KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1 47 | KAFKA_JMX_PORT: 9101 48 | KAFKA_JMX_HOSTNAME: localhost 49 | KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL: http://schema-registry:8081 50 | CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:29092 51 | CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1 52 | CONFLUENT_METRICS_ENABLE: 'true' 53 | CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous' 54 | 55 | # schema-registry: 56 | # image: confluentinc/cp-schema-registry:6.2.1 57 | # hostname: schema-registry 58 | # container_name: schema-registry 59 | # depends_on: 60 | # - broker 61 | # ports: 62 | # - "8081:8081" 63 | # environment: 64 | # SCHEMA_REGISTRY_HOST_NAME: schema-registry 65 | # SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:29092' 66 | # SCHEMA_REGISTRY_LISTENERS: http://0.0.0.0:8081 67 | 68 | # connect: 69 | # image: cnfldemos/cp-server-connect-datagen:0.5.0-6.2.1 70 | # hostname: connect 71 | # container_name: connect 72 | # depends_on: 73 | # - broker 74 | # - schema-registry 75 | # ports: 76 | # - "8083:8083" 77 | # environment: 78 | # CONNECT_BOOTSTRAP_SERVERS: 'broker:29092' 79 | # CONNECT_REST_ADVERTISED_HOST_NAME: connect 80 | # CONNECT_REST_PORT: 8083 81 | # CONNECT_GROUP_ID: compose-connect-group 82 | # CONNECT_CONFIG_STORAGE_TOPIC: docker-connect-configs 83 | # CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: 1 84 | # CONNECT_OFFSET_FLUSH_INTERVAL_MS: 10000 85 | # CONNECT_OFFSET_STORAGE_TOPIC: docker-connect-offsets 86 | # CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: 1 87 | # CONNECT_STATUS_STORAGE_TOPIC: docker-connect-status 88 | # CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: 1 89 | # CONNECT_KEY_CONVERTER: org.apache.kafka.connect.storage.StringConverter 90 | # CONNECT_VALUE_CONVERTER: io.confluent.connect.avro.AvroConverter 91 | # CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL: http://schema-registry:8081 92 | # # CLASSPATH required due to CC-2422 93 | # CLASSPATH: /usr/share/java/monitoring-interceptors/monitoring-interceptors-6.2.1.jar 94 | # CONNECT_PRODUCER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor" 95 | # CONNECT_CONSUMER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor" 96 | # CONNECT_PLUGIN_PATH: "/usr/share/java,/usr/share/confluent-hub-components" 97 | # CONNECT_LOG4J_LOGGERS: org.apache.zookeeper=ERROR,org.I0Itec.zkclient=ERROR,org.reflections=ERROR 98 | 99 | # control-center: 100 | # image: confluentinc/cp-enterprise-control-center:6.2.1 101 | # hostname: control-center 102 | # container_name: control-center 103 | # depends_on: 104 | # - broker 105 | # - schema-registry 106 | # - connect 107 | # - ksqldb-server 108 | # ports: 109 | # - "9021:9021" 110 | # environment: 111 | # CONTROL_CENTER_BOOTSTRAP_SERVERS: 'broker:29092' 112 | # CONTROL_CENTER_CONNECT_CONNECT-DEFAULT_CLUSTER: 'connect:8083' 113 | # CONTROL_CENTER_KSQL_KSQLDB1_URL: "http://ksqldb-server:8088" 114 | # CONTROL_CENTER_KSQL_KSQLDB1_ADVERTISED_URL: "http://localhost:8088" 115 | # CONTROL_CENTER_SCHEMA_REGISTRY_URL: "http://schema-registry:8081" 116 | # CONTROL_CENTER_REPLICATION_FACTOR: 1 117 | # CONTROL_CENTER_INTERNAL_TOPICS_PARTITIONS: 1 118 | # CONTROL_CENTER_MONITORING_INTERCEPTOR_TOPIC_PARTITIONS: 1 119 | # CONFLUENT_METRICS_TOPIC_REPLICATION: 1 120 | # PORT: 9021 121 | 122 | # ksqldb-server: 123 | # image: confluentinc/cp-ksqldb-server:6.2.1 124 | # hostname: ksqldb-server 125 | # container_name: ksqldb-server 126 | # depends_on: 127 | # - broker 128 | # - connect 129 | # ports: 130 | # - "8088:8088" 131 | # environment: 132 | # KSQL_CONFIG_DIR: "/etc/ksql" 133 | # KSQL_BOOTSTRAP_SERVERS: "broker:29092" 134 | # KSQL_HOST_NAME: ksqldb-server 135 | # KSQL_LISTENERS: "http://0.0.0.0:8088" 136 | # KSQL_CACHE_MAX_BYTES_BUFFERING: 0 137 | # KSQL_KSQL_SCHEMA_REGISTRY_URL: "http://schema-registry:8081" 138 | # KSQL_PRODUCER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor" 139 | # KSQL_CONSUMER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor" 140 | # KSQL_KSQL_CONNECT_URL: "http://connect:8083" 141 | # KSQL_KSQL_LOGGING_PROCESSING_TOPIC_REPLICATION_FACTOR: 1 142 | # KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: 'true' 143 | # KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: 'true' 144 | 145 | # ksqldb-cli: 146 | # image: confluentinc/cp-ksqldb-cli:6.2.1 147 | # container_name: ksqldb-cli 148 | # depends_on: 149 | # - broker 150 | # - connect 151 | # - ksqldb-server 152 | # entrypoint: /bin/sh 153 | # tty: true 154 | 155 | # ksql-datagen: 156 | # image: confluentinc/ksqldb-examples:6.2.1 157 | # hostname: ksql-datagen 158 | # container_name: ksql-datagen 159 | # depends_on: 160 | # - ksqldb-server 161 | # - broker 162 | # - schema-registry 163 | # - connect 164 | # command: "bash -c 'echo Waiting for Kafka to be ready... && \ 165 | # cub kafka-ready -b broker:29092 1 40 && \ 166 | # echo Waiting for Confluent Schema Registry to be ready... && \ 167 | # cub sr-ready schema-registry 8081 40 && \ 168 | # echo Waiting a few seconds for topic creation to finish... && \ 169 | # sleep 11 && \ 170 | # tail -f /dev/null'" 171 | # environment: 172 | # KSQL_CONFIG_DIR: "/etc/ksql" 173 | # STREAMS_BOOTSTRAP_SERVERS: broker:29092 174 | # STREAMS_SCHEMA_REGISTRY_HOST: schema-registry 175 | # STREAMS_SCHEMA_REGISTRY_PORT: 8081 176 | 177 | # rest-proxy: 178 | # image: confluentinc/cp-kafka-rest:6.2.1 179 | # depends_on: 180 | # - broker 181 | # - schema-registry 182 | # ports: 183 | # - 8082:8082 184 | # hostname: rest-proxy 185 | # container_name: rest-proxy 186 | # environment: 187 | # KAFKA_REST_HOST_NAME: rest-proxy 188 | # KAFKA_REST_BOOTSTRAP_SERVERS: 'broker:29092' 189 | # KAFKA_REST_LISTENERS: "http://0.0.0.0:8082" 190 | # KAFKA_REST_SCHEMA_REGISTRY_URL: 'http://schema-registry:8081' 191 | -------------------------------------------------------------------------------- /types.go: -------------------------------------------------------------------------------- 1 | package kawa 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | ) 7 | 8 | // Message is the data wrapper which accepts any serializable type as it's 9 | // embedded Value as well as some other metadata. 10 | type Message[T any] struct { 11 | // Key represents the key of this message. This field is intended to be used 12 | // primarily as an input into sharding functions to determine how a message 13 | // should be routed within a topic. 14 | Key string 15 | // Value is the embedded value of this message. It is the object of interest 16 | // to the users of this library. It can be any serializable type so long as 17 | // the sources and destinations know how to serialize it. 18 | Value T 19 | // Topic indicates which topic this message came from (if applicable). It 20 | // should not be used as a means to set the output topic for destinations. 21 | Topic string 22 | // Attributes are inspired by context.Context and are used as a means to pass 23 | // metadata from a source implementation through to a consumer. See examples 24 | // for details. 25 | Attributes Attributes 26 | } 27 | 28 | type Attributes interface { 29 | Unwrap() Attributes 30 | } 31 | 32 | // Source defines the abstraction for which kawa consumes or receives messages 33 | // from an external entity. Most notable implementations are queues (Kafka, 34 | // RabbitMQ, Redis), but anything which is message oriented could be made into 35 | // a source (e.g. a newline-delimited-JSON file could conceivably be a source). 36 | type Source[T any] interface { 37 | // Recv should block until Message is available to be returned from the 38 | // source. Implementations _must_ listen on <-ctx.Done() and return 39 | // ctx.Err() if the context finishes while waiting for new messages. 40 | // 41 | // All errors which are retryable must be handled inside the Recv func, or 42 | // otherwise handled internally. Any errors returned from Recv indicate a 43 | // fatal error to the processor, and the processor will terminate. If you 44 | // want to be able to delegate the responsibility of deciding retryable 45 | // errors to the user of the Source, then allow the user to register a 46 | // callback, e.g. `IsRetryable(err error) bool`, on source instantiation. 47 | // 48 | // The second return value is the acknowlegement function. Ack is called when 49 | // the message returned from Recv has been successfully written to it's 50 | // destination. It should not be called twice. Sources may panic in that 51 | // scenario as it indicates a logical flaw for delivery guarantees within the 52 | // program. 53 | // 54 | // In the case of sending to multiple destinations, or teeing the data stream 55 | // inside a processor's handler function, then the programmer must decide 56 | // themselves how to properly acknowledge the event, and recognize that 57 | // destinations will probably be acknowledging the message as well. 58 | Recv(context.Context) (Message[T], func(), error) 59 | } 60 | 61 | type SourceFunc[T any] func(context.Context) (Message[T], func(), error) 62 | 63 | func (sf SourceFunc[T]) Recv(ctx context.Context) (Message[T], func(), error) { 64 | return sf(ctx) 65 | } 66 | 67 | // MsgAck is a utility type which is used to pass a message and it's 68 | // corresponding ack function through a channel internal to a source or 69 | // destination 70 | type MsgAck[T any] struct { 71 | Msg Message[T] 72 | Ack func() 73 | } 74 | 75 | // Ack is a convenience function for calling the ack function after checking if 76 | // it's nil. 77 | func Ack(ack func()) { 78 | if ack != nil { 79 | ack() 80 | } 81 | } 82 | 83 | // Destination defines the abstraction for writing messages to an external 84 | // entity. Most notable implementations are queues (Kafka, RabbitMQ, Redis), 85 | // but anything which is message oriented could be made into a Destination 86 | // (e.g. a newline-delimited-JSON file could conceivably be a Destination). 87 | type Destination[T any] interface { 88 | // Send sends the passed in messages to the Destination. Implementations 89 | // _must_ listen on <-ctx.Done() and return ctx.Err() if the context finishes 90 | // while waiting to send messages. 91 | // 92 | // *Send need not be blocking*. In the case of a non-blocking call to send, 93 | // it's expected that ack will be called _only after_ the message has been 94 | // successfully written to the Destination. 95 | // 96 | // All errors which are retryable must be handled inside the Send func, or 97 | // otherwise handled internally. Any errors returned from Send indicate a 98 | // fatal error to the processor, and the processor will terminate. If you 99 | // want to be able to delegate the responsibility of deciding retryable 100 | // errors to the user of the Destination, then allow the user to register a 101 | // callback, e.g. `IsRetryable(err error) bool`, when instantiating a 102 | // Destination. 103 | // 104 | // The second argument value is the acknowlegement function. Ack is called 105 | // when the message has been successfully written to the Destination. It 106 | // should not be called twice. Sources may panic if ack is called twice as 107 | // it indicates a logical flaw for delivery guarantees within the program. 108 | // 109 | // In the case of sending to multiple destinations, or teeing the data stream 110 | // inside a processor's handler function, then the programmer must decide 111 | // themselves how to properly acknowledge the event, and recognize that 112 | // destinations will probably be acknowledging the message as well. 113 | Send(context.Context, func(), ...Message[T]) error 114 | } 115 | 116 | type DestinationFunc[T any] func(context.Context, func(), ...Message[T]) error 117 | 118 | func (df DestinationFunc[T]) Send(ctx context.Context, ack func(), msgs ...Message[T]) error { 119 | return df(ctx, ack, msgs...) 120 | } 121 | 122 | // Handler defines a function which operates on a single event of type T1 and 123 | // returns a list of events of type T2. T1 and T2 may be equivalent types. 124 | // Returning an empty slice and a nil error indicates that the message passed 125 | // in was processed successfully, no output was necessary, and therefore should 126 | // be acknowledged by the processor as having been processed successfully. 127 | type Handler[T1, T2 any] interface { 128 | Handle(context.Context, Message[T1]) ([]Message[T2], error) 129 | } 130 | 131 | type HandlerFunc[T1, T2 any] func(context.Context, Message[T1]) ([]Message[T2], error) 132 | 133 | func (hf HandlerFunc[T1, T2]) Handle(ctx context.Context, msg Message[T1]) ([]Message[T2], error) { 134 | return hf(ctx, msg) 135 | } 136 | 137 | func Pipe[T any]() Handler[T, T] { 138 | return pipe[T]{} 139 | } 140 | 141 | type pipe[T any] struct{} 142 | 143 | func (p pipe[T]) Handle(ctx context.Context, msg Message[T]) ([]Message[T], error) { 144 | return []Message[T]{msg}, nil 145 | } 146 | 147 | // // Pipe is a handler which simply passes a message through without modification. 148 | // func Pipe[T any](ctx context.Context, msg Message[T]) ([]Message[T], error) { 149 | // return []Message[T]{msg}, nil 150 | // } 151 | 152 | type DeserFunc[T any] func([]byte) (T, error) 153 | 154 | // BalancedSource handles rebalancing clients 155 | // type BalancedSource[T any] interface { 156 | // Listen(ctx context.Context) (Source[T], error) 157 | // } 158 | 159 | type ByteSource interface { 160 | Recv(context.Context) (Message[[]byte], func(), error) 161 | } 162 | 163 | func TransformUnmarshalJSON[T any](bs []byte) (T, error) { 164 | var val T 165 | err := json.Unmarshal(bs, &val) 166 | return val, err 167 | } 168 | 169 | type DeserializationSource[T any] struct { 170 | src ByteSource 171 | deser func([]byte) (T, error) 172 | } 173 | 174 | func NewDeserSource[T any](src ByteSource, deser DeserFunc[T]) DeserializationSource[T] { 175 | return DeserializationSource[T]{ 176 | src: src, 177 | deser: deser, 178 | } 179 | } 180 | 181 | func (ds DeserializationSource[T]) Recv(ctx context.Context) (Message[T], func(), error) { 182 | msg, ack, err := ds.src.Recv(ctx) 183 | if err != nil { 184 | return Message[T]{}, ack, err 185 | } 186 | val, err := ds.deser(msg.Value) 187 | 188 | ret := Message[T]{ 189 | Key: msg.Key, 190 | Value: val, 191 | Topic: msg.Topic, 192 | Attributes: msg.Attributes, 193 | } 194 | return ret, ack, err 195 | } 196 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kawa 2 | 3 | 4 |
5 |
6 | 7 |    8 |
9 |
10 | Go Reference 11 | GoFrame CI 12 | Go Report Card 13 | License 14 |
15 |
16 | 17 | --- 18 | 19 | kawa ("Kaa-Wah") is an opinionated framework for scalable, reliable stream processing. 20 | 21 | # Installation 22 | 23 | ## Kawa 24 | 25 | Add the library to your project as you would any other Go library: 26 | 27 | ```go 28 | go get -u github.com/runreveal/kawa 29 | ``` 30 | 31 | # Design and Rationale 32 | 33 | See https://blog.runreveal.com/kawa-the-event-processor-for-the-grug-brained-developer/ 34 | 35 | # Roadmap 36 | 37 | - Ensure that consumers of kawa aren't subject to all the dependencies of the plugins. 38 | - Event Routing and/or Multiple Processors in kawa program 39 | - Dynamic Sources (e.g. Kafka Consumer Groups) 40 | 41 | # Disclaimer 42 | 43 | This is nascent software, subject to breaking changes as we reach a good 44 | working set of APIs, interfaces and data models. Please try it out and help 45 | shape the direction of the project by giving us feedback! 46 | 47 | # Development & Extension 48 | 49 | The source and destination interfaces are designed for simplicity of use and 50 | implementation. It should be easy to use the sources and destinations in an 51 | abstract manner without knowing the underlying implementation, and it should 52 | be relatively easy to implement sources and destinations of various types. 53 | 54 | The library provides multiple abstractions suitable for different purposes. 55 | 56 | The easiest way to get started is by using the polling/batch implementations 57 | for sources/destinations, respectively, since they require less overhead in 58 | terms of accounting for offset tracking to ensure at-least-once reliable 59 | processing. 60 | 61 | Extensions under the `x` package provide either generic or `[]byte` based 62 | sources, destinations and utility functions which aren't part of the core 63 | functionality of kawa. They're provided for re-use in other applications. 64 | 65 | To use them, import them into your program and apply the proper serialization 66 | techniques relevant to your application. See examples of this in practice in 67 | the `cmd/kawad/internal` package, where we use it for system logs. 68 | 69 | ## Configure and Run Design Pattern 70 | 71 | The "Configure and Run" pattern is a pattern discovered while writing this 72 | framework that works nicely with other patterns and libraries in Go. 73 | 74 | The general idea is as follows. Each struct maintaining long running goroutines 75 | can be made easy to reason about and operate by splitting it's configuration and 76 | runtime into two separate stages. 77 | 78 | The first stage, "Configure", is simply the initialization of the struct. Most 79 | often, this is the New function for the struct, with required arguments passed 80 | in first, and options passed in as a variadic functional options slice 81 | afterwards. Occasionally, this may involve also implementing a translation 82 | layer for serialization of the struct from JSON or some other serialization 83 | format (see cmd/kawad/config.go for an example of this pattern). 84 | 85 | The next stage, "Run", involves implementing the simple Runner interface: 86 | 87 | ```golang 88 | type Runner interface { 89 | Run(ctx context.Context) error 90 | } 91 | ``` 92 | 93 | Implementing this interface consistently across all instances of structs that 94 | have long-running processes means that we can easily implement cancellation 95 | across a broad number of distinct types via a common context variable. 96 | 97 | It also means that any goroutine can trigger a shutdown by returning an error 98 | from the Run routine. 99 | 100 | While not absolutely required, following this pattern will enable the source or 101 | destination to be seamlessly integrated into the daemon. 102 | 103 | ## Implementing Sources 104 | 105 | Sources are things that you read message-oriented data from. At the most basic 106 | level, it's a collection of bytes that together represent some discrete event. 107 | 108 | ### Polling Source 109 | 110 | We recommend implementing polling sources when querying an API, or whenever 111 | it's easiest to implement a function to periodically get called. The following 112 | is the interface which needs to be satisfied to implement a polling source. 113 | 114 | ```golang 115 | type Poller[T any] interface { 116 | Poll(context.Context, int) ([]kawa.Message[T], func(), error) 117 | } 118 | ``` 119 | 120 | ### Streaming Source 121 | 122 | We recommend implementing streaming sources when the source either implements 123 | it's own batching semantics (like Kafka), or when message latency is more 124 | important than message volume. 125 | 126 | ## Implementing Destinations 127 | 128 | Destinations are things that you write message-oriented data to. 129 | 130 | ### Batch Destination 131 | 132 | Implementing a batch destination is the easiest way to process messages as a 133 | batch being written to some persistent storage. It handles timeouts, batch size, 134 | and parallel writes at the configuration level so destinations only have to implement 135 | a single method "Flush". 136 | 137 | ```golang 138 | type Flusher[T any] interface { 139 | Flush(context.Context, []kawa.Message[T]) error 140 | } 141 | ``` 142 | 143 | ### Streaming Destination 144 | 145 | We recommend implementing streaming destinations when the destination either 146 | implements it's own batching semantics (like Kafka), or when message latency is 147 | more important than message volume. 148 | 149 | # Supported sources 150 | 151 | - syslog 152 | - scanner 153 | - journald 154 | - mqtt 155 | 156 | # Supported destinations 157 | 158 | - s3 / r2 159 | - printer 160 | - runreveal 161 | - mqtt 162 | 163 | # Configuring the Daemon 164 | 165 | ## Source Configuration 166 | 167 | ### syslog 168 | 169 | With the syslog config, and address and content type can be set. 170 | 171 | ``` 172 | { 173 | "type":"syslog", 174 | "addr":"0.0.0.0:5514", 175 | } 176 | ``` 177 | 178 | ### journald 179 | 180 | Journald has no configuration, just set the type and kawa will read from journald. 181 | ``` 182 | { 183 | "type":"journald" 184 | } 185 | ``` 186 | 187 | ### scanner 188 | 189 | Read from stdin. Useful for testing or doing something you probably shouldn't. 190 | 191 | ``` 192 | { 193 | "type":"scanner", 194 | } 195 | ``` 196 | 197 | ### MQTT 198 | MQTT will listen on the supplied topic for new events. 199 | 200 | broker and clientID are required to receive data. 201 | clientID must be unique from any other mqtt destinations or sources 202 | If topic is not supplied, it will default to the wildcard `#`. 203 | 204 | Do not read events from the same topic that an MQTT destination is sending to otherwise kawa will create an infinite loop and eventually crash. 205 | 206 | ``` 207 | { 208 | "type": "mqtt", 209 | "broker": "mqtt://broker.mqtt:1883", 210 | "clientID": "kawa_src", 211 | "userName": "", 212 | "password": "", 213 | "topic": "kawa/src", 214 | 215 | "qos": 1, // Optional defaults to 1 if not included 216 | "retained": false, // Optional defaults to false if not included 217 | } 218 | ``` 219 | 220 | ### Windows Event Logs 221 | Listen for new windows event logs on the specified channel. 222 | 223 | Windows event log collection only works on Windows machines. Use the Windows build to run Kawad on a Windows machine. Kawad will need to be run as an administrator to have access to the event log stream. 224 | 225 | The source config needs a required channel and an optional query. 226 | The channel is the windows event log full name, e.g. to log the operational logs for the TaskScheduler the channel would be 'Microsoft-Windows-TaskScheduler/Operational'. 227 | The query is a filter that can be used to limit the logs that are collected to specific events. View [Microsoft documentation](https://techcommunity.microsoft.com/t5/ask-the-directory-services-team/advanced-xml-filtering-in-the-windows-event-viewer/ba-p/399761) on how filtering works and how to create one to use. 228 | 229 | The following example shows how to log every Security event on the machine. 230 | 231 | ``` 232 | { 233 | "type": "eventlog", 234 | "channel": "Security", 235 | "query": "*" 236 | } 237 | ``` 238 | 239 | 240 | ## Destination Configuration 241 | 242 | ### RunReveal 243 | 244 | WebhookURL is the only config argument and it is required. 245 | 246 | ``` 247 | { 248 | "type":"runreveal", 249 | "webhookURL": "https://api.runreveal.com/....." 250 | } 251 | ``` 252 | 253 | ### S3 254 | 255 | The s3 destination is compatible with s3 and other s3 compatible interfaces. By default the s3 destination will pull credentials from the standard places the aws sdk looks, but they can optionally be set in the configuration. 256 | 257 | customEndpoint must be set for custom destinations, and in that case bucketRegion probably will not be set. bucketName is the only required argument. 258 | 259 | For high volume or low volume, the batchSize can be tweaked but is set to 100 by default. 260 | 261 | ``` 262 | { 263 | "type":"s3", 264 | "bucketName":"my-cool-log-bucket", 265 | "bucketRegion":"us-east-2", 266 | "batchSize":1000, 267 | } 268 | ``` 269 | 270 | ### Printer 271 | 272 | Printer will print the results to stdout. Useful for testing and development. 273 | ``` 274 | { 275 | "type":"printer", 276 | } 277 | ``` 278 | 279 | ### MQTT 280 | MQTT will send events to the supplied topic. 281 | 282 | broker and clientID are required to send data. 283 | clientID must be unique from any other mqtt destinations or sources 284 | If topic is not supplied, it will default to the wildcard `#`. 285 | 286 | ``` 287 | { 288 | "type": "mqtt", 289 | "broker": "mqtt://broker.mqtt:1883", 290 | "clientID": "kawa_dst", 291 | "userName": "", 292 | "password": "", 293 | "topic": "kawa/dest", 294 | 295 | "qos": 1, // Optional defaults to 1 if not included 296 | "retained": false, // Optional defaults to false if not included 297 | } 298 | ``` 299 | 300 | ## Source / Destination Wishlist 301 | 302 | - Kafka 303 | - redis 304 | - NATS 305 | - amqp 306 | - pubsub 307 | - Kinesis 308 | - memcache? 309 | - zmq? 310 | - NSQ? 311 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2022 Alan Braithwaite 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /x/batcher/batcher.go: -------------------------------------------------------------------------------- 1 | package batch 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | "sync" 8 | "time" 9 | 10 | "log/slog" 11 | 12 | "github.com/runreveal/kawa" 13 | "github.com/segmentio/ksuid" 14 | ) 15 | 16 | // ErrDontAck should be returned by ErrorHandlers when they wish to 17 | // signal to the batcher to skip acking a message as delivered, but 18 | // continue to process. For example, if an error is retryable and 19 | // will be retried upstream at the source if an ack is not received 20 | // before some timeout. 21 | var ErrDontAck = errors.New("Destination encountered a retryable error") 22 | 23 | // Flusher is the core interface that the user of this package must implement 24 | // to get the batching functionality. 25 | // It takes a slice of messages and returns an error if the flush fails. It's 26 | // expected to be run synchronously and only return once the flush is complete. 27 | // The flusher MUST respond to the context being canceled and return an error 28 | // if the context is canceled. If no other error occured, then return the 29 | // context error. 30 | type Flusher[T any] interface { 31 | Flush(context.Context, []kawa.Message[T]) error 32 | } 33 | 34 | type FlushFunc[T any] func(context.Context, []kawa.Message[T]) error 35 | 36 | func (ff FlushFunc[T]) Flush(c context.Context, msgs []kawa.Message[T]) error { 37 | return ff(c, msgs) 38 | } 39 | 40 | type ErrorHandler[T any] interface { 41 | HandleError(context.Context, error, []kawa.Message[T]) error 42 | } 43 | 44 | type ErrorFunc[T any] func(context.Context, error, []kawa.Message[T]) error 45 | 46 | func (ef ErrorFunc[T]) HandleError(c context.Context, err error, msgs []kawa.Message[T]) error { 47 | return ef(c, err, msgs) 48 | } 49 | 50 | // Destination is a batching destination that will buffer messages until the 51 | // FlushLength limit is reached or the FlushFrequency timer fires, whichever 52 | // comes first. 53 | // 54 | // `Destination.Run` must be called after calling `New` before events will be 55 | // processed in this destination. Not calling `Run` will likely end in a 56 | // deadlock as the internal channel being written to by `Send` will not be 57 | // getting read. 58 | type Destination[T any] struct { 59 | flusher Flusher[T] 60 | flushq chan struct{} 61 | flushlen int 62 | flushfreq time.Duration 63 | flushcan map[string]context.CancelFunc 64 | flushTimeout time.Duration 65 | stopTimeout time.Duration 66 | watchdogTimeout time.Duration 67 | 68 | errorHandler ErrorHandler[T] 69 | flusherr chan error 70 | 71 | maxRetries int 72 | initialBackoff time.Duration 73 | maxBackoff time.Duration 74 | backoffMultiplier float64 75 | isRetryable func(error) bool 76 | 77 | messages chan msgAck[T] 78 | buf []msgAck[T] 79 | 80 | count int 81 | running bool 82 | syncMu sync.Mutex 83 | } 84 | 85 | type OptFunc func(*Opts) 86 | 87 | type Opts struct { 88 | FlushLength int 89 | FlushFrequency time.Duration 90 | FlushTimeout time.Duration 91 | FlushParallelism int 92 | StopTimeout time.Duration 93 | WatchdogTimeout time.Duration 94 | MaxRetries int 95 | InitialBackoff time.Duration 96 | MaxBackoff time.Duration 97 | BackoffMultiplier float64 98 | IsRetryable func(error) bool 99 | } 100 | 101 | func FlushFrequency(d time.Duration) func(*Opts) { 102 | return func(opts *Opts) { 103 | opts.FlushFrequency = d 104 | } 105 | } 106 | 107 | func FlushLength(size int) func(*Opts) { 108 | return func(opts *Opts) { 109 | opts.FlushLength = size 110 | } 111 | } 112 | 113 | func FlushParallelism(n int) func(*Opts) { 114 | return func(opts *Opts) { 115 | opts.FlushParallelism = n 116 | } 117 | } 118 | 119 | func FlushTimeout(d time.Duration) func(*Opts) { 120 | return func(opts *Opts) { 121 | opts.FlushTimeout = d 122 | } 123 | } 124 | 125 | func WatchdogTimeout(d time.Duration) func(*Opts) { 126 | return func(opts *Opts) { 127 | opts.WatchdogTimeout = d 128 | } 129 | } 130 | 131 | func StopTimeout(d time.Duration) func(*Opts) { 132 | return func(opts *Opts) { 133 | opts.StopTimeout = d 134 | } 135 | } 136 | 137 | func MaxRetries(n int) func(*Opts) { 138 | return func(opts *Opts) { 139 | opts.MaxRetries = n 140 | } 141 | } 142 | 143 | func InitialBackoff(d time.Duration) func(*Opts) { 144 | return func(opts *Opts) { 145 | opts.InitialBackoff = d 146 | } 147 | } 148 | 149 | func MaxBackoff(d time.Duration) func(*Opts) { 150 | return func(opts *Opts) { 151 | opts.MaxBackoff = d 152 | } 153 | } 154 | 155 | func BackoffMultiplier(m float64) func(*Opts) { 156 | return func(opts *Opts) { 157 | opts.BackoffMultiplier = m 158 | } 159 | } 160 | 161 | // IsRetryable takes a callback which will be called on the return value from 162 | // your flush function. If it returns true, the flush will be retried, 163 | // otherwise the error will be passed to your defined error handler. 164 | func IsRetryable(fn func(error) bool) func(*Opts) { 165 | return func(opts *Opts) { 166 | opts.IsRetryable = fn 167 | } 168 | } 169 | 170 | func DiscardHandler[T any]() ErrorHandler[T] { 171 | return ErrorFunc[T](func(context.Context, error, []kawa.Message[T]) error { return nil }) 172 | } 173 | 174 | func Raise[T any]() ErrorHandler[T] { 175 | return ErrorFunc[T](func(_ context.Context, err error, _ []kawa.Message[T]) error { return err }) 176 | } 177 | 178 | // NewDestination instantiates a new batcher. 179 | func NewDestination[T any](f Flusher[T], e ErrorHandler[T], opts ...OptFunc) *Destination[T] { 180 | cfg := Opts{ 181 | FlushLength: 100, 182 | FlushFrequency: 1 * time.Second, 183 | FlushParallelism: 2, 184 | StopTimeout: 5 * time.Second, 185 | MaxRetries: 3, 186 | InitialBackoff: 500 * time.Millisecond, 187 | MaxBackoff: 5 * time.Second, 188 | BackoffMultiplier: 2.0, 189 | } 190 | 191 | for _, o := range opts { 192 | o(&cfg) 193 | } 194 | 195 | // TODO: validate here 196 | if cfg.FlushParallelism < 1 { 197 | panic("FlushParallelism must be greater than or equal to 1") 198 | } 199 | if e == nil { 200 | panic("ErrorHandler must not be nil") 201 | } 202 | if cfg.StopTimeout < 0 { 203 | cfg.StopTimeout = 0 204 | } 205 | if cfg.WatchdogTimeout < 0 { 206 | cfg.WatchdogTimeout = 0 207 | } 208 | if cfg.FlushTimeout < 0 { 209 | cfg.FlushTimeout = 0 210 | } 211 | if cfg.MaxRetries < 0 { 212 | cfg.MaxRetries = 0 213 | } 214 | if cfg.InitialBackoff < 0 { 215 | cfg.InitialBackoff = 0 216 | } 217 | if cfg.MaxBackoff < 0 { 218 | cfg.MaxBackoff = 0 219 | } 220 | if cfg.BackoffMultiplier <= 0 { 221 | cfg.BackoffMultiplier = 1.0 222 | } 223 | 224 | d := &Destination[T]{ 225 | flushlen: cfg.FlushLength, 226 | flushq: make(chan struct{}, cfg.FlushParallelism), 227 | flusher: f, 228 | flushcan: make(map[string]context.CancelFunc), 229 | flushfreq: cfg.FlushFrequency, 230 | flushTimeout: cfg.FlushTimeout, 231 | stopTimeout: cfg.StopTimeout, 232 | watchdogTimeout: cfg.WatchdogTimeout, 233 | 234 | errorHandler: e, 235 | flusherr: make(chan error, cfg.FlushParallelism), 236 | 237 | maxRetries: cfg.MaxRetries, 238 | initialBackoff: cfg.InitialBackoff, 239 | maxBackoff: cfg.MaxBackoff, 240 | backoffMultiplier: cfg.BackoffMultiplier, 241 | isRetryable: cfg.IsRetryable, 242 | 243 | messages: make(chan msgAck[T]), 244 | } 245 | 246 | return d 247 | } 248 | 249 | type msgAck[T any] struct { 250 | msg kawa.Message[T] 251 | ack func() 252 | } 253 | 254 | // Send satisfies the kawa.Destination interface and accepts messages to be 255 | // buffered for flushing after the FlushLength limit is reached or the 256 | // FlushFrequency timer fires, whichever comes first. 257 | // 258 | // Messages will not be acknowledged until they have been flushed successfully. 259 | func (d *Destination[T]) Send(ctx context.Context, ack func(), msgs ...kawa.Message[T]) error { 260 | if len(msgs) < 1 { 261 | return nil 262 | } 263 | 264 | callMe := ackFn(ack, len(msgs)) 265 | 266 | for _, m := range msgs { 267 | select { 268 | case d.messages <- msgAck[T]{msg: m, ack: callMe}: // Here 269 | case <-ctx.Done(): 270 | // TODO: one more flush? 271 | return ctx.Err() 272 | } 273 | } 274 | 275 | return nil 276 | } 277 | 278 | // Run starts the batching destination. It must be called before messages will 279 | // be processed and written to the underlying Flusher. 280 | // Run will block until the context is canceled. 281 | // Upon cancellation, Run will flush any remaining messages in the buffer and 282 | // return any flush errors that occur 283 | func (d *Destination[T]) Run(ctx context.Context) error { 284 | var epoch uint64 285 | epochC := make(chan uint64) 286 | setTimer := true 287 | 288 | d.syncMu.Lock() 289 | if d.running { 290 | panic("already running") 291 | } else { 292 | d.running = true 293 | } 294 | d.syncMu.Unlock() 295 | 296 | var wdChan <-chan time.Time 297 | var wdTimer *time.Timer 298 | 299 | // We are using 3*parallelism for the buffer lenght 300 | // This is the theoretical maximum signals we would get if 301 | // #parallelism number of flushes started/completed/started again 302 | // before the channel would drain. Memory cost is miniscule for 303 | // this buffer because it just holds struct{} 304 | wdResetC := make(chan struct{}, 3*len(d.flushq)) 305 | if d.watchdogTimeout > 0 { 306 | wdTimer = time.NewTimer(d.watchdogTimeout) 307 | wdChan = wdTimer.C 308 | } 309 | 310 | var err error 311 | loop: 312 | for { 313 | select { 314 | case <-wdChan: 315 | // If no flushes are in-flight, this is just an idle timeout, not a deadlock 316 | if len(d.flushq) == 0 { 317 | // Reset the watchdog and continue 318 | if wdTimer != nil { 319 | if !wdTimer.Stop() { 320 | select { 321 | case <-wdTimer.C: 322 | default: 323 | } 324 | } 325 | wdTimer.Reset(d.watchdogTimeout) 326 | } 327 | continue 328 | } 329 | // There are flushes in-flight that haven't completed - this is a real deadlock 330 | return errDeadlock 331 | 332 | case <-wdResetC: 333 | // A flush has started or completed, reset the watchdog 334 | if wdTimer != nil { 335 | if !wdTimer.Stop() { 336 | <-wdTimer.C 337 | } 338 | wdTimer.Reset(d.watchdogTimeout) 339 | } 340 | 341 | case msg := <-d.messages: // Here 342 | d.count++ 343 | if setTimer { 344 | // copy the epoch to send on the chan after the timer fires 345 | epc := epoch 346 | time.AfterFunc(d.flushfreq, func() { 347 | epochC <- epc // Here 348 | }) 349 | 350 | if wdTimer != nil { 351 | if !wdTimer.Stop() { 352 | <-wdTimer.C 353 | } 354 | wdTimer.Reset(d.watchdogTimeout) 355 | } 356 | 357 | setTimer = false 358 | } 359 | d.buf = append(d.buf, msg) 360 | if len(d.buf) >= d.flushlen { 361 | epoch++ 362 | d.flush(ctx, wdResetC) 363 | setTimer = true 364 | } 365 | case tEpoch := <-epochC: 366 | // if we haven't flushed yet this epoch, then flush, otherwise ignore 367 | if tEpoch == epoch { 368 | epoch++ 369 | d.flush(ctx, wdResetC) 370 | setTimer = true 371 | } 372 | case <-ctx.Done(): 373 | // on shutdown, don't attempt final flush even if buffer is not empty 374 | break loop 375 | case err = <-d.flusherr: 376 | break loop 377 | } 378 | } 379 | 380 | // we're done, no flushes in flight 381 | if len(d.flushq) == 0 { 382 | return err 383 | } 384 | 385 | slog.Info("stopping batcher. waiting for remaining flushes to finish.", "len", len(d.flushq)) 386 | for i := 10 * time.Millisecond; i < d.stopTimeout; i = i + 10*time.Millisecond { 387 | if len(d.flushq) == 0 { 388 | return err 389 | } 390 | time.Sleep(10 * time.Millisecond) 391 | } 392 | // flushes still active after timeout 393 | // cancel them. 394 | d.syncMu.Lock() 395 | for k, v := range d.flushcan { 396 | v() 397 | fmt.Println("timeout cancel for id", k) 398 | } 399 | d.syncMu.Unlock() 400 | return errDeadlock 401 | } 402 | 403 | var errDeadlock = errors.New("batcher: flushes timed out") 404 | 405 | func (d *Destination[T]) flush(ctx context.Context, wdResetC chan<- struct{}) { 406 | // We make a new context here so that we can cancel the flush if the parent 407 | // context is canceled. It's important to use context.Background() here because 408 | // we don't want to propagate the parent context's cancelation to the flusher. 409 | // If we did, then the flusher would likely be canceled before it could 410 | // finish flushing. 411 | flctx, cancel := context.WithCancel(context.Background()) 412 | 413 | id := ksuid.New().String() 414 | d.syncMu.Lock() 415 | d.flushcan[id] = cancel 416 | d.syncMu.Unlock() 417 | 418 | // block until a slot is available, or until a timeout is reached in the 419 | // parent context 420 | select { 421 | case d.flushq <- struct{}{}: 422 | // Flush slot acquired - signal watchdog reset 423 | select { 424 | case wdResetC <- struct{}{}: 425 | default: 426 | // Channel full, skip this reset signal 427 | } 428 | case <-ctx.Done(): 429 | cancel() 430 | return 431 | } 432 | 433 | // Have to make a copy so these don't get overwritten 434 | msgs, acks := make([]kawa.Message[T], len(d.buf)), make([]func(), len(d.buf)) 435 | for i, m := range d.buf { 436 | msgs[i] = m.msg 437 | acks[i] = m.ack 438 | } 439 | go func(id string, msgs []kawa.Message[T], acks []func()) { 440 | d.doflush(flctx, msgs, acks) 441 | // clear flush slot 442 | <-d.flushq 443 | // Flush completed - signal watchdog reset 444 | select { 445 | case wdResetC <- struct{}{}: 446 | default: 447 | // Channel full, skip this reset signal 448 | } 449 | // clear cancel 450 | d.syncMu.Lock() 451 | cncl := d.flushcan[id] 452 | delete(d.flushcan, id) 453 | d.syncMu.Unlock() 454 | cncl() 455 | }(id, msgs, acks) 456 | // Clear the buffer for the next batch 457 | d.buf = d.buf[:0] 458 | } 459 | 460 | func (d *Destination[T]) doflush(ctx context.Context, msgs []kawa.Message[T], acks []func()) { 461 | backoff := d.initialBackoff 462 | var flushErr error 463 | 464 | // Retry loop 465 | for attempt := 0; attempt <= d.maxRetries; attempt++ { 466 | // Create a fresh context with timeout for this attempt 467 | attemptCtx := ctx 468 | if d.flushTimeout > 0 { 469 | var cancel context.CancelFunc 470 | attemptCtx, cancel = context.WithTimeout(ctx, d.flushTimeout) 471 | defer cancel() 472 | } 473 | 474 | flushErr = d.flusher.Flush(attemptCtx, msgs) 475 | 476 | // Success case 477 | if flushErr == nil { 478 | if attempt > 0 { 479 | slog.Info("flush succeeded after retry", "attempt", attempt+1, "count", len(msgs)) 480 | } 481 | break 482 | } 483 | 484 | // Failure case - check if we should retry 485 | slog.Debug("flush err", "error", flushErr, "attempt", attempt+1) 486 | 487 | // Check if error is retriable using the callback (if provided) - retry if we have attempts left 488 | if d.isRetryable != nil && d.isRetryable(flushErr) && attempt < d.maxRetries { 489 | slog.Warn( 490 | "flush failed, will retry", 491 | "error", flushErr, 492 | "attempt", attempt+1, 493 | "maxRetries", d.maxRetries+1, 494 | "backoff", backoff, 495 | ) 496 | 497 | // Sleep with backoff before retrying 498 | select { 499 | case <-time.After(backoff): 500 | backoff = time.Duration(float64(backoff) * d.backoffMultiplier) 501 | if backoff > d.maxBackoff { 502 | backoff = d.maxBackoff 503 | } 504 | continue 505 | case <-ctx.Done(): 506 | flushErr = ctx.Err() 507 | break 508 | } 509 | } 510 | 511 | // Non-retriable error or exhausted retries - exit retry loop 512 | slog.Error("flush failed after all retries", "error", flushErr, "attempts", attempt+1) 513 | break 514 | } 515 | 516 | var handlerErr error 517 | if flushErr != nil { 518 | handlerErr = d.errorHandler.HandleError(ctx, flushErr, msgs) 519 | } else { 520 | handlerErr = nil 521 | } 522 | 523 | // If error handler returns ErrDontAck, skip acking but continue running 524 | if errors.Is(handlerErr, ErrDontAck) { 525 | return 526 | } 527 | 528 | // Error handler resolved the error - ack the messages 529 | if handlerErr == nil { 530 | for _, ack := range acks { 531 | if ack != nil { 532 | ack() 533 | } 534 | } 535 | return 536 | } 537 | 538 | // Error handler returned an error - propagate it to stop the batcher 539 | d.flusherr <- handlerErr 540 | } 541 | 542 | // only call ack on last message acknowledgement 543 | func ackFn(ack func(), num int) func() { 544 | ackChu := make(chan struct{}, num-1) 545 | for i := 0; i < num-1; i++ { 546 | ackChu <- struct{}{} 547 | } 548 | // bless you 549 | return func() { 550 | select { 551 | case <-ackChu: 552 | default: 553 | if ack != nil { 554 | ack() 555 | } 556 | } 557 | } 558 | } 559 | -------------------------------------------------------------------------------- /x/batcher/batcher_test.go: -------------------------------------------------------------------------------- 1 | package batch 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "sync" 7 | "sync/atomic" 8 | "testing" 9 | "time" 10 | 11 | "github.com/pkg/errors" 12 | "github.com/runreveal/kawa" 13 | "github.com/stretchr/testify/assert" 14 | ) 15 | 16 | func TestAckChu(t *testing.T) { 17 | var called bool 18 | callMe := ackFn(func() { called = true }, 2) 19 | for i := 0; i < 2; i++ { 20 | callMe() 21 | } 22 | assert.True(t, called, "ack should be called") 23 | 24 | nilMe := ackFn(nil, 2) 25 | for i := 0; i < 2; i++ { 26 | // shouldn't panic 27 | nilMe() 28 | } 29 | } 30 | 31 | // func flushTest[T any](c context.Context, msgs []kawa.Message[T]) { 32 | // for _, msg := range msgs { 33 | // fmt.Println(msg.Value) 34 | // } 35 | // counter++ 36 | // } 37 | 38 | func TestBatcher(t *testing.T) { 39 | 40 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 41 | for _, msg := range msgs { 42 | fmt.Println(msg.Value) 43 | } 44 | return nil 45 | } 46 | 47 | bat := NewDestination[string](FlushFunc[string](ff), Raise[string](), FlushLength(1)) 48 | 49 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 50 | 51 | errc := make(chan error) 52 | 53 | go func(c context.Context, ec chan error) { 54 | ec <- bat.Run(c) 55 | }(ctx, errc) 56 | 57 | writeMsgs := []kawa.Message[string]{ 58 | {Value: "hi"}, 59 | {Value: "hello"}, 60 | {Value: "bonjour"}, 61 | } 62 | 63 | done := make(chan struct{}) 64 | err := bat.Send(ctx, func() { close(done) }, writeMsgs...) 65 | assert.NoError(t, err) 66 | 67 | select { 68 | case err := <-errc: 69 | assert.NoError(t, err) 70 | case <-done: 71 | } 72 | cancel() 73 | 74 | } 75 | 76 | func TestBatchFlushTimeout(t *testing.T) { 77 | hMu := sync.Mutex{} 78 | handled := false 79 | 80 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 81 | for _, msg := range msgs { 82 | fmt.Println(msg.Value) 83 | } 84 | hMu.Lock() 85 | handled = true 86 | hMu.Unlock() 87 | return nil 88 | } 89 | 90 | bat := NewDestination[string]( 91 | FlushFunc[string](ff), 92 | Raise[string](), 93 | FlushFrequency(1*time.Millisecond), 94 | FlushLength(2), 95 | StopTimeout(10*time.Millisecond), 96 | ) 97 | 98 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 99 | 100 | errc := make(chan error) 101 | 102 | go func(c context.Context, ec chan error) { 103 | ec <- bat.Run(c) 104 | }(ctx, errc) 105 | 106 | done := make(chan struct{}) 107 | err := bat.Send(ctx, func() { close(done) }, kawa.Message[string]{Value: "hi"}) 108 | assert.NoError(t, err) 109 | 110 | time.Sleep(15 * time.Millisecond) 111 | 112 | hMu.Lock() 113 | assert.True(t, handled, "value should have been set!") 114 | hMu.Unlock() 115 | 116 | select { 117 | case err := <-errc: 118 | assert.NoError(t, err) 119 | case <-done: 120 | } 121 | cancel() 122 | 123 | } 124 | 125 | func TestBatcherErrors(t *testing.T) { 126 | flushErr := errors.New("flush error") 127 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 128 | return flushErr 129 | } 130 | 131 | t.Run("flush errors return from run", func(t *testing.T) { 132 | bat := NewDestination[string](FlushFunc[string](ff), Raise[string](), FlushLength(1)) 133 | errc := make(chan error) 134 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 135 | 136 | go func(c context.Context, ec chan error) { 137 | ec <- bat.Run(c) 138 | }(ctx, errc) 139 | 140 | writeMsgs := []kawa.Message[string]{ 141 | {Value: "hi"}, 142 | } 143 | 144 | done := make(chan struct{}) 145 | err := bat.Send(ctx, func() { close(done) }, writeMsgs...) 146 | assert.NoError(t, err) 147 | 148 | select { 149 | case err := <-errc: 150 | assert.EqualError(t, err, "flush error") 151 | case <-done: 152 | } 153 | cancel() 154 | }) 155 | 156 | t.Run("cancellation works", func(t *testing.T) { 157 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 158 | return nil 159 | } 160 | bat := NewDestination[string](FlushFunc[string](ff), Raise[string](), FlushLength(1)) 161 | errc := make(chan error) 162 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 163 | go func(c context.Context, ec chan error) { 164 | ec <- bat.Run(c) 165 | }(ctx, errc) 166 | 167 | cancel() 168 | err := <-errc 169 | assert.ErrorIs(t, err, nil, "should return nil since no errors in flush") 170 | }) 171 | 172 | t.Run("deadlock cancellation", func(t *testing.T) { 173 | 174 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 175 | <-c.Done() 176 | return nil 177 | } 178 | bat := NewDestination[string](FlushFunc[string](ff), Raise[string](), FlushLength(1), StopTimeout(10*time.Millisecond)) 179 | 180 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 181 | 182 | errc := make(chan error) 183 | 184 | go func(c context.Context, ec chan error) { 185 | ec <- bat.Run(c) 186 | }(ctx, errc) 187 | 188 | writeMsgs := []kawa.Message[string]{ 189 | // will be blocked flushing 190 | {Value: "hi"}, 191 | // will be stuck waiting for flush slot 192 | {Value: "hello"}, 193 | // will be stuck waiting to write to msgs in Send 194 | {Value: "bonjour"}, 195 | } 196 | 197 | done := make(chan struct{}) 198 | err := bat.Send(ctx, func() { close(done) }, writeMsgs...) 199 | assert.NoError(t, err) 200 | cancel() 201 | 202 | err = <-errc 203 | assert.ErrorIs(t, err, errDeadlock, "should return deadlock error") 204 | }) 205 | 206 | t.Run("handle errors when errors returned from flush", func(t *testing.T) { 207 | 208 | // This test deadlocks in failure 209 | // Should figure out how to write it better 210 | 211 | flushErr := errors.New("flush error") 212 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 213 | time.Sleep(110 * time.Millisecond) 214 | return flushErr 215 | } 216 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 217 | assert.ErrorIs(t, err, flushErr) 218 | return err 219 | }) 220 | bat := NewDestination[string]( 221 | FlushFunc[string](ff), 222 | errHandler, 223 | FlushLength(2), 224 | FlushParallelism(2), 225 | StopTimeout(90*time.Millisecond), 226 | ) 227 | errc := make(chan error) 228 | 229 | ctx, cncl := context.WithCancel(context.Background()) 230 | 231 | go func(c context.Context, ec chan error) { 232 | ec <- bat.Run(c) 233 | }(ctx, errc) 234 | 235 | writeMsgs := []kawa.Message[string]{ 236 | // will be blocked flushing 237 | {Value: "hi"}, 238 | // will be stuck waiting for flush slot 239 | {Value: "hello"}, 240 | // will be stuck waiting to write to msgs in Send 241 | {Value: "bonjour"}, 242 | } 243 | 244 | done := make(chan struct{}) 245 | err := bat.Send(ctx, func() { close(done) }, writeMsgs...) 246 | assert.NoError(t, err, "errors aren't returned from Send") 247 | 248 | cncl() 249 | 250 | // parallelism is 2, so max processing time is 220ms (110ms for the first 251 | // two msgs in parallel, and another 110ms for the third) 252 | // stop timeout of 90ms means we'll see the deadlock error 253 | err = <-errc 254 | assert.ErrorIs(t, err, errDeadlock) 255 | }) 256 | 257 | t.Run("handle errors when errors returned from flush", func(t *testing.T) { 258 | 259 | // This test deadlocks in failure 260 | // Should figure out how to write it better 261 | 262 | flushErr := errors.New("flush error") 263 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 264 | time.Sleep(110 * time.Millisecond) 265 | return flushErr 266 | } 267 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 268 | assert.ErrorIs(t, err, flushErr) 269 | return err 270 | }) 271 | bat := NewDestination[string]( 272 | FlushFunc[string](ff), 273 | errHandler, 274 | FlushLength(2), 275 | FlushParallelism(2), 276 | StopTimeout(90*time.Millisecond), 277 | ) 278 | errc := make(chan error) 279 | 280 | ctx, cncl := context.WithCancel(context.Background()) 281 | 282 | go func(c context.Context, ec chan error) { 283 | ec <- bat.Run(c) 284 | }(ctx, errc) 285 | 286 | writeMsgs := []kawa.Message[string]{ 287 | // will be blocked flushing 288 | {Value: "hi"}, 289 | // will be stuck waiting for flush slot 290 | {Value: "hello"}, 291 | // will be stuck waiting to write to msgs in Send 292 | {Value: "bonjour"}, 293 | } 294 | 295 | done := make(chan struct{}) 296 | err := bat.Send(ctx, func() { close(done) }, writeMsgs...) 297 | assert.NoError(t, err, "errors aren't returned from Send") 298 | 299 | cncl() 300 | 301 | // parallelism is 2, so max processing time is 220ms (110ms for the first 302 | // two msgs in parallel, and another 110ms for the third) 303 | // stop timeout of 90ms means we'll see the deadlock error 304 | err = <-errc 305 | assert.ErrorIs(t, err, errDeadlock) 306 | }) 307 | 308 | t.Run("dont deadlock on errors returned from flush with length 1", func(t *testing.T) { 309 | 310 | // This test deadlocks in failure 311 | // Should figure out how to write it better 312 | 313 | flushErr := errors.New("flush error") 314 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 315 | time.Sleep(5 * time.Millisecond) 316 | return flushErr 317 | } 318 | bat := NewDestination[string](FlushFunc[string](ff), Raise[string](), FlushLength(1), FlushParallelism(2), 319 | StopTimeout(100*time.Millisecond)) 320 | errc := make(chan error) 321 | 322 | ctx := context.Background() 323 | 324 | go func(c context.Context, ec chan error) { 325 | ec <- bat.Run(c) 326 | }(ctx, errc) 327 | 328 | writeMsgs := []kawa.Message[string]{ 329 | // will be blocked flushing 330 | {Value: "hi"}, 331 | // will be stuck waiting for flush slot 332 | {Value: "hello"}, 333 | // will be stuck waiting to write to msgs in Send 334 | {Value: "bonjour"}, 335 | } 336 | 337 | done := make(chan struct{}) 338 | err := bat.Send(ctx, func() { close(done) }, writeMsgs...) 339 | assert.NoError(t, err) 340 | 341 | err = <-errc 342 | assert.ErrorIs(t, err, flushErr) 343 | }) 344 | 345 | t.Run("Don't ack messages if flush handler returns ErrDontAck", func(t *testing.T) { 346 | var retryHandler = func(ctx context.Context, err error, msgs []kawa.Message[string]) error { 347 | return ErrDontAck 348 | } 349 | bat := NewDestination[string]( 350 | FlushFunc[string](ff), 351 | ErrorFunc[string](retryHandler), 352 | FlushLength(1), 353 | FlushParallelism(1), 354 | ) 355 | errc := make(chan error) 356 | 357 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) 358 | 359 | go func(c context.Context, ec chan error) { 360 | ec <- bat.Run(c) 361 | }(ctx, errc) 362 | 363 | messages := []kawa.Message[string]{ 364 | {Value: "one"}, 365 | {Value: "two"}, 366 | {Value: "three"}, 367 | {Value: "ten"}, 368 | } 369 | 370 | ackCount := 0 371 | err := bat.Send(ctx, func() { ackCount += 1 }, messages...) 372 | assert.NoError(t, err) 373 | time.Sleep(50 * time.Millisecond) 374 | cancel() 375 | 376 | err = <-errc 377 | assert.ErrorIs(t, err, nil) 378 | 379 | assert.Equal(t, 0, ackCount) 380 | }) 381 | } 382 | 383 | func TestBatcherRetry(t *testing.T) { 384 | t.Run("retry on retriable error and succeed", func(t *testing.T) { 385 | var attemptCount atomic.Int32 386 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 387 | count := attemptCount.Add(1) 388 | if count < 3 { 389 | return errors.New("temporary error") 390 | } 391 | return nil 392 | } 393 | 394 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 395 | // Just pass through the error 396 | return err 397 | }) 398 | 399 | bat := NewDestination[string]( 400 | FlushFunc[string](ff), 401 | errHandler, 402 | FlushLength(1), 403 | MaxRetries(3), 404 | InitialBackoff(10*time.Millisecond), 405 | IsRetryable(func(err error) bool { 406 | return err != nil && err.Error() == "temporary error" 407 | }), 408 | ) 409 | 410 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 411 | defer cancel() 412 | 413 | errc := make(chan error) 414 | go func(c context.Context, ec chan error) { 415 | ec <- bat.Run(c) 416 | }(ctx, errc) 417 | 418 | ackChan := make(chan struct{}) 419 | err := bat.Send(ctx, func() { close(ackChan) }, kawa.Message[string]{Value: "hi"}) 420 | assert.NoError(t, err) 421 | 422 | // Wait for ack to happen 423 | select { 424 | case <-ackChan: 425 | // Success - message was acked 426 | case <-time.After(200 * time.Millisecond): 427 | t.Fatal("timeout waiting for ack") 428 | } 429 | 430 | cancel() 431 | err = <-errc 432 | assert.NoError(t, err) 433 | assert.Equal(t, int32(3), attemptCount.Load(), "should have made 3 attempts") 434 | }) 435 | 436 | t.Run("retry exhausts max attempts", func(t *testing.T) { 437 | var attemptCount atomic.Int32 438 | flushErr := errors.New("persistent error") 439 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 440 | attemptCount.Add(1) 441 | return flushErr 442 | } 443 | 444 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 445 | return err 446 | }) 447 | 448 | bat := NewDestination[string]( 449 | FlushFunc[string](ff), 450 | errHandler, 451 | FlushLength(1), 452 | MaxRetries(2), 453 | InitialBackoff(5*time.Millisecond), 454 | IsRetryable(func(err error) bool { 455 | return err != nil && err.Error() == "persistent error" 456 | }), 457 | ) 458 | 459 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 460 | defer cancel() 461 | 462 | errc := make(chan error) 463 | go func(c context.Context, ec chan error) { 464 | ec <- bat.Run(c) 465 | }(ctx, errc) 466 | 467 | ackCount := 0 468 | err := bat.Send(ctx, func() { ackCount++ }, kawa.Message[string]{Value: "hi"}) 469 | assert.NoError(t, err) 470 | 471 | err = <-errc 472 | assert.ErrorIs(t, err, flushErr) 473 | assert.Equal(t, int32(3), attemptCount.Load(), "should have made 3 attempts (initial + 2 retries)") 474 | assert.Equal(t, 0, ackCount, "should not have acked") 475 | }) 476 | 477 | t.Run("no retry on non-retriable error", func(t *testing.T) { 478 | var attemptCount atomic.Int32 479 | flushErr := errors.New("non-retriable error") 480 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 481 | attemptCount.Add(1) 482 | return flushErr 483 | } 484 | 485 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 486 | // Just pass through the error 487 | return err 488 | }) 489 | 490 | bat := NewDestination[string]( 491 | FlushFunc[string](ff), 492 | errHandler, 493 | FlushLength(1), 494 | MaxRetries(3), 495 | ) 496 | 497 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 498 | defer cancel() 499 | 500 | errc := make(chan error) 501 | go func(c context.Context, ec chan error) { 502 | ec <- bat.Run(c) 503 | }(ctx, errc) 504 | 505 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: "hi"}) 506 | assert.NoError(t, err) 507 | 508 | err = <-errc 509 | assert.ErrorIs(t, err, flushErr) 510 | assert.Equal(t, int32(1), attemptCount.Load(), "should have made only 1 attempt") 511 | }) 512 | 513 | t.Run("retry with exponential backoff", func(t *testing.T) { 514 | attemptTimes := []time.Time{} 515 | flushErr := errors.New("error") 516 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 517 | attemptTimes = append(attemptTimes, time.Now()) 518 | return flushErr 519 | } 520 | 521 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 522 | return err 523 | }) 524 | 525 | bat := NewDestination[string]( 526 | FlushFunc[string](ff), 527 | errHandler, 528 | FlushLength(1), 529 | MaxRetries(2), 530 | InitialBackoff(50*time.Millisecond), 531 | BackoffMultiplier(2.0), 532 | IsRetryable(func(err error) bool { 533 | return err != nil 534 | }), 535 | ) 536 | 537 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 538 | defer cancel() 539 | 540 | errc := make(chan error) 541 | go func(c context.Context, ec chan error) { 542 | ec <- bat.Run(c) 543 | }(ctx, errc) 544 | 545 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: "hi"}) 546 | assert.NoError(t, err) 547 | 548 | err = <-errc 549 | assert.ErrorIs(t, err, flushErr) 550 | assert.Equal(t, 3, len(attemptTimes), "should have 3 attempts") 551 | 552 | // Check backoff timing (with some tolerance) 553 | timeBetween1and2 := attemptTimes[1].Sub(attemptTimes[0]) 554 | assert.GreaterOrEqual(t, timeBetween1and2, 50*time.Millisecond) 555 | assert.Less(t, timeBetween1and2, 70*time.Millisecond) 556 | 557 | timeBetween2and3 := attemptTimes[2].Sub(attemptTimes[1]) 558 | assert.GreaterOrEqual(t, timeBetween2and3, 100*time.Millisecond) 559 | assert.Less(t, timeBetween2and3, 120*time.Millisecond) 560 | }) 561 | 562 | t.Run("ErrDontAck takes precedence over retry", func(t *testing.T) { 563 | var attemptCount atomic.Int32 564 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 565 | attemptCount.Add(1) 566 | return errors.New("error") 567 | } 568 | 569 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 570 | // Return ErrDontAck, should not retry 571 | return ErrDontAck 572 | }) 573 | 574 | bat := NewDestination[string]( 575 | FlushFunc[string](ff), 576 | errHandler, 577 | FlushLength(1), 578 | MaxRetries(3), 579 | ) 580 | 581 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 582 | defer cancel() 583 | 584 | errc := make(chan error) 585 | go func(c context.Context, ec chan error) { 586 | ec <- bat.Run(c) 587 | }(ctx, errc) 588 | 589 | ackCount := 0 590 | err := bat.Send(ctx, func() { ackCount++ }, kawa.Message[string]{Value: "hi"}) 591 | assert.NoError(t, err) 592 | 593 | time.Sleep(50 * time.Millisecond) 594 | cancel() 595 | 596 | err = <-errc 597 | assert.NoError(t, err) 598 | assert.Equal(t, int32(1), attemptCount.Load(), "should have made only 1 attempt") 599 | assert.Equal(t, 0, ackCount, "should not have acked") 600 | }) 601 | 602 | t.Run("retry respects FlushTimeout per attempt", func(t *testing.T) { 603 | var attemptCount atomic.Int32 604 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 605 | attemptCount.Add(1) 606 | // Sleep longer than timeout to trigger deadline exceeded 607 | time.Sleep(100 * time.Millisecond) 608 | return c.Err() 609 | } 610 | 611 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 612 | return err 613 | }) 614 | 615 | bat := NewDestination[string]( 616 | FlushFunc[string](ff), 617 | errHandler, 618 | FlushLength(1), 619 | FlushTimeout(50*time.Millisecond), // Timeout shorter than flush operation 620 | MaxRetries(2), 621 | InitialBackoff(10*time.Millisecond), 622 | IsRetryable(func(err error) bool { 623 | return errors.Is(err, context.DeadlineExceeded) 624 | }), 625 | ) 626 | 627 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) 628 | defer cancel() 629 | 630 | errc := make(chan error) 631 | go func(c context.Context, ec chan error) { 632 | ec <- bat.Run(c) 633 | }(ctx, errc) 634 | 635 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: "hi"}) 636 | assert.NoError(t, err) 637 | 638 | err = <-errc 639 | assert.ErrorIs(t, err, context.DeadlineExceeded) 640 | // Should have tried 3 times, each time getting timeout 641 | assert.Equal(t, int32(3), attemptCount.Load(), "should have made 3 attempts") 642 | }) 643 | 644 | t.Run("zero retries means no retry", func(t *testing.T) { 645 | var attemptCount atomic.Int32 646 | flushErr := errors.New("error") 647 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 648 | attemptCount.Add(1) 649 | return flushErr 650 | } 651 | 652 | var errHandler = ErrorFunc[string](func(c context.Context, err error, msgs []kawa.Message[string]) error { 653 | return err 654 | }) 655 | 656 | bat := NewDestination[string]( 657 | FlushFunc[string](ff), 658 | errHandler, 659 | FlushLength(1), 660 | MaxRetries(0), // No retries 661 | IsRetryable(func(err error) bool { 662 | return err != nil 663 | }), 664 | ) 665 | 666 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 667 | defer cancel() 668 | 669 | errc := make(chan error) 670 | go func(c context.Context, ec chan error) { 671 | ec <- bat.Run(c) 672 | }(ctx, errc) 673 | 674 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: "hi"}) 675 | assert.NoError(t, err) 676 | 677 | err = <-errc 678 | assert.ErrorIs(t, err, flushErr) 679 | assert.Equal(t, int32(1), attemptCount.Load(), "should have made only initial attempt") 680 | }) 681 | } 682 | 683 | func TestWatchdog(t *testing.T) { 684 | t.Run("idle system does not trigger watchdog", func(t *testing.T) { 685 | var flushCount atomic.Int32 686 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 687 | flushCount.Add(1) 688 | return nil 689 | } 690 | 691 | bat := NewDestination[string]( 692 | FlushFunc[string](ff), 693 | Raise[string](), 694 | FlushLength(10), 695 | FlushFrequency(50*time.Millisecond), 696 | WatchdogTimeout(100*time.Millisecond), 697 | ) 698 | 699 | ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) 700 | defer cancel() 701 | 702 | errc := make(chan error) 703 | go func(c context.Context, ec chan error) { 704 | ec <- bat.Run(c) 705 | }(ctx, errc) 706 | 707 | // Send one message, which will flush after 50ms 708 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: "message1"}) 709 | assert.NoError(t, err) 710 | 711 | // Wait for flush to happen 712 | time.Sleep(70 * time.Millisecond) 713 | 714 | // Now system is idle. Watchdog is 100ms, but no flushes are in-flight 715 | // so it should reset and not error 716 | time.Sleep(150 * time.Millisecond) 717 | 718 | cancel() 719 | err = <-errc 720 | assert.NoError(t, err, "idle system should not trigger watchdog") 721 | assert.Equal(t, int32(1), flushCount.Load(), "should have flushed once") 722 | }) 723 | 724 | t.Run("stuck flush with no new messages triggers watchdog", func(t *testing.T) { 725 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 726 | // Simulate a flush that ignores context cancellation 727 | time.Sleep(1 * time.Second) 728 | return nil 729 | } 730 | 731 | bat := NewDestination[string]( 732 | FlushFunc[string](ff), 733 | Raise[string](), 734 | FlushLength(1), 735 | FlushTimeout(50*time.Millisecond), // Context will be cancelled, but flush ignores it 736 | WatchdogTimeout(150*time.Millisecond), 737 | ) 738 | 739 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) 740 | defer cancel() 741 | 742 | errc := make(chan error) 743 | go func(c context.Context, ec chan error) { 744 | ec <- bat.Run(c) 745 | }(ctx, errc) 746 | 747 | // Send one message which will trigger a flush 748 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: "message1"}) 749 | assert.NoError(t, err) 750 | 751 | // Watchdog should fire after 150ms because flush is stuck 752 | err = <-errc 753 | assert.ErrorIs(t, err, errDeadlock, "stuck flush should trigger watchdog") 754 | }) 755 | 756 | t.Run("stuck flush with continuing message arrival triggers watchdog", func(t *testing.T) { 757 | var flushCount atomic.Int32 758 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 759 | count := flushCount.Add(1) 760 | if count == 1 { 761 | // First flush gets stuck and ignores context 762 | time.Sleep(1 * time.Second) 763 | return nil 764 | } 765 | // Subsequent flushes complete quickly 766 | return nil 767 | } 768 | 769 | bat := NewDestination[string]( 770 | FlushFunc[string](ff), 771 | Raise[string](), 772 | FlushLength(1), 773 | FlushParallelism(2), // Allow parallel flushes 774 | FlushTimeout(50*time.Millisecond), 775 | WatchdogTimeout(200*time.Millisecond), 776 | ) 777 | 778 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) 779 | defer cancel() 780 | 781 | errc := make(chan error) 782 | go func(c context.Context, ec chan error) { 783 | ec <- bat.Run(c) 784 | }(ctx, errc) 785 | 786 | // Send first message - this will get stuck 787 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: "message1"}) 788 | assert.NoError(t, err) 789 | 790 | // Wait a bit for first flush to start 791 | time.Sleep(30 * time.Millisecond) 792 | 793 | // Send more messages periodically to keep resetting the old watchdog 794 | // With our new implementation, this should still detect the stuck flush 795 | for i := 0; i < 3; i++ { 796 | time.Sleep(80 * time.Millisecond) 797 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: fmt.Sprintf("message%d", i+2)}) 798 | assert.NoError(t, err) 799 | } 800 | 801 | // Watchdog should eventually fire because first flush is stuck 802 | err = <-errc 803 | assert.ErrorIs(t, err, errDeadlock, "stuck flush should trigger watchdog even with new messages") 804 | }) 805 | 806 | t.Run("watchdog resets on flush completion", func(t *testing.T) { 807 | var flushCount atomic.Int32 808 | var ff = func(c context.Context, msgs []kawa.Message[string]) error { 809 | flushCount.Add(1) 810 | // Each flush takes 80ms, but completes successfully 811 | time.Sleep(80 * time.Millisecond) 812 | return nil 813 | } 814 | 815 | bat := NewDestination[string]( 816 | FlushFunc[string](ff), 817 | Raise[string](), 818 | FlushLength(1), 819 | WatchdogTimeout(200*time.Millisecond), 820 | ) 821 | 822 | ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) 823 | defer cancel() 824 | 825 | errc := make(chan error) 826 | go func(c context.Context, ec chan error) { 827 | ec <- bat.Run(c) 828 | }(ctx, errc) 829 | 830 | // Send 3 messages, each will flush independently 831 | for i := 0; i < 3; i++ { 832 | err := bat.Send(ctx, nil, kawa.Message[string]{Value: fmt.Sprintf("message%d", i+1)}) 833 | assert.NoError(t, err) 834 | } 835 | 836 | // Wait for all flushes to complete 837 | time.Sleep(300 * time.Millisecond) 838 | 839 | cancel() 840 | err := <-errc 841 | assert.NoError(t, err, "watchdog should not fire when flushes complete successfully") 842 | assert.Equal(t, int32(3), flushCount.Load(), "should have completed 3 flushes") 843 | }) 844 | } 845 | --------------------------------------------------------------------------------