From d0934bc4dab14eda5fa45b9f80956fbd947cfd7c Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Thu, 12 Jan 2023 10:31:29 +0000 Subject: [PATCH] docs: add jetstream configs and instructions * add Makefile to replace bash script --- .gitignore | 1 + Makefile | 17 ++++++++++++++ docker-compose.yaml | 13 ++++++++++- docs/jetsream.md | 56 +++++++++++++++++++++++++++++++++++++++++++++ run_tests.sh | 4 ---- 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 Makefile create mode 100644 docs/jetsream.md delete mode 100644 run_tests.sh diff --git a/.gitignore b/.gitignore index d4f63f1..2914fae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ covprofile .env .env.test +cic-chain-events diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0ae19ef --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +BIN := cic-chain-events + +.PHONY: build + +build: + CGO_ENABLED=1 go build -v -ldflags="-s -w" -o ${BIN} cmd/*.go + +run: + CGO_ENABLED=1 go run -ldflags="-s -w" cmd/*.go + +mod: + go mod tidy + go mod verify + +test: + source .env.test + go test -v -covermode atomic -coverprofile=covprofile ./internal/... \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 523a9b5..7d6422e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -17,6 +17,17 @@ services: interval: 10s timeout: 5s retries: 5 + nats: + image: nats:2.9 + restart: unless-stopped + command: "-js -sd /nats/data" + volumes: + - cic-indexer-nats:/nats/data + ports: + - '4222:4222' + - '8222:8222' volumes: cic-indexer-pg: - driver: local \ No newline at end of file + driver: local + cic-indexer-nats: + driver: local \ No newline at end of file diff --git a/docs/jetsream.md b/docs/jetsream.md new file mode 100644 index 0000000..ed91f9d --- /dev/null +++ b/docs/jetsream.md @@ -0,0 +1,56 @@ +## NATS JetStream + +### Server setup + +- Enable `-js -sd` + +## Stream setup + +```go +_, err = js.AddStream(&nats.StreamConfig{ + Name: streamName, + // Remove from JS once Acked (Should not be used with 1 consumer which acts as a relayer e.g. Benthos). + // Retention: nats.WorkQueuePolicy, + // MaxAge allows us to replay it within 48 hrs + MaxAge: time.Hour * 48, + Storage: nats.FileStorage, + Subjects: []string{streamSubjects}, + // Sliding window dedup period. + Duplicates: time.Minute * 5, +}) +``` + +## Producer + +```go +// nats.MsgId is the unique identifier for dedup +ctx.Publish("*", []byte("*"), nats.MsgId("*")) +``` + +## Consumer setup + +- Explicit ACK +- Durable +- Deliver: all + +### Benthos example + +```toml +input: + label: jetstream + nats_jetstream: + urls: + - nats://127.0.0.1:4222 + subject: "*" + durable: benthos + deliver: all + output: + stdout: + codec: lines +``` + +### Replay example + +```bash +nats sub "*" --all --start-sequence=$N +``` \ No newline at end of file diff --git a/run_tests.sh b/run_tests.sh deleted file mode 100644 index 869445b..0000000 --- a/run_tests.sh +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/bash -set -e - -source .env.test && go test -v -covermode atomic -coverprofile=covprofile ./internal/...