Skip to content

Commit d5f0a19

Browse files
committed
Limit max # of DB connections for mysql backend
1 parent 0521e45 commit d5f0a19

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

backend/mysql/mysql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func NewMysqlBackend(host string, port int, user, password, database string, opt
4949
panic(err)
5050
}
5151

52+
if options.MySQLOptions != nil {
53+
options.MySQLOptions(db)
54+
}
55+
5256
b := &mysqlBackend{
5357
dsn: dsn,
5458
db: db,

backend/mysql/options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package mysql
22

33
import (
4+
"database/sql"
5+
46
"github.com/cschleiden/go-workflows/backend"
57
)
68

79
type options struct {
810
backend.Options
911

12+
MySQLOptions func(db *sql.DB)
13+
1014
// ApplyMigrations automatically applies database migrations on startup.
1115
ApplyMigrations bool
1216
}
@@ -20,6 +24,12 @@ func WithApplyMigrations(applyMigrations bool) option {
2024
}
2125
}
2226

27+
func WithMySQLOptions(f func(db *sql.DB)) option {
28+
return func(o *options) {
29+
o.MySQLOptions = f
30+
}
31+
}
32+
2333
// WithBackendOptions allows to pass generic backend options.
2434
func WithBackendOptions(opts ...backend.BackendOption) option {
2535
return func(o *options) {

bench/main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"flag"
77
"fmt"
88
"log"
9-
"log/slog"
109
"os"
1110
"sync"
1211
"time"
@@ -40,7 +39,10 @@ func main() {
4039
defer cancel()
4140

4241
mm := newMemMetrics()
43-
ba := getBackend(*b, backend.WithLogger(slog.New(&nullHandler{})), backend.WithMetrics(mm))
42+
ba := getBackend(*b,
43+
// backend.WithLogger(slog.New(&nullHandler{})),
44+
backend.WithMetrics(mm),
45+
)
4446

4547
wo := worker.DefaultWorkerOptions
4648
wo.WorkflowExecutorCacheSize = *cacheSize
@@ -129,7 +131,12 @@ func getBackend(b string, opt ...backend.BackendOption) backend.Backend {
129131
panic(err)
130132
}
131133

132-
return monoprocess.NewMonoprocessBackend(mysql.NewMysqlBackend("localhost", 3306, "root", "root", "bench", mysql.WithBackendOptions(opt...)))
134+
return monoprocess.NewMonoprocessBackend(
135+
mysql.NewMysqlBackend("localhost", 3306, "root", "root", "bench", mysql.WithBackendOptions(opt...),
136+
mysql.WithMySQLOptions(func(db *sql.DB) {
137+
db.SetMaxOpenConns(100)
138+
})),
139+
)
133140

134141
case "redis":
135142
rclient := redisv8.NewUniversalClient(&redisv8.UniversalOptions{

0 commit comments

Comments
 (0)