Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Stefane Fermigier
neo
Commits
da01e01e
Commit
da01e01e
authored
Feb 19, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
X sqlite: Switch to using github.com/gwenn/gosqlite directly
parent
c0502d41
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
275 additions
and
47 deletions
+275
-47
go/neo/storage/sqlite/pool.go
go/neo/storage/sqlite/pool.go
+102
-0
go/neo/storage/sqlite/sqlite.go
go/neo/storage/sqlite/sqlite.go
+173
-47
No files found.
go/neo/storage/sqlite/pool.go
0 → 100644
View file @
da01e01e
// Copyright (C) 2018 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
package
sqlite
// simple connection pool
import
(
"errors"
"sync"
"lab.nexedi.com/kirr/go123/xerr"
sqlite3
"github.com/gwenn/gosqlite"
)
// connPool is a pool of sqlite3.Conn
type
connPool
struct
{
factory
func
()
(
*
sqlite3
.
Conn
,
error
)
// =nil if pool closed
mu
sync
.
Mutex
connv
[]
*
sqlite3
.
Conn
// operated as stack
}
// newConnPool creates new connPool that will be using factory to create new
// connections.
func
newConnPool
(
factory
func
()
(
*
sqlite3
.
Conn
,
error
))
*
connPool
{
return
&
connPool
{
factory
:
factory
}
}
// Close closes pool and all connections that were in it.
func
(
p
*
connPool
)
Close
()
error
{
p
.
mu
.
Lock
()
connv
:=
p
.
connv
p
.
connv
=
nil
p
.
factory
=
nil
p
.
mu
.
Unlock
()
var
errv
xerr
.
Errorv
for
_
,
conn
:=
range
connv
{
err
:=
conn
.
Close
()
errv
.
Appendif
(
err
)
}
return
errv
.
Err
()
}
var
errClosedPool
=
errors
.
New
(
"sqlite: pool: getConn on closed pool"
)
// getConn returns a connection - either from pool, or newly created if the
// pool was empty.
func
(
p
*
connPool
)
getConn
()
(
conn
*
sqlite3
.
Conn
,
_
error
)
{
p
.
mu
.
Lock
()
factory
:=
p
.
factory
if
factory
==
nil
{
p
.
mu
.
Unlock
()
return
nil
,
errClosedPool
}
if
l
:=
len
(
p
.
connv
);
l
>
0
{
l
--
conn
=
p
.
connv
[
l
]
p
.
connv
[
l
]
=
nil
// just in case
p
.
connv
=
p
.
connv
[
:
l
]
}
p
.
mu
.
Unlock
()
if
conn
!=
nil
{
return
conn
,
nil
}
// pool was empty - we need to create new connection
return
factory
()
}
// putConn puts a connection to pool.
//
// Caller must not directly use conn after call to putConn anymore.
func
(
p
*
connPool
)
putConn
(
conn
*
sqlite3
.
Conn
)
{
p
.
mu
.
Lock
()
if
p
.
factory
!=
nil
{
// forgiving putConn after close
p
.
connv
=
append
(
p
.
connv
,
conn
)
}
p
.
mu
.
Unlock
()
}
go/neo/storage/sqlite/sqlite.go
View file @
da01e01e
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment