Skip to content

Commit b0465ed

Browse files
Driver Configuration Parameters (spacecloud-io#1374)
Co-authored-by: Noorain Panjwani <[email protected]>
1 parent c4e61b8 commit b0465ed

File tree

12 files changed

+93
-40
lines changed

12 files changed

+93
-40
lines changed

.github/workflows/go.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jobs:
55
name: Build and test gateway
66
runs-on: ubuntu-latest
77
steps:
8-
- name: Set up Go 1.13
8+
- name: Set up Go 1.15
99
uses: actions/setup-go@v1
1010
with:
11-
go-version: 1.13
11+
go-version: 1.15
1212
id: go
1313

1414
- name: Check out code into the Go module directory
@@ -36,10 +36,10 @@ jobs:
3636
name: Build and test Runner
3737
runs-on: ubuntu-latest
3838
steps:
39-
- name: Set up Go 1.13
39+
- name: Set up Go 1.15
4040
uses: actions/setup-go@v1
4141
with:
42-
go-version: 1.13
42+
go-version: 1.15
4343
id: go
4444

4545
- name: Check out code into the Go module directory
@@ -67,10 +67,10 @@ jobs:
6767
name: Build and test space cli
6868
runs-on: ubuntu-latest
6969
steps:
70-
- name: Set up Go 1.13
70+
- name: Set up Go 1.15
7171
uses: actions/setup-go@v1
7272
with:
73-
go-version: 1.13
73+
go-version: 1.15
7474
id: go
7575
- name: Check out code into the Go module directory
7676
uses: actions/checkout@v1

gateway/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.14.9-alpine3.12
1+
FROM golang:1.15.3-alpine3.12
22
WORKDIR /build
33

44
# Take the current space cloud version as a argument

gateway/config/config.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,26 @@ type ProjectConfig struct {
8888
ContextTimeGraphQL int `json:"contextTimeGraphQL,omitempty" yaml:"contextTimeGraphQL,omitempty"` // contextTime sets the timeout of query
8989
}
9090

91+
// DriverConfig stores the parameters for drivers of Databases.
92+
type DriverConfig struct {
93+
MaxConn int `json:"maxConn,omitempty" yaml:"maxConn,omitempty"` //for SQL and Mongo
94+
MaxIdleTimeout int `json:"maxIdleTimeout,omitempty" yaml:"maxIdleTimeout,omitempty"` //for SQL and Mongo
95+
MinConn uint64 `json:"minConn,omitempty" yaml:"minConn,omitempty"` //only for Mongo
96+
MaxIdleConn int `json:"maxIdleConn,omitempty" yaml:"maxIdleConn,omitempty"` //only for SQL
97+
}
98+
9199
// DatabaseConfig stores information of database config
92100
type DatabaseConfig struct {
93-
DbAlias string `json:"dbAlias,omitempty" yaml:"dbAlias"`
94-
Type string `json:"type,omitempty" yaml:"type"` // database type
95-
DBName string `json:"name,omitempty" yaml:"name"` // name of the logical database or schema name according to the database type
96-
Conn string `json:"conn,omitempty" yaml:"conn"`
97-
IsPrimary bool `json:"isPrimary" yaml:"isPrimary"`
98-
Enabled bool `json:"enabled" yaml:"enabled"`
99-
BatchTime int `json:"batchTime,omitempty" yaml:"batchTime"` // time in milli seconds
100-
BatchRecords int `json:"batchRecords,omitempty" yaml:"batchRecords"` // indicates number of records per batch
101-
Limit int64 `json:"limit,omitempty" yaml:"limit"` // indicates number of records to send per request
101+
DbAlias string `json:"dbAlias,omitempty" yaml:"dbAlias"`
102+
Type string `json:"type,omitempty" yaml:"type"` // database type
103+
DBName string `json:"name,omitempty" yaml:"name"` // name of the logical database or schema name according to the database type
104+
Conn string `json:"conn,omitempty" yaml:"conn"`
105+
IsPrimary bool `json:"isPrimary" yaml:"isPrimary"`
106+
Enabled bool `json:"enabled" yaml:"enabled"`
107+
BatchTime int `json:"batchTime,omitempty" yaml:"batchTime"` // time in milli seconds
108+
BatchRecords int `json:"batchRecords,omitempty" yaml:"batchRecords"` // indicates number of records per batch
109+
Limit int64 `json:"limit,omitempty" yaml:"limit"` // indicates number of records to send per request
110+
DriverConf DriverConfig `json:"driverConf,omitempty" yaml:"driverConf"`
102111
}
103112

104113
// DatabaseSchema stores information of db schemas

gateway/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ require (
5959

6060
)
6161

62-
go 1.14
62+
go 1.15

gateway/modules/crud/bolt/bolt.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/spaceuptech/helpers"
77
"go.etcd.io/bbolt"
88

9+
"github.com/spaceuptech/space-cloud/gateway/config"
910
"github.com/spaceuptech/space-cloud/gateway/model"
1011
"github.com/spaceuptech/space-cloud/gateway/utils"
1112
)
@@ -40,8 +41,8 @@ func (b *Bolt) Close() error {
4041
}
4142

4243
// IsSame checks if we've got the same connection string
43-
func (b *Bolt) IsSame(conn, dbName string) bool {
44-
return b.connection == conn && dbName == b.bucketName
44+
func (b *Bolt) IsSame(conn, dbName string, driverConf config.DriverConfig) bool {
45+
return b.connection == conn && dbName == b.bucketName //DriverConfig is not used for now.
4546
}
4647

4748
// IsClientSafe checks whether database is enabled and connected

gateway/modules/crud/crud.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type Crud interface {
6363
RawBatch(ctx context.Context, batchedQueries []string) error
6464
GetDBType() model.DBType
6565
IsClientSafe(ctx context.Context) error
66-
IsSame(conn, dbName string) bool
66+
IsSame(conn, dbName string, driverConf config.DriverConfig) bool
6767
Close() error
6868
GetConnectionState(ctx context.Context) bool
6969
SetQueryFetchLimit(limit int64)
@@ -74,14 +74,14 @@ func Init() *Module {
7474
return &Module{batchMapTableToChan: make(batchMap), dataLoader: loader{loaderMap: map[string]*dataloader.Loader{}}}
7575
}
7676

77-
func (m *Module) initBlock(dbType model.DBType, enabled bool, connection, dbName string) (Crud, error) {
77+
func (m *Module) initBlock(dbType model.DBType, enabled bool, connection, dbName string, driverConf config.DriverConfig) (Crud, error) {
7878
switch dbType {
7979
case model.Mongo:
80-
return mgo.Init(enabled, connection, dbName)
80+
return mgo.Init(enabled, connection, dbName, driverConf)
8181
case model.EmbeddedDB:
8282
return bolt.Init(enabled, connection, dbName)
8383
case model.MySQL, model.Postgres, model.SQLServer:
84-
c, err := sql.Init(dbType, enabled, connection, dbName)
84+
c, err := sql.Init(dbType, enabled, connection, dbName, driverConf)
8585
if err == nil && enabled {
8686
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
8787
defer cancel()
@@ -90,7 +90,7 @@ func (m *Module) initBlock(dbType model.DBType, enabled bool, connection, dbName
9090
}
9191
}
9292
if dbType == model.MySQL {
93-
return sql.Init(dbType, enabled, fmt.Sprintf("%s%s", connection, dbName), dbName)
93+
return sql.Init(dbType, enabled, fmt.Sprintf("%s%s", connection, dbName), dbName, driverConf)
9494
}
9595
return c, err
9696
default:

gateway/modules/crud/mgo/mongo.go

+29-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go.mongodb.org/mongo-driver/mongo"
1111
"go.mongodb.org/mongo-driver/mongo/options"
1212

13+
"github.com/spaceuptech/space-cloud/gateway/config"
1314
"github.com/spaceuptech/space-cloud/gateway/model"
1415
"github.com/spaceuptech/space-cloud/gateway/utils"
1516
)
@@ -21,11 +22,12 @@ type Mongo struct {
2122
connection string
2223
dbName string
2324
client *mongo.Client
25+
driverConf config.DriverConfig
2426
}
2527

2628
// Init initialises a new mongo instance
27-
func Init(enabled bool, connection, dbName string) (mongoStub *Mongo, err error) {
28-
mongoStub = &Mongo{dbName: dbName, enabled: enabled, connection: connection, client: nil}
29+
func Init(enabled bool, connection, dbName string, driverConf config.DriverConfig) (mongoStub *Mongo, err error) {
30+
mongoStub = &Mongo{dbName: dbName, enabled: enabled, connection: connection, client: nil, driverConf: driverConf}
2931

3032
if mongoStub.enabled {
3133
err = mongoStub.connect()
@@ -44,8 +46,8 @@ func (m *Mongo) Close() error {
4446
}
4547

4648
// IsSame checks if we've got the same connection string
47-
func (m *Mongo) IsSame(conn, dbName string) bool {
48-
return strings.HasPrefix(m.connection, conn) && dbName == m.dbName
49+
func (m *Mongo) IsSame(conn, dbName string, driverConf config.DriverConfig) bool {
50+
return strings.HasPrefix(m.connection, conn) && dbName == m.dbName && driverConf.MaxConn == m.driverConf.MaxConn && driverConf.MaxIdleTimeout == m.driverConf.MaxIdleTimeout && driverConf.MinConn == m.driverConf.MinConn
4951
}
5052

5153
// IsClientSafe checks whether database is enabled and connected
@@ -70,7 +72,29 @@ func (m *Mongo) connect() error {
7072
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
7173
defer cancel()
7274

73-
client, err := mongo.NewClient(options.Client().ApplyURI(m.connection))
75+
opts := options.Client().ApplyURI(m.connection)
76+
77+
maxConn := m.driverConf.MaxConn
78+
if maxConn == 0 {
79+
maxConn = 100
80+
}
81+
82+
maxIdleTimeout := m.driverConf.MaxIdleTimeout
83+
if maxIdleTimeout == 0 {
84+
maxIdleTimeout = 60 * 5 * 1000
85+
}
86+
87+
minConn := m.driverConf.MinConn
88+
if minConn == 0 {
89+
minConn = 10
90+
}
91+
92+
opts = opts.SetMaxPoolSize((uint64)(maxConn))
93+
duration := time.Duration(maxIdleTimeout) * time.Millisecond
94+
opts = opts.SetMaxConnIdleTime(duration)
95+
opts = opts.SetMinPoolSize(minConn)
96+
client, err := mongo.NewClient(opts)
97+
7498
if err != nil {
7599
return err
76100
}

gateway/modules/crud/setters.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (m *Module) SetConfig(project string, crud config.DatabaseConfigs) error {
5656
if m.block != nil {
5757
m.block.SetQueryFetchLimit(v.Limit)
5858
// Skip if the connection string is the same
59-
if m.block.IsSame(connectionString, v.DBName) {
59+
if m.block.IsSame(connectionString, v.DBName, v.DriverConf) {
6060
continue
6161
}
6262
// Close the previous database connection
@@ -69,7 +69,7 @@ func (m *Module) SetConfig(project string, crud config.DatabaseConfigs) error {
6969
var err error
7070

7171
v.Type = strings.TrimPrefix(v.Type, "sql-")
72-
c, err = m.initBlock(model.DBType(v.Type), v.Enabled, connectionString, v.DBName)
72+
c, err = m.initBlock(model.DBType(v.Type), v.Enabled, connectionString, v.DBName, v.DriverConf)
7373

7474
if v.Enabled {
7575
if err != nil {

gateway/modules/crud/sql/sql.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
_ "github.com/go-sql-driver/mysql" // Import for MySQL
1515
_ "github.com/lib/pq" // Import for postgres
1616

17+
"github.com/spaceuptech/space-cloud/gateway/config"
1718
"github.com/spaceuptech/space-cloud/gateway/model"
1819
"github.com/spaceuptech/space-cloud/gateway/utils"
1920
)
@@ -26,11 +27,12 @@ type SQL struct {
2627
client *sqlx.DB
2728
dbType string
2829
name string // logical db name or schema name according to the database type
30+
driverConf config.DriverConfig
2931
}
3032

3133
// Init initialises a new sql instance
32-
func Init(dbType model.DBType, enabled bool, connection string, dbName string) (s *SQL, err error) {
33-
s = &SQL{enabled: enabled, connection: connection, name: dbName, client: nil}
34+
func Init(dbType model.DBType, enabled bool, connection string, dbName string, driverConf config.DriverConfig) (s *SQL, err error) {
35+
s = &SQL{enabled: enabled, connection: connection, name: dbName, client: nil, driverConf: driverConf}
3436

3537
switch dbType {
3638
case model.Postgres:
@@ -55,8 +57,8 @@ func Init(dbType model.DBType, enabled bool, connection string, dbName string) (
5557
}
5658

5759
// IsSame checks if we've got the same connection string
58-
func (s *SQL) IsSame(conn, dbName string) bool {
59-
return strings.HasPrefix(s.connection, conn) && dbName == s.name
60+
func (s *SQL) IsSame(conn, dbName string, driverConf config.DriverConfig) bool {
61+
return strings.HasPrefix(s.connection, conn) && dbName == s.name && driverConf.MaxConn == s.driverConf.MaxConn && driverConf.MaxIdleTimeout == s.driverConf.MaxIdleTimeout && driverConf.MaxIdleConn == s.driverConf.MaxIdleConn
6062
}
6163

6264
// Close gracefully the SQL client
@@ -115,8 +117,25 @@ func (s *SQL) connect() error {
115117

116118
s.client = sql
117119

118-
s.client.SetMaxOpenConns(10)
119-
s.client.SetMaxIdleConns(0)
120+
maxConn := s.driverConf.MaxConn
121+
if maxConn == 0 {
122+
maxConn = 100
123+
}
124+
125+
maxIdleConn := s.driverConf.MaxIdleConn
126+
if maxIdleConn == 0 {
127+
maxIdleConn = 50
128+
}
129+
130+
maxIdleTimeout := s.driverConf.MaxIdleTimeout
131+
if maxIdleTimeout == 0 {
132+
maxIdleTimeout = 60 * 5 * 1000
133+
}
134+
135+
s.client.SetMaxOpenConns(maxConn)
136+
s.client.SetMaxIdleConns(maxIdleConn)
137+
duration := time.Duration(maxIdleTimeout) * time.Millisecond
138+
s.client.SetConnMaxIdleTime(duration)
120139
return sql.PingContext(ctx)
121140
}
122141

runner/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.14.9-alpine3.12
1+
FROM golang:1.15.3-alpine3.12
22
WORKDIR /build
33
COPY . .
44
#RUN apk --no-cache add build-base

runner/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/spaceuptech/space-cloud/runner
22

3-
go 1.14
3+
go 1.15
44

55
require (
66
github.com/AlecAivazis/survey/v2 v2.0.7

space-cli/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/spaceuptech/space-cloud/space-cli
22

3-
go 1.13
3+
go 1.15
44

55
require (
66
github.com/AlecAivazis/survey/v2 v2.0.7

0 commit comments

Comments
 (0)