Created
June 4, 2021 19:00
-
-
Save Emyrk/4e099ef978b269c1f234d63c5c7e6f33 to your computer and use it in GitHub Desktop.
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
type delayedDB struct { | |
q queryer | |
delay time.Duration | |
} | |
func NewDelayDB(d time.Duration, q queryer) queryer { | |
return delayedDB{ | |
q: q, | |
delay: d, | |
} | |
} | |
func (t delayedDB) Delay(ctx context.Context) { | |
timer := time.NewTimer(t.delay) | |
defer timer.Stop() | |
select { | |
case <-timer.C: | |
return | |
case <-ctx.Done(): | |
return | |
} | |
} | |
func (t delayedDB) DriverName() string { | |
return t.q.DriverName() | |
} | |
func (t delayedDB) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error) { | |
t.Delay(context.Background()) | |
return t.q.NamedQuery(query, arg) | |
} | |
func (t delayedDB) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) { | |
t.Delay(ctx) | |
return t.q.NamedExecContext(ctx, query, arg) | |
} | |
func (t delayedDB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error { | |
t.Delay(ctx) | |
return t.q.GetContext(ctx, dest, query, args...) | |
} | |
func (t delayedDB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error { | |
t.Delay(ctx) | |
return t.q.SelectContext(ctx, dest, query, args...) | |
} | |
func (t delayedDB) PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error) { | |
t.Delay(ctx) | |
return t.q.PrepareNamedContext(ctx, query) | |
} | |
func (t delayedDB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { | |
t.Delay(ctx) | |
return t.q.ExecContext(ctx, query, args...) | |
} | |
func (t delayedDB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) { | |
t.Delay(ctx) | |
return t.q.QueryxContext(ctx, query, args...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment