Browse Source

Fix MySQL connection for actions, context cancelation

Vova Tkach 5 years ago
parent
commit
d7d30332af
3 changed files with 55 additions and 1 deletions
  1. 49 0
      engine/engine.go
  2. 6 0
      engine/modules/modules.go
  3. 0 1
      engine/mysqlpool/mysqlpool.go

+ 49 - 0
engine/engine.go

@@ -32,6 +32,11 @@ func Response(mp *mysqlpool.MySqlPool, sb *basket.Basket, l *logger.Logger, m *m
 }
 
 func (this *Engine) Process() bool {
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	this.Wrap.IsBackend = this.Wrap.R.URL.Path == "/cp" || strings.HasPrefix(this.Wrap.R.URL.Path, "/cp/")
 	this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
 	this.Wrap.UrlArgs = append(this.Wrap.UrlArgs, utils.UrlToArray(this.Wrap.R.URL.Path)...)
@@ -44,6 +49,11 @@ func (this *Engine) Process() bool {
 		return true
 	}
 
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	// Redirect to CP for creating MySQL config file
 	if !this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
 		this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
@@ -62,6 +72,11 @@ func (this *Engine) Process() bool {
 		return true
 	}
 
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	// Check for MySQL connection
 	err := this.Wrap.UseDatabase()
 	if err != nil {
@@ -69,6 +84,11 @@ func (this *Engine) Process() bool {
 		return true
 	}
 
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	// Separated logic
 	if !this.Wrap.IsBackend {
 		// Maintenance mode
@@ -95,6 +115,11 @@ func (this *Engine) Process() bool {
 		return this.Mods.XXXFrontEnd(this.Wrap)
 	}
 
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	// Show login page if need
 	if this.Wrap.S.GetInt("UserId", 0) <= 0 {
 		// Redirect
@@ -106,12 +131,22 @@ func (this *Engine) Process() bool {
 		return true
 	}
 
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	// Try load current user data
 	if !this.Wrap.LoadSessionUser() {
 		http.Redirect(this.Wrap.W, this.Wrap.R, "/", 302)
 		return true
 	}
 
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	// Only active admins can use backend
 	if !(this.Wrap.User.A_admin == 1 && this.Wrap.User.A_active == 1) {
 		// Redirect
@@ -128,6 +163,11 @@ func (this *Engine) Process() bool {
 		return true
 	}
 
+	// Request was canceled
+	if this.contextDone() {
+		return false
+	}
+
 	// Render backend
 	return this.Mods.XXXBackEnd(this.Wrap)
 }
@@ -139,3 +179,12 @@ func (this *Engine) redirectFixCpUrl() bool {
 	}
 	return false
 }
+
+func (this *Engine) contextDone() bool {
+	select {
+	case <-this.Wrap.R.Context().Done():
+		return true
+	default:
+	}
+	return false
+}

+ 6 - 0
engine/modules/modules.go

@@ -294,6 +294,12 @@ func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
 			}
 			if name != "" {
 				if act, ok := this.acts[name]; ok {
+					// Check for MySQL connection
+					if err := wrap.UseDatabase(); err != nil {
+						this.XXXActionHeaders(wrap, http.StatusNotFound)
+						wrap.MsgError(err.Error())
+						return true
+					}
 					if act.Info.WantUser || act.Info.WantAdmin {
 						if !wrap.LoadSessionUser() {
 							this.XXXActionHeaders(wrap, http.StatusNotFound)

+ 0 - 1
engine/mysqlpool/mysqlpool.go

@@ -20,7 +20,6 @@ func New() *MySqlPool {
 }
 
 func (this *MySqlPool) Get(key string) *sqlw.DB {
-	// TODO: return nil if context canceled!
 	this.Lock()
 	defer this.Unlock()
 	if value, ok := this.connections[key]; ok == true {