-
Notifications
You must be signed in to change notification settings - Fork 0
docs+ci+refactor: staff-level README, CI/CD, drop global Redis singleton #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| vet: | ||
| name: vet | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version-file: go.mod | ||
| cache: true | ||
| - run: go vet ./... | ||
|
|
||
| lint: | ||
| name: lint | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version-file: go.mod | ||
| cache: true | ||
| - uses: golangci/golangci-lint-action@v9 | ||
| with: | ||
| version: v2.12 | ||
|
|
||
| build: | ||
| name: build | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version-file: go.mod | ||
| cache: true | ||
| - run: go build ./... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| name: Security | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| schedule: | ||
| - cron: '0 6 * * 1' | ||
|
|
||
| permissions: | ||
| contents: read | ||
| security-events: write | ||
|
|
||
| jobs: | ||
| gosec: | ||
| name: gosec | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Run gosec | ||
| uses: securego/gosec@v2.26.1 | ||
| with: | ||
| args: '-no-fail -fmt sarif -out gosec.sarif ./...' | ||
| - name: Upload SARIF | ||
| uses: github/codeql-action/upload-sarif@v3 | ||
| with: | ||
| sarif_file: gosec.sarif | ||
| category: gosec | ||
|
|
||
| govulncheck: | ||
| name: govulncheck | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version: 'stable' | ||
| cache: true | ||
| - run: go install golang.org/x/vuln/cmd/govulncheck@latest | ||
| - run: govulncheck ./... | ||
|
|
||
| trivy: | ||
| name: trivy-fs | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Run Trivy filesystem scan | ||
| uses: aquasecurity/trivy-action@v0.36.0 | ||
| with: | ||
| scan-type: fs | ||
| format: sarif | ||
| output: trivy.sarif | ||
| ignore-unfixed: true | ||
| severity: 'HIGH,CRITICAL' | ||
| - name: Upload SARIF | ||
| uses: github/codeql-action/upload-sarif@v3 | ||
| with: | ||
| sarif_file: trivy.sarif | ||
| category: trivy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| version: "2" | ||
|
|
||
| run: | ||
| timeout: 3m | ||
|
|
||
| linters: | ||
| default: none | ||
| enable: | ||
| - bodyclose | ||
| - errcheck | ||
| - gocritic | ||
| - govet | ||
| - ineffassign | ||
| - misspell | ||
| - prealloc | ||
| - revive | ||
| - staticcheck | ||
| - unconvert | ||
| - unused | ||
|
|
||
| settings: | ||
| revive: | ||
| rules: | ||
| - name: var-naming | ||
| - name: empty-block | ||
| - name: unused-parameter | ||
|
|
||
| issues: | ||
| max-issues-per-linter: 0 | ||
| max-same-issues: 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,134 @@ | ||
| # taskq | ||
| Distributing task across multiple workers to manage the concurrency | ||
|
|
||
| > A scaffold for a Redis-backed distributed task queue in Go — Viper-driven config, single-instance Redis client, ready for fan-out + worker pool implementation. | ||
|
|
||
| [](https://github.com/Chetas1/taskq/actions/workflows/ci.yml) | ||
| [](https://github.com/Chetas1/taskq/actions/workflows/security.yml) | ||
| [](https://go.dev/dl/) | ||
| [](https://redis.io) | ||
|
|
||
| > **Status: scaffold.** This repo currently boots, loads YAML config, and connects to Redis. The producer/worker logic is the next milestone — see [Roadmap](#roadmap). | ||
|
|
||
| --- | ||
|
|
||
| ## Why this exists | ||
|
|
||
| A reference Go scaffold for the building blocks of a distributed task queue: | ||
|
|
||
| - **Viper-driven config** with file + env override (per `12-Factor`). | ||
| - **Single, dependency-injected Redis client** (avoids the global-singleton trap). | ||
| - **Clear seams** — `config`, `internal/app` — so producer and worker logic can be added without restructuring. | ||
|
|
||
| ## Target Architecture | ||
|
|
||
| ```mermaid | ||
| flowchart LR | ||
| subgraph Producers | ||
| P1[HTTP / gRPC API] | ||
| P2[Cron / scheduler] | ||
| end | ||
|
|
||
| subgraph Redis Streams | ||
| S[(Stream: tasks)] | ||
| G1[Consumer Group: workers] | ||
| end | ||
|
|
||
| subgraph Workers | ||
| W1[Worker 1] | ||
| W2[Worker 2] | ||
| Wn[Worker N] | ||
| end | ||
|
|
||
| DLQ[(Stream: tasks-dead)] | ||
| M[(Metrics / Tracing)] | ||
|
|
||
| P1 -- XADD --> S | ||
| P2 -- XADD --> S | ||
| S --- G1 | ||
| G1 -- XREADGROUP --> W1 | ||
| G1 -- XREADGROUP --> W2 | ||
| G1 -- XREADGROUP --> Wn | ||
| W1 -. retries exhausted .-> DLQ | ||
| W2 -. metrics .-> M | ||
| ``` | ||
|
|
||
| ## Today's Code Path | ||
|
|
||
| ```mermaid | ||
| sequenceDiagram | ||
| autonumber | ||
| participant Bin as cmd/main | ||
| participant Cfg as config.LoadConfig | ||
| participant App as app.Init | ||
| participant R as Redis | ||
| Bin->>Cfg: read config/config.yaml + env | ||
| Cfg-->>Bin: *Config{Redis{Addr,Password,DB}} | ||
| Bin->>App: Init(cfg) | ||
| App->>R: redis.NewClient + PING | ||
| R-->>App: PONG | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| `config/config.yaml`: | ||
|
|
||
| ```yaml | ||
| redis: | ||
| addr: "localhost:6379" | ||
| password: "" | ||
| db: 0 | ||
| ``` | ||
|
|
||
| Env overrides (`viper.AutomaticEnv` planned): `REDIS_ADDR`, `REDIS_PASSWORD`, `REDIS_DB`. | ||
|
|
||
| ## Roadmap | ||
|
|
||
| | Milestone | Status | | ||
| |--------------------------------------------------|--------| | ||
| | Boot + Redis health-check | ✅ | | ||
| | Producer API: `Enqueue(task) -> taskID` | ⏳ | | ||
| | Worker pool with at-least-once semantics | ⏳ | | ||
| | Retry policy (exponential backoff + max attempts)| ⏳ | | ||
| | Dead-letter stream | ⏳ | | ||
| | Idempotency keys + dedupe | ⏳ | | ||
| | OpenTelemetry tracing | ⏳ | | ||
| | Prometheus metrics (queue depth, age, p99 latency) | ⏳ | | ||
| | Graceful shutdown (drain in-flight, ack) on SIGTERM | ⏳ | | ||
| | Integration tests with `dockertest` / `miniredis`| ⏳ | | ||
|
|
||
| ## Operational Concerns (when implemented) | ||
|
|
||
| - **At-least-once delivery.** Plan to use Redis Streams + consumer groups; commit offsets with `XACK` only after handler succeeds. | ||
| - **Visibility timeouts.** `XCLAIM` on pending entries past the timeout (e.g. 5× expected handler latency). | ||
| - **Retries.** Exponential backoff with jitter; cap retries; drop to DLQ on exhaustion. | ||
| - **Backpressure.** Bound stream length with `MAXLEN ~ N`; expose queue depth as a metric. | ||
| - **Graceful shutdown.** Stop accepting new tasks, wait for in-flight workers up to a timeout, then `XCLAIM` to a clean-up consumer. | ||
|
|
||
| ## Security Considerations | ||
|
|
||
| - **TLS to Redis.** Today: plaintext (`localhost`). Production: `rediss://` with CA bundle and cert pinning. | ||
| - **Auth.** Today: `password` in YAML. Production: env var or secret-manager-injected, never committed. | ||
| - **Payload trust.** Workers must treat task payloads as untrusted — validate, bound, and rate-limit. | ||
| - **Egress from workers.** If workers call external APIs, sandbox with timeouts + circuit breakers. | ||
|
|
||
| ## Development | ||
|
|
||
| ```bash | ||
| # Boot Redis (one-shot) | ||
| docker run -d --name redis-taskq -p 6379:6379 redis:7-alpine | ||
|
|
||
| # Run | ||
| go run . | ||
|
|
||
| # Lint + vet + vulnerability scan | ||
| go vet ./... | ||
| golangci-lint run | ||
| govulncheck ./... | ||
|
|
||
| # Stop Redis | ||
| docker rm -f redis-taskq | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| See [LICENSE](LICENSE). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,45 @@ | ||
| // Package config loads runtime configuration from `config/config.yaml` | ||
| // and environment variables (via Viper's AutomaticEnv). Environment variables | ||
| // override file values; nested keys use `_` separators (e.g. `REDIS_ADDR`). | ||
| package config | ||
|
|
||
| import ( | ||
| "log" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| // Config is the top-level taskq configuration. | ||
| type Config struct { | ||
| Redis Redis | ||
| } | ||
|
|
||
| // Redis holds the connection details for the backing Redis instance / cluster. | ||
| type Redis struct { | ||
| Addr string | ||
| Password string | ||
| DB int | ||
| } | ||
|
|
||
| func LoadConfig() *Config { | ||
| // LoadConfig reads `config/config.yaml`, applies environment overrides, and | ||
| // returns the parsed Config. Returns a wrapped error if the file is missing | ||
| // or the YAML cannot be unmarshalled. | ||
| func LoadConfig() (*Config, error) { | ||
| v := viper.New() | ||
|
|
||
| v.SetConfigName("config") | ||
| v.SetConfigType("yaml") | ||
| v.AddConfigPath("./config/") | ||
| v.AutomaticEnv() | ||
|
|
||
| if err := v.ReadInConfig(); err != nil { | ||
| log.Fatal(err) | ||
| return nil, fmt.Errorf("read config: %w", err) | ||
| } | ||
|
|
||
| var config Config | ||
| if err := v.Unmarshal(&config); err != nil { | ||
| log.Fatal(err) | ||
| var cfg Config | ||
| if err := v.Unmarshal(&cfg); err != nil { | ||
| return nil, fmt.Errorf("unmarshal config: %w", err) | ||
| } | ||
|
|
||
| return &config | ||
| return &cfg, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Missing
SetEnvKeyReplacermakes env var overrides for nested config keys silently ineffectivev.AutomaticEnv()is called atconfig/config.go:33, and the doc comment at line 1-3 explicitly claims environment variables override file values with_separators (e.g.REDIS_ADDR). However, Viper maps config keyredis.addrto env varREDIS.ADDRby default (uppercased with literal.). Without callingv.SetEnvKeyReplacer(strings.NewReplacer(".", "_")), environment variables likeREDIS_ADDR,REDIS_PASSWORD, andREDIS_DBwill be silently ignored, and the YAML file values will always win. This makes the env override feature broken despite being wired up.Was this helpful? React with 👍 or 👎 to provide feedback.