upd(all): update vendored code
This commit is contained in:
Generated
Vendored
+39
-7
@@ -1,6 +1,39 @@
|
||||
The MIT License (MIT)
|
||||
# DEPRECATION WARNING
|
||||
|
||||
Copyright (c) 2015 labstack
|
||||
`sigctx` is now part of [SentimensRG/ctx](https://github.com/SentimensRG/ctx).
|
||||
|
||||
This repository will no longer receive updates.
|
||||
|
||||
# sigctx
|
||||
Go contexts for graceful shutdown
|
||||
|
||||
## installation
|
||||
|
||||
```bash
|
||||
go get -u github.com/SentimensRG/sigctx
|
||||
```
|
||||
|
||||
## usage
|
||||
|
||||
```go
|
||||
ctx := sigctx.New() // returns a regular context.Context
|
||||
|
||||
// With this simple pattern, your goroutines are guaranteed to terminate correctly
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
go someBlockingFunction(ctx)
|
||||
defer cancel()
|
||||
|
||||
<-ctx.Done() // will unblock on SIGINT and SIGTERM
|
||||
```
|
||||
|
||||
## RFC
|
||||
|
||||
If you find this useful, please let me know: <l.thibault@sentimens.com>
|
||||
|
||||
## License
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2017 Sentimens Research Group, LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -9,14 +42,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package sigctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var ctx context.Context
|
||||
|
||||
func init() {
|
||||
var cancel func()
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
|
||||
go func() {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
select {
|
||||
case <-ch:
|
||||
cancel()
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// New signal-bound context
|
||||
func New() context.Context {
|
||||
return ctx
|
||||
}
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
Random struct {
|
||||
}
|
||||
)
|
||||
|
||||
// Charsets
|
||||
const (
|
||||
Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
Lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||||
Alphabetic = Uppercase + Lowercase
|
||||
Numeric = "0123456789"
|
||||
Alphanumeric = Alphabetic + Numeric
|
||||
Symbols = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?`
|
||||
Hex = Numeric + "abcdef"
|
||||
)
|
||||
|
||||
var (
|
||||
global = New()
|
||||
)
|
||||
|
||||
func New() *Random {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return new(Random)
|
||||
}
|
||||
|
||||
func (r *Random) String(length uint8, charsets ...string) string {
|
||||
charset := strings.Join(charsets, "")
|
||||
if charset == "" {
|
||||
charset = Alphanumeric
|
||||
}
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
b[i] = charset[rand.Int63()%int64(len(charset))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func String(length uint8, charsets ...string) string {
|
||||
return global.String(length, charsets...)
|
||||
}
|
||||
+86
-12
@@ -125,7 +125,15 @@ func (r *DatabaseFactory) Get(dbName ...string) (*DB, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.instances[name] = &DB{handle, context.Background(), nil}
|
||||
r.instances[name] = &DB{
|
||||
handle,
|
||||
context.Background(),
|
||||
nil,
|
||||
&sql.TxOptions{
|
||||
ReadOnly: false,
|
||||
},
|
||||
nil,
|
||||
}
|
||||
return r.instances[name], nil
|
||||
}
|
||||
}
|
||||
@@ -147,24 +155,70 @@ type DB struct {
|
||||
|
||||
ctx context.Context
|
||||
|
||||
Tx *sqlx.Tx
|
||||
TxOpts *sql.TxOptions
|
||||
|
||||
Profiler DatabaseProfiler
|
||||
}
|
||||
|
||||
// Quiet will return a DB handle without a profiler (throw-away)
|
||||
func (r *DB) Quiet() *DB {
|
||||
return &DB{
|
||||
DB: r.DB,
|
||||
r.DB,
|
||||
r.ctx,
|
||||
r.Tx,
|
||||
r.TxOpts,
|
||||
nil,
|
||||
}
|
||||
}
|
||||
|
||||
// With will return a DB handle with a bound context (throw-away)
|
||||
func (r *DB) With(ctx context.Context) *DB {
|
||||
return &DB{
|
||||
DB: r.DB,
|
||||
ctx: ctx,
|
||||
r.DB,
|
||||
ctx,
|
||||
r.Tx,
|
||||
r.TxOpts,
|
||||
r.Profiler,
|
||||
}
|
||||
}
|
||||
|
||||
// Begin will create a transaction in the DB with a context
|
||||
func (r *DB) Begin() error {
|
||||
var err error
|
||||
if r.Tx != nil {
|
||||
return errors.New("Transaction already started")
|
||||
}
|
||||
if r.ctx == nil {
|
||||
r.Tx, err = r.DB.Beginx()
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
r.Tx, err = r.DB.BeginTxx(r.ctx, r.TxOpts)
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
func (r *DB) Commit() error {
|
||||
if r.Tx != nil {
|
||||
if err := r.Tx.Commit(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
r.Tx = nil
|
||||
return nil
|
||||
}
|
||||
return errors.New("No transation active")
|
||||
}
|
||||
|
||||
func (r *DB) Rollback() error {
|
||||
if r.Tx != nil {
|
||||
if err := r.Tx.Rollback(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
r.Tx = nil
|
||||
return nil
|
||||
}
|
||||
return errors.New("No transaction active")
|
||||
}
|
||||
|
||||
// SetFields will provide a string with SQL named bindings from a string slice
|
||||
func (r *DB) SetFields(fields []string) string {
|
||||
idx := 0
|
||||
@@ -180,25 +234,39 @@ func (r *DB) SetFields(fields []string) string {
|
||||
}
|
||||
|
||||
// NamedExec adds profiling on top of the parent DB.NameExec
|
||||
func (r *DB) NamedExec(query string, arg interface{}) (sql.Result, error) {
|
||||
func (r *DB) NamedExec(query string, arg interface{}) (res sql.Result, err error) {
|
||||
exec := func() (sql.Result, error) {
|
||||
if r.Tx != nil {
|
||||
return r.Tx.NamedExecContext(r.ctx, query, arg)
|
||||
}
|
||||
return r.DB.NamedExecContext(r.ctx, query, arg)
|
||||
}
|
||||
|
||||
if r.Profiler != nil {
|
||||
ctx := DatabaseProfilerContext{}.new(query, arg)
|
||||
res, err := r.DB.NamedExecContext(r.ctx, query, arg)
|
||||
res, err = exec()
|
||||
r.Profiler.Post(ctx)
|
||||
return res, err
|
||||
} else {
|
||||
res, err = exec()
|
||||
}
|
||||
return r.DB.NamedExecContext(r.ctx, query, arg)
|
||||
return res, errors.Wrap(err, "exec query failed")
|
||||
}
|
||||
|
||||
// Select is a helper function that will ignore sql.ErrNoRows
|
||||
func (r *DB) Select(dest interface{}, query string, args ...interface{}) error {
|
||||
var err error
|
||||
exec := func() error {
|
||||
if r.Tx != nil {
|
||||
return r.Tx.SelectContext(r.ctx, dest, query, args...)
|
||||
}
|
||||
return r.DB.SelectContext(r.ctx, dest, query, args...)
|
||||
}
|
||||
if r.Profiler != nil {
|
||||
ctx := DatabaseProfilerContext{}.new(query, args...)
|
||||
err = r.DB.SelectContext(r.ctx, dest, query, args...)
|
||||
err = exec()
|
||||
r.Profiler.Post(ctx)
|
||||
} else {
|
||||
err = r.DB.SelectContext(r.ctx, dest, query, args...)
|
||||
err = exec()
|
||||
}
|
||||
// clear no rows returned error
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -210,12 +278,18 @@ func (r *DB) Select(dest interface{}, query string, args ...interface{}) error {
|
||||
// Get is a helper function that will ignore sql.ErrNoRows
|
||||
func (r *DB) Get(dest interface{}, query string, args ...interface{}) error {
|
||||
var err error
|
||||
exec := func() error {
|
||||
if r.Tx != nil {
|
||||
return r.Tx.GetContext(r.ctx, dest, query, args...)
|
||||
}
|
||||
return r.DB.GetContext(r.ctx, dest, query, args...)
|
||||
}
|
||||
if r.Profiler != nil {
|
||||
ctx := DatabaseProfilerContext{}.new(query, args...)
|
||||
err = r.DB.GetContext(r.ctx, dest, query, args...)
|
||||
err = exec()
|
||||
r.Profiler.Post(ctx)
|
||||
} else {
|
||||
err = r.DB.GetContext(r.ctx, dest, query, args...)
|
||||
err = exec()
|
||||
}
|
||||
// clear no rows returned error
|
||||
if err == sql.ErrNoRows {
|
||||
|
||||
Reference in New Issue
Block a user