-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_pool.odin
72 lines (62 loc) · 1.85 KB
/
pg_pool.odin
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
package main
import "base:intrinsics"
import "core:log"
import "core:mem"
import "core:os"
import "core:slice"
import "core:strings"
import pq "./shared/odin-postgresql"
CONNINFO: cstring
@(init)
env_db :: proc() {
DATABASE_URL_ENV ::
"DATABASE_URL_DEV" when #config(DB_DEV, DEV) else "DATABASE_URL_PROD"
database_url, ok_database_url := os.lookup_env(
DATABASE_URL_ENV,
context.temp_allocator,
)
log.assertf(ok_database_url, "remember to export %s", DATABASE_URL_ENV)
CONNINFO = strings.clone_to_cstring(database_url)
}
Pg_Pool_Atomic :: struct {
free_list: []bool,
connections: []pq.Conn,
}
FREE, USED :: true, false
pool_init :: proc(p: ^Pg_Pool_Atomic, cap, init_count: int) {
log.assertf(init_count <= cap, "can't have more than N connections")
p.free_list = make([]bool, cap)
p.connections = make([]pq.Conn, cap)
for &is_free in p.free_list do is_free = FREE
for i in 0 ..< init_count do p.connections[i] = pq.connectdb(CONNINFO)
}
pool_get :: proc(p: ^Pg_Pool_Atomic) -> pq.Conn {
i, ok_i := -1, false
for &b, j in p.free_list {
if intrinsics.atomic_load_explicit(&b, .Acquire) == FREE {
i, ok_i = j, true
break
}
}
log.assert(ok_i, "thread count should equal pool count")
intrinsics.atomic_store_explicit(&p.free_list[i], USED, .Release)
// maybe i have exclusize here
conn := p.connections[i]
if pq.status(conn) == .Ok do return conn
if conn == nil {
p.connections[i] = pq.connectdb(CONNINFO)
return p.connections[i]
}
pq.reset(conn)
return conn
}
pool_release :: proc(p: ^Pg_Pool_Atomic, conn: pq.Conn) {
i, ok_i := slice.linear_search(p.connections[:], conn)
log.assert(ok_i, "thread count should equal pool count")
intrinsics.atomic_store_explicit(&p.free_list[i], FREE, .Release)
}
pool_destroy :: proc(p: ^Pg_Pool_Atomic) {
// can we asume this is fine?
p.free_list = {}
for conn in p.connections do pq.finish(conn)
}