Browse Source

Protect XML generation on hight load

Vova Tkach 5 years ago
parent
commit
9c1949f261
2 changed files with 25 additions and 2 deletions
  1. 10 2
      modules/module_api.go
  2. 15 0
      modules/module_api_xml.go

+ 10 - 2
modules/module_api.go

@@ -31,10 +31,18 @@ func (this *Modules) RegisterModule_Api() *Module {
 
 				target_file := wrap.DHtdocs + string(os.PathSeparator) + "products.xml"
 				if !utils.IsFileExists(target_file) {
-					// XML
-					data := []byte(this.api_GenerateXml(wrap))
+					data := []byte(this.api_GenerateEmptyXml(wrap))
+
+					// Make empty file
+					if file, err := os.Create(target_file); err == nil {
+						file.Write(data)
+					}
+
+					// Make regular XML
+					data = []byte(this.api_GenerateXml(wrap))
 
 					// Save file
+					wrap.RemoveProductXmlCacheFile()
 					if file, err := os.Create(target_file); err == nil {
 						file.Write(data)
 					}

+ 15 - 0
modules/module_api_xml.go

@@ -238,3 +238,18 @@ func (this *Modules) api_GenerateXml(wrap *wrapper.Wrapper) string {
 		`</shop>` +
 		`</yml_catalog>`
 }
+
+func (this *Modules) api_GenerateEmptyXml(wrap *wrapper.Wrapper) string {
+	return `<?xml version="1.0" encoding="UTF-8"?>` +
+		`<!DOCTYPE yml_catalog SYSTEM "shops.dtd">` +
+		`<yml_catalog date="` + time.Unix(int64(time.Now().Unix()), 0).Format("2006-01-02 15:04") + `">` +
+		`<shop>` +
+		`<name>` + html.EscapeString((*wrap.Config).API.XML.Name) + `</name>` +
+		`<company>` + html.EscapeString((*wrap.Config).API.XML.Company) + `</company>` +
+		`<url>` + html.EscapeString((*wrap.Config).API.XML.Url) + `</url>` +
+		`<currencies></currencies>` +
+		`<categories></categories>` +
+		`<offers></offers>` +
+		`</shop>` +
+		`</yml_catalog>`
+}