-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69cc709
commit cd5ffbe
Showing
13 changed files
with
351 additions
and
216 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
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 was deleted.
Oops, something went wrong.
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,24 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
func ContextWithCancelOnSignal(ctx context.Context) context.Context { | ||
ctx, cancel := context.WithCancel(ctx) | ||
stop := make(chan os.Signal, 1) | ||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
go func() { | ||
defer cancel() | ||
select { | ||
case <-stop: | ||
case <-ctx.Done(): | ||
} | ||
}() | ||
|
||
return ctx | ||
} |
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,73 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/wearemojo/mojo-public-go/lib/db/mongodb" | ||
"github.com/wearemojo/mojo-public-go/lib/merr" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"go.mongodb.org/mongo-driver/mongo/readconcern" | ||
"go.mongodb.org/mongo-driver/mongo/writeconcern" | ||
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring" | ||
) | ||
|
||
// MongoDB configures a connection to a Mongo database. | ||
type MongoDB struct { | ||
URI string `json:"uri"` | ||
ConnectTimeout time.Duration `json:"connect_timeout"` | ||
MaxConnIdleTime *time.Duration `json:"max_conn_idle_time"` | ||
MaxConnecting *uint64 `json:"max_connecting"` | ||
MaxPoolSize *uint64 `json:"max_pool_size"` | ||
MinPoolSize *uint64 `json:"min_pool_size"` | ||
} | ||
|
||
// Options returns the MongoDB client options and database name. | ||
func (m MongoDB) Options(ctx context.Context) (opts *options.ClientOptions, dbName string, err error) { | ||
opts = options.Client().ApplyURI(m.URI) | ||
opts.MaxConnIdleTime = m.MaxConnIdleTime | ||
opts.MaxConnecting = m.MaxConnecting | ||
opts.MaxPoolSize = m.MaxPoolSize | ||
opts.MinPoolSize = m.MinPoolSize | ||
|
||
err = opts.Validate() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// all Go services use majority reads/writes, and this is unlikely to change | ||
// if it does change, switch to accepting as an argument | ||
opts.SetReadConcern(readconcern.Majority()) | ||
opts.SetWriteConcern(writeconcern.Majority()) | ||
|
||
cs, err := connstring.Parse(m.URI) | ||
if err != nil { | ||
return | ||
} | ||
|
||
dbName = cs.Database | ||
if dbName == "" { | ||
err = merr.New(ctx, "mongo_db_name_missing", nil) | ||
} | ||
|
||
return | ||
} | ||
|
||
// Connect returns a connected mongo.Database instance. | ||
func (m MongoDB) Connect(ctx context.Context) (*mongodb.Database, error) { | ||
opts, dbName, err := m.Options(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if m.ConnectTimeout == 0 { | ||
m.ConnectTimeout = 10 * time.Second | ||
} | ||
|
||
// this package can only be used for service config | ||
// so can only happen at init-time - no need to accept context input | ||
ctx, cancel := context.WithTimeout(ctx, m.ConnectTimeout) | ||
defer cancel() | ||
|
||
return mongodb.Connect(ctx, opts, dbName) | ||
} |
Oops, something went wrong.