Skip to content

Commit

Permalink
Merge pull request #1193 from thedataflows/main
Browse files Browse the repository at this point in the history
New storage driver: NATS
  • Loading branch information
ReneWerner87 authored Feb 7, 2024
2 parents 1cd2f10 + 8f4e626 commit 71ee192
Show file tree
Hide file tree
Showing 15 changed files with 1,014 additions and 2 deletions.
8 changes: 7 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,10 @@ updates:
labels:
- "🤖 Dependencies"
schedule:
interval: "daily"
interval: "daily"
- package-ecosystem: "gomod"
directory: "/nats/" # Location of package manifests
labels:
- "🤖 Dependencies"
schedule:
interval: "daily"
50 changes: 50 additions & 0 deletions .github/release-drafter-nats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name-template: 'Nats - v$RESOLVED_VERSION'
tag-template: 'nats/v$RESOLVED_VERSION'
tag-prefix: nats/v
include-paths:
- nats
categories:
- title: '❗ Breaking Changes'
labels:
- '❗ BreakingChange'
- title: '🚀 New'
labels:
- '✏️ Feature'
- title: '🧹 Updates'
labels:
- '🧹 Updates'
- '🤖 Dependencies'
- title: '🐛 Fixes'
labels:
- '☢️ Bug'
- title: '📚 Documentation'
labels:
- '📒 Documentation'
change-template: '- $TITLE (#$NUMBER)'
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
exclude-contributors:
- dependabot
- dependabot[bot]
version-resolver:
major:
labels:
- 'major'
- '❗ BreakingChange'
minor:
labels:
- 'minor'
- '✏️ Feature'
patch:
labels:
- 'patch'
- '📒 Documentation'
- '☢️ Bug'
- '🤖 Dependencies'
- '🧹 Updates'
default: patch
template: |
$CHANGES
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...nats/v$RESOLVED_VERSION
Thank you $CONTRIBUTORS for making this update possible.
7 changes: 7 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ jobs:
options: >-
--health-cmd pg_isready --health-interval 10s --health-timeout 5s
--health-retries 5
steps:
- name: Fetch Repository
uses: actions/checkout@v4
Expand Down Expand Up @@ -132,6 +133,12 @@ jobs:
run: |
redis-server --port 6379 &
- name: Run NATS
run: |
./.github/scripts/gen-test-certs.sh
docker run -d --name nats-jetstream -p 4443:4443 -v ./nats/testdata:/testdata -v ./tls:/tls nats:latest --jetstream -c /testdata/nats-tls.conf
sleep 2
- name: Run Benchmarks
run: |
set -o pipefail
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/gosec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
json: true
escape_json: false
dir_names: true
dir_names_max_depth: '1'
dir_names_exclude_current_dir: true

gosec-scan:
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/release-drafter-nats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release Drafter Nats
on:
push:
# branches to consider in the event; optional, defaults to all
branches:
- master
- main
paths:
- 'nats/**'
jobs:
draft_release_nats:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: release-drafter/release-drafter@v5
with:
config-name: release-drafter-nats.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34 changes: 34 additions & 0 deletions .github/workflows/test-nats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
on:
push:
branches:
- master
- main
paths:
- 'nats/**'
pull_request:
paths:
- 'nats/**'
name: "Tests Nats Driver"
jobs:
Tests:
strategy:
matrix:
go-version:
- 1.20.x
- 1.21.x
runs-on: ubuntu-latest
steps:
- name: Fetch Repository
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '${{ matrix.go-version }}'
- name: Generate config
run: ./.github/scripts/gen-test-certs.sh
- name: Run NATS
run: |
docker run -d --name nats-jetstream -p 4443:4443 -v ./nats/testdata:/testdata -v ./tls:/tls nats:latest --jetstream -c /testdata/nats-tls.conf
sleep 2
- name: Test Nats
run: cd ./nats && go test ./... -v -race
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
vendor/
vendor
/Godeps/

# Go specific
go.work*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ type Storage interface {
- [MongoDB](./mongodb/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+Mongodb%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-mongodb.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [MSSQL](./mssql/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+MSSQL%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-mssql.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [MySQL](./mysql/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+MySQL%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-mysql.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [NATS](./nats/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests%20Nats%20Driver%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-nats.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [Pebble](./pebble/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+Pebble%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-pebble.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [Postgres](./postgres/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+Postgres%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-postgres.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [Redis](./redis/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+Redis%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-redis.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [Rueidis](./rueidis/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+rueidis%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-rueidis.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [S3](./s3/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+S3%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-s3.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [ScyllaDB](./scylladb/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+scylladb%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-scylladb.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
- [SQLite3](./sqlite3/README.md) <a href="https://github.com/gofiber/storage/actions?query=workflow%3A%22Tests+Sqlite3%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-sqlite3.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>

116 changes: 116 additions & 0 deletions nats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
id: nats
title: Nats
---


![Release](https://img.shields.io/github/v/tag/gofiber/storage?filter=nats*)
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord)
![Test](https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-nats.yml?label=Tests)
![Security](https://img.shields.io/github/actions/workflow/status/gofiber/storage/gosec.yml?label=Security)
![Linter](https://img.shields.io/github/actions/workflow/status/gofiber/storage/linter.yml?label=Linter)

A NATS Key/Value storage driver.

**Note: Requires Go 1.20 and above**

### Table of Contents

- [Signatures](#signatures)
- [Installation](#installation)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)

### Signatures

```go
func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() (*nats.Conn, jetstream.KeyValue)
func (s *Storage) Keys() ([]string, error)
```

### Installation

[NATS Key/Value Store](https://docs.nats.io/nats-concepts/jetstream/key-value-store) driver is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:

```bash
go mod init github.com/<user>/<repo>
```

And then install the nats implementation:

```bash
go get github.com/gofiber/storage/nats
```

### Examples

Import the storage package.

```go
import "github.com/gofiber/storage/nats"
```

You can use the following possibilities to create a storage:

```go
// Initialize default config
store := nats.New()

// Initialize custom config
store := nats.New(Config{
URLs: "nats://127.0.0.1:4443",
NatsOptions: []nats.Option{
nats.MaxReconnects(2),
// Enable TLS by specifying RootCAs
nats.RootCAs("./testdata/certs/ca.pem"),
},
KeyValueConfig: jetstream.KeyValueConfig{
Bucket: "test",
Storage: jetstream.MemoryStorage,
},
})
```

### Config

```go
type Config struct {
// Nats URLs, default "nats://127.0.0.1:4222". Can be comma separated list for multiple servers
URLs string
// Nats connection options. See nats_test.go for an example of how to use this.
NatsOptions []nats.Option
// Nats connection name
ClientName string
// Nats context
Context context.Context
// Nats key value config
KeyValueConfig jetstream.KeyValueConfig
// Logger. Using Fiber AllLogger interface for adapting the various log libraries.
Logger log.AllLogger
// Use the Logger for nats events, default: false
Verbose bool
// Wait for connection to be established, default: 100ms
WaitForConnection time.Duration
}
```

### Default Config

```go
var ConfigDefault = Config{
URLs: nats.DefaultURL,
Context: context.Background(),
ClientName: "fiber_storage",
KeyValueConfig: jetstream.KeyValueConfig{
Bucket: "fiber_storage",
},
WaitForConnection: 100 * time.Millisecond,
}
```
78 changes: 78 additions & 0 deletions nats/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package nats

import (
"context"
"time"

"github.com/gofiber/fiber/v2/log"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)

// Config defines the config for storage.
type Config struct {
// Nats URLs, default "nats://127.0.0.1:4222". Can be comma separated list for multiple servers
URLs string
// Nats connection options. See nats_test.go for an example of how to use this.
NatsOptions []nats.Option
// Nats connection name
ClientName string
// Nats context
Context context.Context
// Nats key value config
KeyValueConfig jetstream.KeyValueConfig
// Logger. Using Fiber AllLogger interface for adapting the various log libraries.
Logger log.AllLogger
// Use the Logger for nats events, default: false
Verbose bool
// Wait for connection to be established, default: 100ms
WaitForConnection time.Duration
}

// ConfigDefault is the default config
var ConfigDefault = Config{
URLs: nats.DefaultURL,
Context: context.Background(),
ClientName: "fiber_storage",
KeyValueConfig: jetstream.KeyValueConfig{
Bucket: "fiber_storage",
},
WaitForConnection: 100 * time.Millisecond,
}

// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}

// Override default config
cfg := config[0]

// Set default values
if cfg.URLs == "" {
cfg.URLs = ConfigDefault.URLs
}
if cfg.Context == nil {
cfg.Context = ConfigDefault.Context
}
if len(cfg.KeyValueConfig.Bucket) == 0 {
cfg.KeyValueConfig.Bucket = ConfigDefault.KeyValueConfig.Bucket
}
if cfg.Verbose {
if cfg.Logger == nil {
cfg.Logger = log.DefaultLogger()
}
} else {
cfg.Logger = nil
}
if cfg.ClientName == "" {
cfg.ClientName = ConfigDefault.ClientName
}
if cfg.WaitForConnection == 0 {
cfg.WaitForConnection = ConfigDefault.WaitForConnection
}

return cfg
}
22 changes: 22 additions & 0 deletions nats/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module github.com/gofiber/storage/nats

go 1.20

require (
github.com/gofiber/fiber/v2 v2.52.0
github.com/nats-io/nats.go v1.32.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 71ee192

Please sign in to comment.