6
6
"github.com/jackc/pgx/v5/pgconn"
7
7
"github.com/jackc/pgx/v5/pgxpool"
8
8
godatabases "github.com/ralvarezdev/go-databases"
9
+ "time"
9
10
)
10
11
11
12
type (
@@ -37,11 +38,17 @@ type (
37
38
params ... interface {},
38
39
) pgx.Row
39
40
ScanRow (row pgx.Row , destinations ... interface {}) error
41
+ SetStatTicker (
42
+ duration time.Duration ,
43
+ fn func (* pgxpool.Stat ),
44
+ )
45
+ ClearStatTicker ()
40
46
}
41
47
42
48
// DefaultService is the default service struct
43
49
DefaultService struct {
44
- pool * pgxpool.Pool
50
+ pool * pgxpool.Pool
51
+ statTicker * time.Ticker
45
52
}
46
53
)
47
54
@@ -56,7 +63,7 @@ func NewDefaultService(pool *pgxpool.Pool) (
56
63
}
57
64
58
65
return & DefaultService {
59
- pool ,
66
+ pool : pool ,
60
67
}, nil
61
68
}
62
69
@@ -193,3 +200,34 @@ func (d *DefaultService) ScanRow(
193
200
// Scan the row
194
201
return row .Scan (destinations ... )
195
202
}
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