-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1193 from thedataflows/main
New storage driver: NATS
- Loading branch information
Showing
15 changed files
with
1,014 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,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. |
This file contains 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
This file contains 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
This file contains 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,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 }} |
This file contains 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,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 |
This file contains 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 |
---|---|---|
|
@@ -28,3 +28,6 @@ | |
vendor/ | ||
vendor | ||
/Godeps/ | ||
|
||
# Go specific | ||
go.work* |
This file contains 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
This file contains 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,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, | ||
} | ||
``` |
This file contains 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,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 | ||
} |
This file contains 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,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 | ||
) |
Oops, something went wrong.