-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from grafana/122-connection-options-in-the-op…
…en-function Connection options in the open function
- Loading branch information
Showing
8 changed files
with
197 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package sql | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/grafana/sobek" | ||
) | ||
|
||
// options represents connection related options for Open(). | ||
type options struct { | ||
ConnMaxIdleTime sobek.Value | ||
ConnMaxLifetime sobek.Value | ||
MaxIdleConns sobek.Value | ||
MaxOpenConns sobek.Value | ||
} | ||
|
||
func (o *options) apply(db *sql.DB) error { | ||
if o == nil { | ||
return nil | ||
} | ||
|
||
if o.ConnMaxIdleTime != nil { | ||
d, err := time.ParseDuration(o.ConnMaxIdleTime.String()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
db.SetConnMaxIdleTime(d) | ||
} | ||
|
||
if o.ConnMaxLifetime != nil { | ||
d, err := time.ParseDuration(o.ConnMaxLifetime.String()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
db.SetConnMaxLifetime(d) | ||
} | ||
|
||
if o.MaxIdleConns != nil { | ||
db.SetMaxIdleConns(int(o.MaxIdleConns.ToInteger())) | ||
} | ||
|
||
if o.MaxOpenConns != nil { | ||
db.SetMaxOpenConns(int(o.MaxOpenConns.ToInteger())) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package sql | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/grafana/sobek" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_options_apply(t *testing.T) { | ||
t.Parallel() | ||
|
||
db, err := sql.Open("ramsql", "foo") | ||
|
||
require.NoError(t, err) | ||
|
||
rt := sobek.New() | ||
|
||
require.NoError(t, (&options{}).apply(db)) | ||
require.NoError(t, (&options{ConnMaxIdleTime: rt.ToValue("5s")}).apply(db)) | ||
require.NoError(t, (&options{ConnMaxLifetime: rt.ToValue("10m")}).apply(db)) | ||
|
||
require.NoError(t, (&options{MaxIdleConns: rt.ToValue(5)}).apply(db)) | ||
require.NoError(t, (&options{MaxOpenConns: rt.ToValue(10)}).apply(db)) | ||
|
||
require.Error(t, (&options{ConnMaxIdleTime: rt.ToValue("5g")}).apply(db)) | ||
require.Error(t, (&options{ConnMaxLifetime: rt.ToValue("10e")}).apply(db)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters