Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
id
usermetadb
Commits
899cc402
Commit
899cc402
authored
Oct 04, 2020
by
ale
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Close sql.Stmt objects that are bound to the current transaction
parent
cf09f6a2
Pipeline
#7917
passed with stages
in 1 minute and 40 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
14 deletions
+40
-14
server/analysis.go
server/analysis.go
+3
-1
server/lastlogin.go
server/lastlogin.go
+7
-2
server/mgmt.go
server/mgmt.go
+8
-3
server/userlog.go
server/userlog.go
+20
-8
server/userlog_test.go
server/userlog_test.go
+2
-0
No files found.
server/analysis.go
View file @
899cc402
...
...
@@ -34,7 +34,9 @@ func (d *analysisService) Close() {
func
(
d
*
analysisService
)
CheckDevice
(
ctx
context
.
Context
,
username
string
,
deviceInfo
*
auth
.
DeviceInfo
)
(
bool
,
error
)
{
var
seen
bool
err
:=
withReadonlyTX
(
d
.
db
,
func
(
tx
*
sql
.
Tx
)
error
{
err
:=
d
.
stmts
.
get
(
tx
,
"check_device_info"
)
.
QueryRow
(
username
,
deviceInfo
.
ID
)
.
Scan
(
&
seen
)
stmt
:=
d
.
stmts
.
get
(
tx
,
"check_device_info"
)
defer
stmt
.
Close
()
err
:=
stmt
.
QueryRow
(
username
,
deviceInfo
.
ID
)
.
Scan
(
&
seen
)
if
err
!=
nil
&&
err
!=
sql
.
ErrNoRows
{
return
err
}
...
...
server/lastlogin.go
View file @
899cc402
...
...
@@ -74,7 +74,9 @@ func (l *lastloginDB) AddLastLogin(ctx context.Context, entry *usermetadb.LastLo
return
retryBusy
(
ctx
,
func
()
error
{
return
withTX
(
l
.
db
,
func
(
tx
*
sql
.
Tx
)
error
{
_
,
err
:=
l
.
stmts
.
get
(
tx
,
stmt
)
.
Exec
(
args
...
)
stmt
:=
l
.
stmts
.
get
(
tx
,
stmt
)
defer
stmt
.
Close
()
_
,
err
:=
stmt
.
Exec
(
args
...
)
return
err
})
})
...
...
@@ -98,7 +100,9 @@ func (l *lastloginDB) GetLastLogin(ctx context.Context, username string, service
var
entries
[]
*
usermetadb
.
LastLoginEntry
err
:=
withReadonlyTX
(
l
.
db
,
func
(
tx
*
sql
.
Tx
)
error
{
rows
,
err
:=
l
.
stmts
.
get
(
tx
,
stmt
)
.
Query
(
args
...
)
stmt
:=
l
.
stmts
.
get
(
tx
,
stmt
)
defer
stmt
.
Close
()
rows
,
err
:=
stmt
.
Query
(
args
...
)
if
err
!=
nil
{
return
err
}
...
...
@@ -147,6 +151,7 @@ func (l *lastloginDB) GetUnusedAccounts(ctx context.Context, usernames []string,
if
err
!=
nil
{
return
err
}
defer
rows
.
Close
()
for
rows
.
Next
()
{
var
username
string
if
err
:=
rows
.
Scan
(
&
username
);
err
!=
nil
{
...
...
server/mgmt.go
View file @
899cc402
...
...
@@ -89,7 +89,9 @@ func compactLogs(db *sql.DB, stmts statementMap, username string, count int) err
if
err
!=
nil
{
return
err
}
if
_
,
err
:=
stmts
.
get
(
tx
,
"consolidate_userlog"
)
.
Exec
(
username
,
count
);
err
!=
nil
{
stmt
:=
stmts
.
get
(
tx
,
"consolidate_userlog"
)
defer
stmt
.
Close
()
if
_
,
err
:=
stmt
.
Exec
(
username
,
count
);
err
!=
nil
{
return
err
}
return
tx
.
Commit
()
...
...
@@ -99,12 +101,15 @@ func compactLogs(db *sql.DB, stmts statementMap, username string, count int) err
func
pruneLogs
(
db
*
sql
.
DB
,
stmts
statementMap
,
pruneCutoffDays
int
)
error
{
// Run each batch deletion in its own transaction, just to be nice.
cutoff
:=
time
.
Now
()
.
AddDate
(
0
,
0
,
-
pruneCutoffDays
)
for
_
,
stmt
:=
range
[]
string
{
"prune_userlog"
,
"prune_device_info"
}
{
for
_
,
stmt
Name
:=
range
[]
string
{
"prune_userlog"
,
"prune_device_info"
}
{
tx
,
err
:=
db
.
Begin
()
if
err
!=
nil
{
return
err
}
if
_
,
err
:=
stmts
.
get
(
tx
,
stmt
)
.
Exec
(
cutoff
);
err
!=
nil
{
stmt
:=
stmts
.
get
(
tx
,
stmtName
)
_
,
err
=
stmt
.
Exec
(
cutoff
)
stmt
.
Close
()
if
err
!=
nil
{
tx
.
Rollback
()
// nolint
return
err
}
...
...
server/userlog.go
View file @
899cc402
...
...
@@ -168,9 +168,13 @@ func (u *userlogDB) updateDeviceInfo(tx *sql.Tx, username string, deviceInfo *au
now
:=
roundTimestamp
(
time
.
Now
()
.
UTC
())
var
seen
bool
err
:=
u
.
stmts
.
get
(
tx
,
"check_device_info"
)
.
QueryRow
(
username
,
deviceInfo
.
ID
)
.
Scan
(
&
seen
)
stmt
:=
u
.
stmts
.
get
(
tx
,
"check_device_info"
)
defer
stmt
.
Close
()
err
:=
stmt
.
QueryRow
(
username
,
deviceInfo
.
ID
)
.
Scan
(
&
seen
)
if
err
==
sql
.
ErrNoRows
{
_
,
err
=
u
.
stmts
.
get
(
tx
,
"insert_device_info"
)
.
Exec
(
insStmt
:=
u
.
stmts
.
get
(
tx
,
"insert_device_info"
)
defer
insStmt
.
Close
()
_
,
err
=
insStmt
.
Exec
(
username
,
deviceInfo
.
ID
,
deviceInfo
.
RemoteZone
,
...
...
@@ -188,7 +192,9 @@ func (u *userlogDB) updateDeviceInfo(tx *sql.Tx, username string, deviceInfo *au
// legitimately happen that the other parameters
// change too (like when restoring a backup to another
// platform/os), so we update them too.
_
,
err
=
u
.
stmts
.
get
(
tx
,
"update_device_info"
)
.
Exec
(
insStmt
:=
u
.
stmts
.
get
(
tx
,
"update_device_info"
)
defer
insStmt
.
Close
()
_
,
err
=
insStmt
.
Exec
(
now
,
deviceInfo
.
RemoteZone
,
deviceInfo
.
UserAgent
,
...
...
@@ -212,7 +218,7 @@ func (u *userlogDB) AddLog(ctx context.Context, entry *usermetadb.LogEntry) erro
// If we're given a DeviceInfo entry, update it or create it
// if it does not exist yet.
stmt
:=
"insert_userlog"
stmt
Name
:=
"insert_userlog"
args
:=
[]
interface
{}{
entry
.
Username
,
entry
.
Service
,
...
...
@@ -222,7 +228,7 @@ func (u *userlogDB) AddLog(ctx context.Context, entry *usermetadb.LogEntry) erro
entry
.
Timestamp
,
}
if
entry
.
DeviceInfo
!=
nil
{
stmt
=
"insert_userlog_with_device_info"
stmt
Name
=
"insert_userlog_with_device_info"
args
=
append
(
args
,
entry
.
DeviceInfo
.
ID
)
args
=
append
(
args
,
entry
.
DeviceInfo
.
RemoteZone
)
args
=
append
(
args
,
entry
.
DeviceInfo
.
UserAgent
)
...
...
@@ -238,7 +244,9 @@ func (u *userlogDB) AddLog(ctx context.Context, entry *usermetadb.LogEntry) erro
return
err
}
}
_
,
err
:=
u
.
stmts
.
get
(
tx
,
stmt
)
.
Exec
(
args
...
)
stmt
:=
u
.
stmts
.
get
(
tx
,
stmtName
)
defer
stmt
.
Close
()
_
,
err
:=
stmt
.
Exec
(
args
...
)
return
err
})
});
err
!=
nil
{
...
...
@@ -263,7 +271,9 @@ func (u *userlogDB) GetUserLogs(ctx context.Context, username string, maxDays, l
var
out
[]
*
usermetadb
.
LogEntry
err
:=
withReadonlyTX
(
u
.
db
,
func
(
tx
*
sql
.
Tx
)
error
{
rows
,
err
:=
u
.
stmts
.
get
(
tx
,
"get_user_logs"
)
.
Query
(
username
,
cutoff
,
limit
)
stmt
:=
u
.
stmts
.
get
(
tx
,
"get_user_logs"
)
defer
stmt
.
Close
()
rows
,
err
:=
stmt
.
Query
(
username
,
cutoff
,
limit
)
if
err
!=
nil
{
return
err
}
...
...
@@ -301,7 +311,9 @@ func (u *userlogDB) GetUserLogs(ctx context.Context, username string, maxDays, l
func
(
u
*
userlogDB
)
GetUserDevices
(
ctx
context
.
Context
,
username
string
)
([]
*
usermetadb
.
MetaDeviceInfo
,
error
)
{
var
out
[]
*
usermetadb
.
MetaDeviceInfo
err
:=
withReadonlyTX
(
u
.
db
,
func
(
tx
*
sql
.
Tx
)
error
{
rows
,
err
:=
u
.
stmts
.
get
(
tx
,
"devices_with_counts"
)
.
Query
(
username
)
stmt
:=
u
.
stmts
.
get
(
tx
,
"devices_with_counts"
)
defer
stmt
.
Close
()
rows
,
err
:=
stmt
.
Query
(
username
)
if
err
!=
nil
{
return
err
}
...
...
server/userlog_test.go
View file @
899cc402
...
...
@@ -92,11 +92,13 @@ func bulkLoadTestLogs(t testing.TB, db *sql.DB) *usermetadb.LogEntry {
t
.
Fatalf
(
"insert_userlog: %v"
,
err
)
}
insertLogStmt
:=
tx
.
Stmt
(
stmt
)
defer
insertLogStmt
.
Close
()
stmt
,
err
=
db
.
Prepare
(
userlogDBStatements
[
"insert_device_info"
])
if
err
!=
nil
{
t
.
Fatalf
(
"insert_device_info: %v"
,
err
)
}
insertDeviceInfoStmt
:=
tx
.
Stmt
(
stmt
)
defer
insertDeviceInfoStmt
.
Close
()
userDevices
:=
generateAllRandomDevices
()
entries
:=
generateTestLogs
(
1000
,
userDevices
)
...
...
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