-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
139 lines (121 loc) · 3.68 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"database/sql"
"log"
"net/http"
"time"
"github.com/kienmatu/go-connection-pooling/model"
"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
var allTime int64 = 0
var allCount int64 = 0
var newTime int64 = 0
var newCount int64 = 0
var poolTime int64 = 0
var poolCount int64 = 0
var dsn = "postgres://postgres:password1@localhost:5433/postgres?sslmode=disable"
var query = "SELECT id, name, price, description FROM products limit 1000"
func scanProducts(rows *sql.Rows) ([]*model.Product, error) {
defer rows.Close()
products := make([]*model.Product, 0)
for rows.Next() {
var p model.Product
err := rows.Scan(&p.ID, &p.Name, &p.Price, &p.Description)
if err != nil {
return nil, err
}
products = append(products, &p)
}
return products, nil
}
func main() {
// Postgres allows 100 connections in default
// Set the maximum number of idle connections in the pool
idleConn := 4
// Set the maximum number of connections in the pool
maxConnections := 4
// Set the maximum amount of time a connection can be reused
maxConnLifetime := 2 * time.Minute
poolConn, err := sqlx.Open("postgres", dsn)
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
defer poolConn.Close()
poolConn.SetMaxOpenConns(maxConnections)
poolConn.SetMaxIdleConns(idleConn)
poolConn.SetConnMaxLifetime(maxConnLifetime)
// normal connection
conn, err := sqlx.Open("postgres", dsn)
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
// default will be 2 idle connections
// so set it to 1 to simulate
conn.SetMaxIdleConns(1)
// Initialize the HTTP router
router := gin.Default()
router.StaticFile("/", "./index.html")
router.GET("/products/normal", func(c *gin.Context) {
startTime := time.Now()
// Query the database for all products
rows, err := conn.Query(query)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
products, err := scanProducts(rows)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
elapsed := time.Since(startTime).Microseconds()
allCount++
allTime += elapsed
c.JSON(http.StatusOK, model.Response{Elapsed: elapsed, Average: float64(allTime / allCount), Products: products})
})
router.GET("/products/pooled", func(c *gin.Context) {
startTime := time.Now()
// Query the database for all products
rows, err := poolConn.Query(query)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
products, err := scanProducts(rows)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
elapsed := time.Since(startTime).Microseconds()
poolCount++
poolTime += elapsed
c.JSON(http.StatusOK, model.Response{Elapsed: elapsed, Average: float64(poolTime / poolCount), Products: products})
})
router.GET("/products/new", func(c *gin.Context) {
startTime := time.Now()
conn, err := sqlx.Open("postgres", dsn)
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
rows, err := conn.Query(query)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
products, err := scanProducts(rows)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
elapsed := time.Since(startTime).Microseconds()
newCount++
newTime += elapsed
c.JSON(http.StatusOK, model.Response{Elapsed: elapsed, Average: float64(newTime / newCount), Products: products})
})
// Start the HTTP server
if err := router.Run(":8080"); err != nil {
log.Fatalf("Unable to start HTTP server: %v\n", err)
}
}