feat: add mongo options (#2753)
* feat: add mongo options * feat: add mongo options * feat: add mongo options * feat: add mongo options * feat: add mongo options * feat: add mongo options
This commit is contained in:
@@ -3,15 +3,12 @@ package mon
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/syncx"
|
"github.com/zeromicro/go-zero/core/syncx"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
mopt "go.mongodb.org/mongo-driver/mongo/options"
|
mopt "go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultTimeout = time.Second
|
|
||||||
|
|
||||||
var clientManager = syncx.NewResourceManager()
|
var clientManager = syncx.NewResourceManager()
|
||||||
|
|
||||||
// ClosableClient wraps *mongo.Client and provides a Close method.
|
// ClosableClient wraps *mongo.Client and provides a Close method.
|
||||||
@@ -30,9 +27,20 @@ func Inject(key string, client *mongo.Client) {
|
|||||||
clientManager.Inject(key, &ClosableClient{client})
|
clientManager.Inject(key, &ClosableClient{client})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClient(url string) (*mongo.Client, error) {
|
func getClient(url string, opts ...Option) (*mongo.Client, error) {
|
||||||
val, err := clientManager.GetResource(url, func() (io.Closer, error) {
|
val, err := clientManager.GetResource(url, func() (io.Closer, error) {
|
||||||
cli, err := mongo.Connect(context.Background(), mopt.Client().ApplyURI(url))
|
o := mopt.Client().ApplyURI(url)
|
||||||
|
opts = append([]Option{defaultTimeoutOption()}, opts...)
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(o)
|
||||||
|
}
|
||||||
|
|
||||||
|
cli, err := mongo.Connect(context.Background(), o)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = cli.Ping(context.Background(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ func MustNewModel(uri, db, collection string, opts ...Option) *Model {
|
|||||||
|
|
||||||
// NewModel returns a Model.
|
// NewModel returns a Model.
|
||||||
func NewModel(uri, db, collection string, opts ...Option) (*Model, error) {
|
func NewModel(uri, db, collection string, opts ...Option) (*Model, error) {
|
||||||
cli, err := getClient(uri)
|
cli, err := getClient(uri, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,15 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/syncx"
|
"github.com/zeromicro/go-zero/core/syncx"
|
||||||
|
mopt "go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultTimeout = time.Second * 3
|
||||||
|
|
||||||
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
|
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
options struct {
|
options = mopt.ClientOptions
|
||||||
timeout time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// Option defines the method to customize a mongo model.
|
// Option defines the method to customize a mongo model.
|
||||||
Option func(opts *options)
|
Option func(opts *options)
|
||||||
@@ -22,8 +23,15 @@ func SetSlowThreshold(threshold time.Duration) {
|
|||||||
slowThreshold.Set(threshold)
|
slowThreshold.Set(threshold)
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultOptions() *options {
|
func defaultTimeoutOption() Option {
|
||||||
return &options{
|
return func(opts *options) {
|
||||||
timeout: defaultTimeout,
|
opts.SetTimeout(defaultTimeout)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithTimeout set the mon client operation timeout.
|
||||||
|
func WithTimeout(timeout time.Duration) Option {
|
||||||
|
return func(opts *options) {
|
||||||
|
opts.SetTimeout(timeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
mopt "go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSetSlowThreshold(t *testing.T) {
|
func TestSetSlowThreshold(t *testing.T) {
|
||||||
@@ -13,6 +14,14 @@ func TestSetSlowThreshold(t *testing.T) {
|
|||||||
assert.Equal(t, time.Second, slowThreshold.Load())
|
assert.Equal(t, time.Second, slowThreshold.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDefaultOptions(t *testing.T) {
|
func Test_defaultTimeoutOption(t *testing.T) {
|
||||||
assert.Equal(t, defaultTimeout, defaultOptions().timeout)
|
opts := mopt.Client()
|
||||||
|
defaultTimeoutOption()(opts)
|
||||||
|
assert.Equal(t, defaultTimeout, *opts.Timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWithTimeout(t *testing.T) {
|
||||||
|
opts := mopt.Client()
|
||||||
|
WithTimeout(time.Second)(opts)
|
||||||
|
assert.Equal(t, time.Second, *opts.Timeout)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user