mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
feat: add keeltime
This commit is contained in:
parent
a1a8c3f47a
commit
1e7dc782c6
@ -4,6 +4,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt"
|
"github.com/golang-jwt/jwt"
|
||||||
|
|
||||||
|
keeltime "github.com/foomo/keel/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxTimeDifferenceBetweenNodes represents an offset that should be taken
|
// MaxTimeDifferenceBetweenNodes represents an offset that should be taken
|
||||||
@ -11,7 +13,7 @@ import (
|
|||||||
var MaxTimeDifferenceBetweenNodes = time.Second * 30
|
var MaxTimeDifferenceBetweenNodes = time.Second * 30
|
||||||
|
|
||||||
func NewStandardClaims() jwt.StandardClaims {
|
func NewStandardClaims() jwt.StandardClaims {
|
||||||
now := time.Now().Add(-MaxTimeDifferenceBetweenNodes)
|
now := keeltime.Now().Add(-MaxTimeDifferenceBetweenNodes)
|
||||||
return jwt.StandardClaims{
|
return jwt.StandardClaims{
|
||||||
IssuedAt: now.Unix(),
|
IssuedAt: now.Unix(),
|
||||||
NotBefore: now.Unix(),
|
NotBefore: now.Unix(),
|
||||||
|
|||||||
@ -2,6 +2,8 @@ package cookie
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
keeltime "github.com/foomo/keel/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TimeProvider func() time.Time
|
type TimeProvider func() time.Time
|
||||||
@ -31,6 +33,6 @@ func NewTimeProvider(opts ...TimeProviderOption) TimeProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return func() time.Time {
|
return func() time.Time {
|
||||||
return time.Now().Add(options.Offset)
|
return keeltime.Now().Add(options.Offset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/foomo/keel/log"
|
"github.com/foomo/keel/log"
|
||||||
|
keeltime "github.com/foomo/keel/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -45,7 +46,7 @@ func LoggerWithMessage(v string) LoggerOption {
|
|||||||
func LoggerWithOptions(opts LoggerOptions) Middleware {
|
func LoggerWithOptions(opts LoggerOptions) Middleware {
|
||||||
return func(l *zap.Logger, name string, next http.Handler) http.Handler {
|
return func(l *zap.Logger, name string, next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := keeltime.Now()
|
||||||
|
|
||||||
// wrap response write to get access to status & size
|
// wrap response write to get access to status & size
|
||||||
wr := WrapResponseWriter(w)
|
wr := WrapResponseWriter(w)
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/foomo/keel/log"
|
"github.com/foomo/keel/log"
|
||||||
|
keeltime "github.com/foomo/keel/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -62,7 +63,7 @@ func ResponseTime(opts ...ResponseTimeOption) Middleware {
|
|||||||
func ResponseTimeWithOptions(opts ResponseTimeOptions) Middleware {
|
func ResponseTimeWithOptions(opts ResponseTimeOptions) Middleware {
|
||||||
return func(l *zap.Logger, name string, next http.Handler) http.Handler {
|
return func(l *zap.Logger, name string, next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := keeltime.Now()
|
||||||
rw := WrapResponseWriter(w)
|
rw := WrapResponseWriter(w)
|
||||||
rw.SetWriteResponseTimeHeader(opts.SetHeader)
|
rw.SetWriteResponseTimeHeader(opts.SetHeader)
|
||||||
next.ServeHTTP(rw, r)
|
next.ServeHTTP(rw, r)
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
http2 "github.com/foomo/keel/net/http"
|
http2 "github.com/foomo/keel/net/http"
|
||||||
|
keeltime "github.com/foomo/keel/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// responseWriter is a wrapper that includes that http statusCode and size for logging
|
// responseWriter is a wrapper that includes that http statusCode and size for logging
|
||||||
@ -25,7 +26,7 @@ func WrapResponseWriter(w http.ResponseWriter) *responseWriter {
|
|||||||
}
|
}
|
||||||
return &responseWriter{
|
return &responseWriter{
|
||||||
ResponseWriter: w,
|
ResponseWriter: w,
|
||||||
start: time.Now(),
|
start: keeltime.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
keelerrors "github.com/foomo/keel/errors"
|
keelerrors "github.com/foomo/keel/errors"
|
||||||
keelpersistence "github.com/foomo/keel/persistence"
|
keelpersistence "github.com/foomo/keel/persistence"
|
||||||
|
keeltime "github.com/foomo/keel/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -155,7 +156,7 @@ func (c *Collection) Upsert(ctx context.Context, id string, entity Entity) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := entity.(EntityWithTimestamps); ok {
|
if v, ok := entity.(EntityWithTimestamps); ok {
|
||||||
now := time.Now()
|
now := keeltime.Now()
|
||||||
if ct := v.GetCreatedAt(); ct.IsZero() {
|
if ct := v.GetCreatedAt(); ct.IsZero() {
|
||||||
v.SetCreatedAt(now)
|
v.SetCreatedAt(now)
|
||||||
}
|
}
|
||||||
@ -169,9 +170,7 @@ func (c *Collection) Upsert(ctx context.Context, id string, entity Entity) error
|
|||||||
|
|
||||||
if currentVersion == 0 {
|
if currentVersion == 0 {
|
||||||
// insert the new document
|
// insert the new document
|
||||||
if _, err := c.collection.InsertOne(ctx, entity); err != nil {
|
return c.Insert(ctx, entity)
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else if res := c.collection.FindOneAndUpdate(
|
} else if res := c.collection.FindOneAndUpdate(
|
||||||
ctx,
|
ctx,
|
||||||
bson.D{{Key: "id", Value: id}, {Key: "version", Value: currentVersion}},
|
bson.D{{Key: "id", Value: id}, {Key: "version", Value: currentVersion}},
|
||||||
@ -194,7 +193,13 @@ func (c *Collection) Upsert(ctx context.Context, id string, entity Entity) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete ...
|
func (c *Collection) Insert(ctx context.Context, entity Entity) error {
|
||||||
|
if _, err := c.collection.InsertOne(ctx, entity); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Collection) Delete(ctx context.Context, id string) error {
|
func (c *Collection) Delete(ctx context.Context, id string) error {
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return keelpersistence.ErrNotFound
|
return keelpersistence.ErrNotFound
|
||||||
|
|||||||
36
time/time.go
Normal file
36
time/time.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package keeltime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Now = time.Now
|
||||||
|
NowStaticNSec = int64(1609498800e9) // 2021-01-01 12:00:00
|
||||||
|
NowIncrementalNSec = NowStaticNSec
|
||||||
|
)
|
||||||
|
|
||||||
|
// Static sets now to a static time provider
|
||||||
|
func Static() {
|
||||||
|
Now = static
|
||||||
|
}
|
||||||
|
|
||||||
|
// Incremental sets now to a incremental time provider
|
||||||
|
func Incremental() {
|
||||||
|
Now = incremental
|
||||||
|
}
|
||||||
|
|
||||||
|
func static() time.Time {
|
||||||
|
return time.Unix(0, NowStaticNSec)
|
||||||
|
}
|
||||||
|
|
||||||
|
func incremental() time.Time {
|
||||||
|
t := time.Unix(0, NowIncrementalNSec)
|
||||||
|
NowIncrementalNSec++
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetIncremental sets the incremental time to the static default
|
||||||
|
func ResetIncremental() {
|
||||||
|
NowIncrementalNSec = NowStaticNSec
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user