Skip to content

Commit 04fb905

Browse files
committed
nats: Remove dependency to gofiber/fiber and logger
1 parent 8b1d41d commit 04fb905

File tree

5 files changed

+42
-103
lines changed

5 files changed

+42
-103
lines changed

nats/README.md

+31-35
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ title: Nats
1212

1313
A NATS Key/Value storage driver.
1414

15-
**Note: Requires Go 1.20 and above**
15+
## Note: Requires Go 1.20 and above
1616

1717
### Table of Contents
1818

@@ -57,60 +57,56 @@ Import the storage package.
5757
import "github.com/gofiber/storage/nats"
5858
```
5959

60-
You can use the following possibilities to create a storage:
60+
You can use the following options to create a storage driver:
6161

6262
```go
6363
// Initialize default config
6464
store := nats.New()
6565

6666
// Initialize custom config
6767
store := nats.New(Config{
68-
URLs: "nats://127.0.0.1:4443",
69-
NatsOptions: []nats.Option{
70-
nats.MaxReconnects(2),
71-
// Enable TLS by specifying RootCAs
72-
nats.RootCAs("./testdata/certs/ca.pem"),
73-
},
74-
KeyValueConfig: jetstream.KeyValueConfig{
75-
Bucket: "test",
76-
Storage: jetstream.MemoryStorage,
77-
},
68+
URLs: "nats://127.0.0.1:4443",
69+
NatsOptions: []nats.Option{
70+
nats.MaxReconnects(2),
71+
// Enable TLS by specifying RootCAs
72+
nats.RootCAs("./testdata/certs/ca.pem"),
73+
},
74+
KeyValueConfig: jetstream.KeyValueConfig{
75+
Bucket: "test",
76+
Storage: jetstream.MemoryStorage,
77+
},
7878
})
7979
```
8080

8181
### Config
8282

8383
```go
8484
type Config struct {
85-
// Nats URLs, default "nats://127.0.0.1:4222". Can be comma separated list for multiple servers
86-
URLs string
87-
// Nats connection options. See nats_test.go for an example of how to use this.
88-
NatsOptions []nats.Option
89-
// Nats connection name
90-
ClientName string
91-
// Nats context
92-
Context context.Context
93-
// Nats key value config
94-
KeyValueConfig jetstream.KeyValueConfig
95-
// Logger. Using Fiber AllLogger interface for adapting the various log libraries.
96-
Logger log.AllLogger
97-
// Use the Logger for nats events, default: false
98-
Verbose bool
99-
// Wait for connection to be established, default: 100ms
100-
WaitForConnection time.Duration
85+
// Nats URLs, default "nats://127.0.0.1:4222". Can be comma separated list for multiple servers
86+
URLs string
87+
// Nats connection options. See nats_test.go for an example of how to use this.
88+
NatsOptions []nats.Option
89+
// Nats connection name
90+
ClientName string
91+
// Nats context
92+
Context context.Context
93+
// Nats key value config
94+
KeyValueConfig jetstream.KeyValueConfig
95+
// Wait for connection to be established, default: 100ms
96+
WaitForConnection time.Duration
10197
}
10298
```
10399

104100
### Default Config
105101

106102
```go
107103
var ConfigDefault = Config{
108-
URLs: nats.DefaultURL,
109-
Context: context.Background(),
110-
ClientName: "fiber_storage",
111-
KeyValueConfig: jetstream.KeyValueConfig{
112-
Bucket: "fiber_storage",
113-
},
114-
WaitForConnection: 100 * time.Millisecond,
104+
URLs: nats.DefaultURL,
105+
Context: context.Background(),
106+
ClientName: "fiber_storage",
107+
KeyValueConfig: jetstream.KeyValueConfig{
108+
Bucket: "fiber_storage",
109+
},
110+
WaitForConnection: 100 * time.Millisecond,
115111
}
116112
```

nats/config.go

+6-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"time"
66

7-
"github.com/gofiber/fiber/v2/log"
87
"github.com/nats-io/nats.go"
98
"github.com/nats-io/nats.go/jetstream"
109
)
@@ -21,11 +20,7 @@ type Config struct {
2120
Context context.Context
2221
// Nats key value config
2322
KeyValueConfig jetstream.KeyValueConfig
24-
// Logger. Using Fiber AllLogger interface for adapting the various log libraries.
25-
Logger log.AllLogger
26-
// Use the Logger for nats events, default: false
27-
Verbose bool
28-
// Wait for connection to be established, default: 100ms
23+
// Wait for connection to be established, default: 250ms
2924
WaitForConnection time.Duration
3025
}
3126

@@ -37,7 +32,7 @@ var ConfigDefault = Config{
3732
KeyValueConfig: jetstream.KeyValueConfig{
3833
Bucket: "fiber_storage",
3934
},
40-
WaitForConnection: 100 * time.Millisecond,
35+
WaitForConnection: 250 * time.Millisecond,
4136
}
4237

4338
// Helper function to set default values
@@ -54,22 +49,19 @@ func configDefault(config ...Config) Config {
5449
if cfg.URLs == "" {
5550
cfg.URLs = ConfigDefault.URLs
5651
}
52+
5753
if cfg.Context == nil {
5854
cfg.Context = ConfigDefault.Context
5955
}
56+
6057
if len(cfg.KeyValueConfig.Bucket) == 0 {
6158
cfg.KeyValueConfig.Bucket = ConfigDefault.KeyValueConfig.Bucket
6259
}
63-
if cfg.Verbose {
64-
if cfg.Logger == nil {
65-
cfg.Logger = log.DefaultLogger()
66-
}
67-
} else {
68-
cfg.Logger = nil
69-
}
60+
7061
if cfg.ClientName == "" {
7162
cfg.ClientName = ConfigDefault.ClientName
7263
}
64+
7365
if cfg.WaitForConnection == 0 {
7466
cfg.WaitForConnection = ConfigDefault.WaitForConnection
7567
}

nats/go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/gofiber/storage/nats
33
go 1.20
44

55
require (
6-
github.com/gofiber/fiber/v2 v2.52.5
76
github.com/nats-io/nats.go v1.36.0
87
github.com/stretchr/testify v1.9.0
98
)
@@ -14,7 +13,6 @@ require (
1413
github.com/nats-io/nkeys v0.4.7 // indirect
1514
github.com/nats-io/nuid v1.0.1 // indirect
1615
github.com/pmezard/go-difflib v1.0.0 // indirect
17-
github.com/valyala/bytebufferpool v1.0.0 // indirect
1816
golang.org/x/crypto v0.18.0 // indirect
1917
golang.org/x/sys v0.16.0 // indirect
2018
golang.org/x/text v0.14.0 // indirect

nats/go.sum

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
4-
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
5-
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
63
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
74
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
85
github.com/nats-io/nats.go v1.36.0 h1:suEUPuWzTSse/XhESwqLxXGuj8vGRuPRoG7MoRN/qyU=
@@ -15,8 +12,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
1512
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1613
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1714
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
18-
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
19-
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
2015
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
2116
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
2217
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=

nats/nats.go

+5-47
Original file line numberDiff line numberDiff line change
@@ -34,58 +34,27 @@ func init() {
3434
gob.Register(entry{})
3535
}
3636

37-
// logErrorw is a helper function to log error messages
38-
func (s *Storage) logErrorw(msg string, keysAndValues ...interface{}) {
39-
if s.cfg.Verbose {
40-
s.cfg.Logger.Errorw(msg, keysAndValues...)
41-
}
42-
}
43-
44-
// logInfow is a helper function to log error messages
45-
func (s *Storage) logInfow(msg string, keysAndValues ...interface{}) {
46-
if s.cfg.Verbose {
47-
s.cfg.Logger.Infow(msg, keysAndValues...)
48-
}
49-
}
50-
5137
// connectHandler is a helper function to set the initial connect handler
5238
func (s *Storage) connectHandler(nc *nats.Conn) {
53-
s.logInfow("connected",
54-
"diver", "nats",
55-
"url", nc.ConnectedUrlRedacted(),
56-
)
57-
58-
var err error
5939
s.mu.Lock()
6040
defer s.mu.Unlock()
41+
42+
var err error
6143
s.kv, err = newNatsKV(
6244
nc,
6345
s.ctx,
6446
s.cfg.KeyValueConfig,
6547
)
6648
if err != nil {
67-
s.logErrorw("kv not initialized",
68-
"diver", "nats",
69-
"error", err.Error(),
70-
)
7149
s.err = errors.Join(s.err, err)
7250
}
7351
}
7452

7553
// disconnectErrHandler is a helper function to set the disconnect error handler
7654
func (s *Storage) disconnectErrHandler(nc *nats.Conn, err error) {
77-
if err != nil {
78-
s.logErrorw("disconnected",
79-
"diver", "nats",
80-
"error", err.Error(),
81-
)
82-
} else {
83-
s.logInfow("disconnected",
84-
"diver", "nats",
85-
)
86-
}
8755
s.mu.Lock()
8856
defer s.mu.Unlock()
57+
8958
nc.Opts.RetryOnFailedConnect = true
9059
if err != nil {
9160
s.err = errors.Join(s.err, err)
@@ -99,30 +68,20 @@ func (s *Storage) reconnectHandler(nc *nats.Conn) {
9968

10069
// errorHandler is a helper function to set the error handler
10170
func (s *Storage) errorHandler(nc *nats.Conn, sub *nats.Subscription, err error) {
102-
s.logErrorw("error handler",
103-
"diver", "nats",
104-
"sub", sub.Subject,
105-
"error", err.Error(),
106-
)
10771
s.mu.Lock()
10872
defer s.mu.Unlock()
73+
10974
if err != nil {
11075
s.err = errors.Join(s.err, fmt.Errorf("subject %q: %w", sub.Subject, err))
11176
}
11277
}
11378

114-
// closedHandler is a helper function to set the closed handler
115-
func (s *Storage) closedHandler(nc *nats.Conn) {
116-
s.logInfow("closed",
117-
"diver", "nats",
118-
)
119-
}
120-
12179
func newNatsKV(nc *nats.Conn, ctx context.Context, keyValueConfig jetstream.KeyValueConfig) (jetstream.KeyValue, error) {
12280
js, err := jetstream.New(nc)
12381
if err != nil {
12482
return nil, fmt.Errorf("get jetstream: %w", err)
12583
}
84+
12685
jskv, err := js.KeyValue(ctx, keyValueConfig.Bucket)
12786
if err != nil {
12887
if errors.Is(err, jetstream.ErrBucketNotFound) {
@@ -170,7 +129,6 @@ func New(config ...Config) *Storage {
170129
nats.DisconnectErrHandler(storage.disconnectErrHandler),
171130
nats.ReconnectHandler(storage.reconnectHandler),
172131
nats.ErrorHandler(storage.errorHandler),
173-
nats.ClosedHandler(storage.closedHandler),
174132
},
175133
cfg.NatsOptions...,
176134
)

0 commit comments

Comments
 (0)