Skip to content

Commit b197b1c

Browse files
authored
Fix cache test (Fiber v1.12) (TechEmpower#5816)
* Update Fiber v1.10.0 * Fiber: Bump v1.12.0 * Update constant * Fiber optimization * v1.12.1 * Update dependencies * Update cached worlds test * Update cached test * Update cached test * Update cache test * Fix cache test * Update cache test * Update cache test * Fix panic Co-authored-by: Fenny <Fenny>
1 parent cdd67ce commit b197b1c

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

frameworks/Go/fiber/src/server.go

+32-21
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
)
1919

2020
var (
21-
child bool
22-
db *pgxpool.Pool
21+
child bool
22+
db *pgxpool.Pool
23+
cachedWorlds Worlds
2324
)
2425

2526
const (
@@ -28,7 +29,7 @@ const (
2829
helloworld = "Hello, World!"
2930
worldselectsql = "SELECT id, randomNumber FROM World WHERE id = $1"
3031
worldupdatesql = "UPDATE World SET randomNumber = $1 WHERE id = $2"
31-
worldcachesql = "SELECT * FROM World"
32+
worldcachesql = "SELECT * FROM World LIMIT $1"
3233
fortuneselectsql = "SELECT id, message FROM Fortune"
3334
)
3435

@@ -112,7 +113,7 @@ func ReleaseWorld(w *World) {
112113
// WorldsPool ...
113114
var WorldsPool = sync.Pool{
114115
New: func() interface{} {
115-
return make(Worlds, 0, 512)
116+
return make(Worlds, 0, 500)
116117
},
117118
}
118119

@@ -144,6 +145,27 @@ func initDatabase() {
144145
if err != nil {
145146
panic(err)
146147
}
148+
populateCache()
149+
}
150+
151+
// this will populate the cached worlds for the cache test
152+
func populateCache() {
153+
worlds := make(Worlds, worldcount)
154+
rows, err := db.Query(context.Background(), worldcachesql, worldcount)
155+
if err != nil {
156+
panic(err)
157+
}
158+
for i := 0; i < worldcount; i++ {
159+
w := &worlds[i]
160+
if !rows.Next() {
161+
break
162+
}
163+
if err := rows.Scan(&w.ID, &w.RandomNumber); err != nil {
164+
panic(err)
165+
}
166+
//db.QueryRow(context.Background(), worldselectsql, RandomWorld()).Scan(&w.ID, &w.RandomNumber)
167+
}
168+
cachedWorlds = worlds
147169
}
148170

149171
// jsonHandler :
@@ -226,26 +248,15 @@ func plaintextHandler(c *fiber.Ctx) {
226248
c.SendString(helloworld)
227249
}
228250

229-
var cachePopulated = false
230-
var catchedWorlds []World
231-
232-
func populateCache() {
233-
worlds := AcquireWorlds()[:500]
234-
for i := 0; i < 500; i++ {
235-
w := &worlds[i]
236-
db.QueryRow(context.Background(), worldselectsql, RandomWorld()).Scan(&w.ID, &w.RandomNumber)
237-
}
238-
catchedWorlds = worlds
239-
cachePopulated = true
240-
}
241-
242251
// cachedHandler :
243252
func cachedHandler(c *fiber.Ctx) {
244-
if !cachePopulated {
245-
populateCache()
246-
}
247253
n := QueriesCount(c)
248-
c.JSON(catchedWorlds[:n])
254+
worlds := AcquireWorlds()[:n]
255+
for i := 0; i < n; i++ {
256+
worlds[i] = cachedWorlds[RandomWorld()-1]
257+
}
258+
c.JSON(worlds)
259+
ReleaseWorlds(worlds)
249260
}
250261

251262
// RandomWorld :

0 commit comments

Comments
 (0)