66 "github.com/jackc/pgx/v5/pgconn"
77 "github.com/jackc/pgx/v5/pgxpool"
88 godatabases "github.com/ralvarezdev/go-databases"
9+ "time"
910)
1011
1112type (
@@ -37,11 +38,17 @@ type (
3738 params ... interface {},
3839 ) pgx.Row
3940 ScanRow (row pgx.Row , destinations ... interface {}) error
41+ SetStatTicker (
42+ duration time.Duration ,
43+ fn func (* pgxpool.Stat ),
44+ )
45+ ClearStatTicker ()
4046 }
4147
4248 // DefaultService is the default service struct
4349 DefaultService struct {
44- pool * pgxpool.Pool
50+ pool * pgxpool.Pool
51+ statTicker * time.Ticker
4552 }
4653)
4754
@@ -56,7 +63,7 @@ func NewDefaultService(pool *pgxpool.Pool) (
5663 }
5764
5865 return & DefaultService {
59- pool ,
66+ pool : pool ,
6067 }, nil
6168}
6269
@@ -193,3 +200,34 @@ func (d *DefaultService) ScanRow(
193200 // Scan the row
194201 return row .Scan (destinations ... )
195202}
203+
204+ // SetStatTicker sets a stat ticker
205+ func (d * DefaultService ) SetStatTicker (
206+ duration time.Duration ,
207+ fn func (* pgxpool.Stat ),
208+ ) {
209+ // Check if the stat ticker is nil
210+ if d .statTicker != nil {
211+ d .statTicker .Stop ()
212+ }
213+
214+ // Set the stat ticker
215+ d .statTicker = time .NewTicker (duration )
216+
217+ // Set the stat ticker
218+ go func () {
219+ for {
220+ select {
221+ case <- d .statTicker .C :
222+ fn (d .pool .Stat ())
223+ }
224+ }
225+ }()
226+ }
227+
228+ // ClearStatTicker clears the stat ticker
229+ func (d * DefaultService ) ClearStatTicker () {
230+ if d .statTicker != nil {
231+ d .statTicker .Stop ()
232+ }
233+ }
0 commit comments