-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (47 loc) · 1012 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"database/sql"
"fmt"
"os"
"sync"
"time"
_ "github.com/jackc/pgx/v4/stdlib"
)
func main() {
db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
db.SetConnMaxIdleTime(1 * time.Hour)
defer db.Close()
ctx := context.Background()
GetCourses(ctx, db)
fmt.Println("initial calls ended")
// initial queries end.
for {
<-time.After(50 * time.Minute)
GetCourses(ctx, db)
}
}
func GetCourses(ctx context.Context, db *sql.DB) {
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(num int) {
defer wg.Done()
getCourses(ctx, num, db)
}(i)
}
wg.Wait()
}
func getCourses(ctx context.Context, num int, db *sql.DB) {
var numRows int
err := db.QueryRowContext(ctx, "select count(*) from courses").Scan(&numRows)
if err != nil {
fmt.Printf("%d: err: %v\n", num, err)
return
}
fmt.Printf("%d: number of rows: %d\n", num, numRows)
}