improved some of the tasks to use fewer goroutines
This commit is contained in:
@@ -12,6 +12,8 @@ package database
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/gommon/log"
|
||||
)
|
||||
|
||||
// AuditRecord holds an audit record instance.
|
||||
@@ -46,6 +48,9 @@ const (
|
||||
AuditAdminLockUnlockAccount = 113
|
||||
)
|
||||
|
||||
// auditWriteQueue is a channel to store audit records in the background.
|
||||
var auditWriteQueue chan *AuditRecord
|
||||
|
||||
/* AmNewAudit creates a new audit record.
|
||||
* Parameters:
|
||||
* rectype - Audit record type.
|
||||
@@ -94,3 +99,32 @@ func (ar *AuditRecord) Store() error {
|
||||
ar.OnDate = moment
|
||||
return nil
|
||||
}
|
||||
|
||||
// auditWriter is the routine that stores audit records in trhe background.
|
||||
func auditWriter(workChan chan *AuditRecord, doneChan chan bool) {
|
||||
for ar := range workChan {
|
||||
err := ar.Store()
|
||||
if err != nil {
|
||||
log.Errorf("dropped audit record on the floor: %v", err)
|
||||
}
|
||||
}
|
||||
doneChan <- true
|
||||
}
|
||||
|
||||
// AmStoreAudit stores the audit record in the background.
|
||||
func AmStoreAudit(rec *AuditRecord) {
|
||||
if rec != nil {
|
||||
auditWriteQueue <- rec
|
||||
}
|
||||
}
|
||||
|
||||
// setupAuditWriter sets up the background audit writer.
|
||||
func setupAuditWriter() func() {
|
||||
auditWriteQueue = make(chan *AuditRecord, 16)
|
||||
doneChan := make(chan bool)
|
||||
go auditWriter(auditWriteQueue, doneChan)
|
||||
return func() {
|
||||
close(auditWriteQueue)
|
||||
<-doneChan
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -20,12 +20,16 @@ var amdb *sqlx.DB
|
||||
|
||||
// SetupDb sets up the database and associated items.
|
||||
func SetupDb() (func(), error) {
|
||||
var fn1 func() = nil
|
||||
db, err := sqlx.Open(config.GlobalConfig.Database.Driver, config.GlobalConfig.Database.Dsn)
|
||||
if err == nil {
|
||||
amdb = db
|
||||
// TODO: additional initialization
|
||||
fn1 = setupAuditWriter()
|
||||
}
|
||||
return func() {
|
||||
if fn1 != nil {
|
||||
fn1()
|
||||
}
|
||||
amdb.Close()
|
||||
}, err
|
||||
}
|
||||
|
||||
+2
-6
@@ -226,9 +226,7 @@ func AmAuthenticateUser(name string, password string, remoteIP string) (*User, e
|
||||
log.Debugf("AmAuthenicate() authenticating user %s...", name)
|
||||
var ar *AuditRecord = nil
|
||||
defer func() {
|
||||
if ar != nil {
|
||||
go ar.Store()
|
||||
}
|
||||
AmStoreAudit(ar)
|
||||
}()
|
||||
|
||||
user, err := AmGetUserByName(name)
|
||||
@@ -294,9 +292,7 @@ func crackAuthString(authString string) (int32, string, error) {
|
||||
func AmAuthenticateUserByToken(authString string, remoteIP string) (*User, error) {
|
||||
var ar *AuditRecord = nil
|
||||
defer func() {
|
||||
if ar != nil {
|
||||
go ar.Store()
|
||||
}
|
||||
AmStoreAudit(ar)
|
||||
}()
|
||||
|
||||
uid, token, err := crackAuthString(authString)
|
||||
|
||||
Reference in New Issue
Block a user