From 6fb3709fab5fd356d90525ab341f0325ee634df3 Mon Sep 17 00:00:00 2001 From: shawn Date: Wed, 16 Nov 2022 12:15:24 +0800 Subject: [PATCH] fix: read and write timeout change to be an env --- env/db/pg.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/env/db/pg.go b/env/db/pg.go index 5ee9f21..6378865 100644 --- a/env/db/pg.go +++ b/env/db/pg.go @@ -12,15 +12,12 @@ import ( "go.uber.org/zap" ) -const ( - DBReadTimeout = 5 * time.Minute - DBWriteTimeout = 1 * time.Minute -) - var ( DBReadURL string // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING DBWriteURL string DBMigrationsDir = "./migrations" + DBReadTimeout = 5 * time.Minute + DBWriteTimeout = 1 * time.Minute DBStmtTimeout = 10 * time.Second DBNumConns = 0 // max db conns, 0 = use driver-recommended default readUserName string @@ -129,6 +126,20 @@ func init() { DBStmtTimeout = time.Duration(toInt) * time.Second } } + if envKey, dbrt := env.GetEnv("DB_READ_TIMEOUT"); dbrt != "" { + if toInt, err := strconv.ParseInt(dbrt, 10, 64); err != nil { + log.Warn(constant.IntConversionError, zap.String("env", envKey), zap.Error(err)) + } else { + DBReadTimeout = time.Duration(toInt) * time.Second + } + } + if envKey, dbwt := env.GetEnv("DB_WRITE_TIMEOUT"); dbwt != "" { + if toInt, err := strconv.ParseInt(dbwt, 10, 64); err != nil { + log.Warn(constant.IntConversionError, zap.String("env", envKey), zap.Error(err)) + } else { + DBWriteTimeout = time.Duration(toInt) * time.Second + } + } if envKey, dbnc := env.GetEnv("DB_NUM_CONNS"); dbnc != "" { if toInt, err := strconv.ParseInt(dbnc, 10, 64); err != nil { log.Warn(constant.IntConversionError, zap.String("env", envKey), zap.Error(err))