You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by el...@apache.org on 2018/08/01 21:19:11 UTC
[trafficcontrol] branch master updated: remove the prepared
transaction from the auth wrapper that was a bottleneck
This is an automated email from the ASF dual-hosted git repository.
elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git
The following commit(s) were added to refs/heads/master by this push:
new bbb83d8 remove the prepared transaction from the auth wrapper that was a bottleneck
bbb83d8 is described below
commit bbb83d8ebed56d19c1790aba1b92dc4493a4857f
Author: Dylan Volz <Dy...@comcast.com>
AuthorDate: Wed Aug 1 15:11:43 2018 -0600
remove the prepared transaction from the auth wrapper that was a bottleneck
---
traffic_ops/traffic_ops_golang/auth/authorize.go | 4 ++--
traffic_ops/traffic_ops_golang/routing.go | 10 +++-------
traffic_ops/traffic_ops_golang/wrappers.go | 17 +++++++++++++++--
3 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/traffic_ops/traffic_ops_golang/auth/authorize.go b/traffic_ops/traffic_ops_golang/auth/authorize.go
index b0b9cb9..89fe2c9 100644
--- a/traffic_ops/traffic_ops_golang/auth/authorize.go
+++ b/traffic_ops/traffic_ops_golang/auth/authorize.go
@@ -63,9 +63,9 @@ type key int
const CurrentUserKey key = iota
// GetCurrentUserFromDB - returns the id and privilege level of the given user along with the username, or -1 as the id, - as the userName and PrivLevelInvalid if the user doesn't exist.
-func GetCurrentUserFromDB(CurrentUserStmt *sqlx.Stmt, user string) CurrentUser {
+func GetCurrentUserFromDB(DB *sqlx.DB, CurrentUserStmt, user string) CurrentUser {
var currentUserInfo CurrentUser
- err := CurrentUserStmt.Get(¤tUserInfo, user)
+ err := DB.Get(¤tUserInfo, CurrentUserStmt, user)
switch {
case err == sql.ErrNoRows:
log.Errorf("checking user %v info: user not in database", user)
diff --git a/traffic_ops/traffic_ops_golang/routing.go b/traffic_ops/traffic_ops_golang/routing.go
index db364e7..62c52fd 100644
--- a/traffic_ops/traffic_ops_golang/routing.go
+++ b/traffic_ops/traffic_ops_golang/routing.go
@@ -21,7 +21,6 @@ package main
import (
"context"
- "fmt"
"net/http"
"regexp"
"sort"
@@ -217,10 +216,7 @@ func RegisterRoutes(d ServerData) error {
return err
}
- userInfoStmt, err := prepareUserInfoStmt(d.DB)
- if err != nil {
- return fmt.Errorf("Error preparing db priv level query: %s", err)
- }
+ userInfoStmt := getUserInfoStmt()
authBase := AuthBase{secret: d.Config.Secrets[0], getCurrentUserInfoStmt: userInfoStmt, override: nil} //we know d.Config.Secrets is a slice of at least one or start up would fail.
routes := CreateRouteMap(routeSlice, rawRoutes, authBase, d.RequestTimeout)
@@ -232,8 +228,8 @@ func RegisterRoutes(d ServerData) error {
return nil
}
-func prepareUserInfoStmt(db *sqlx.DB) (*sqlx.Stmt, error) {
- return db.Preparex("SELECT r.priv_level, u.id, u.username, COALESCE(u.tenant_id, -1) AS tenant_id, ARRAY(SELECT rc.cap_name FROM role_capability AS rc WHERE rc.role_id=r.id) AS capabilities FROM tm_user AS u JOIN role AS r ON u.role = r.id WHERE u.username = $1")
+func getUserInfoStmt() string {
+ return "SELECT r.priv_level, u.id, u.username, COALESCE(u.tenant_id, -1) AS tenant_id, ARRAY(SELECT rc.cap_name FROM role_capability AS rc WHERE rc.role_id=r.id) AS capabilities FROM tm_user AS u JOIN role AS r ON u.role = r.id WHERE u.username = $1"
}
func use(h http.HandlerFunc, middlewares []Middleware) http.HandlerFunc {
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go b/traffic_ops/traffic_ops_golang/wrappers.go
index 6bc57f8..bcac30d 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -36,6 +36,7 @@ import (
"github.com/apache/trafficcontrol/lib/go-log"
tc "github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/about"
+ "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tocookie"
"github.com/jmoiron/sqlx"
@@ -47,7 +48,7 @@ var ServerName = "traffic_ops_golang" + "/" + about.About.Version
// AuthBase ...
type AuthBase struct {
secret string
- getCurrentUserInfoStmt *sqlx.Stmt
+ getCurrentUserInfoStmt string
override Middleware
}
@@ -98,8 +99,20 @@ func (a AuthBase) GetWrapper(privLevelRequired int) Middleware {
handleErr(http.StatusUnauthorized, errors.New("Unauthorized, please log in."))
return
}
+ var DB *sqlx.DB
+ val := r.Context().Value(api.DBContextKey)
+ if val != nil {
+ switch v := val.(type) {
+ case *sqlx.DB:
+ DB = v
+ default:
+ handleErr(http.StatusInternalServerError, errors.New("No DB found"))
+ }
+ } else {
+ handleErr(http.StatusInternalServerError, errors.New("No DB found"))
+ }
- currentUserInfo := auth.GetCurrentUserFromDB(a.getCurrentUserInfoStmt, username)
+ currentUserInfo := auth.GetCurrentUserFromDB(DB, a.getCurrentUserInfoStmt, username)
if currentUserInfo.PrivLevel < privLevelRequired {
handleErr(http.StatusForbidden, errors.New("Forbidden."))
return