Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To many Open Files #14

Closed
xDarkicex opened this issue Feb 16, 2017 · 2 comments
Closed

To many Open Files #14

xDarkicex opened this issue Feb 16, 2017 · 2 comments

Comments

@xDarkicex
Copy link

ParseFile Error Open ./app/views/layouts/application.pug
too many open files, this seem to be an issue once my site begins to see continuous traffic.
It will crash my server.

Parse code

`
package helpers

import (
    "encoding/json"
    "fmt"
    "html/template"
    "log"
    "strings"
    "time"
        
    "gopkg.in/mgo.v2/bson"

    "github.com/Joker/jade"
    "github.com/tdewolff/minify"
    "github.com/tdewolff/minify/html"
     "github.com/xDarkicex/PortfolioGo/config"
)

 // var pugEngine *html.Engine
var t *template.Template

//Render function renders page with our data
func Render(a RouterArgs, view string, object map[string]interface{}) {
   times := make(map[string]interface{})
times["total"] = time.Now()

object["current_user"] = a.User
object["view"] = view

// times["gorilla-save"] = time.Now()
object["flashes"] = a.Session.Flashes()
a.Session.Save(a.Request, a.Response)
// times["gorilla-save"] = time.Since(times["gorilla-save"].(time.Time))

times["jade"] = time.Now()
layout, err := jade.ParseFile("./app/views/layouts/application.pug")
if err != nil {
	Logger.Printf("\nParseFile error: %v", err)
}
m := minify.New()
m.AddFunc("text/html", html.Minify)
layoutMin, err := m.String("text/html", layout)
if err != nil {
	panic(err)
}
currentView, err := jade.ParseFile("./app/views/" + view + ".pug")
if err != nil {
	Logger.Printf("\nParseFile error: %v", err)
}
currentViewMin, err := m.String("text/html", currentView)
if err != nil {
	panic(err)
}
times["jade"] = time.Since(times["jade"].(time.Time))

////////////////////////////////////////////
// FUNCMAPS ARE LIFE!!! THIS IS LIFE NOW
////////////////////////////////////////////

funcMap := make(template.FuncMap)
funcMap["Split"] = func(s string, d string) []string {
	return strings.Split(s, d)
}
funcMap["Join"] = func(a []string, b string) string {
	return strings.Join(a, b)
}
funcMap["ParseFlashes"] = func(fucks []interface{}) []Flash {
	var flashes []Flash
	for _, k := range fucks {
		var flash Flash
		json.Unmarshal([]byte(k.(string)), &flash)
		flashes = append(flashes, flash)
	}
	return flashes
}
funcMap["formatPostTime"] = func(t time.Time) string {
	return t.Format(time.UnixDate)
}

funcMap["formatTitle"] = func(s string) string {
	title := strings.SplitAfter(s, "/")
	return strings.Title(title[1])
}

times["render-page"] = time.Now()
gotpl, err := template.New("layout").Funcs(funcMap).Parse(layoutMin)
if err != nil {
	Logger.Printf("\nTemplate parse error: %v", err)
}
_, err = gotpl.New(view).Parse(currentViewMin)
if err != nil {
	Logger.Printf("\nIndex parse error: %v", err)
}
err = gotpl.Execute(a.Response, object)
if err != nil {
	log.Printf("\nExecute error: %v", err)
}
times["render-page"] = time.Since(times["render-page"].(time.Time))

times["total"] = time.Since(times["total"].(time.Time))
if config.Data.Env != "production" {
	fmt.Println("Render Start ==>")
	defer fmt.Println("Render Complete ==> ", times["total"])
	for k, v := range times {
		fmt.Printf("Time %s: %s\n", k, v)
	}
}
}

// User struct for passing a user everywhere
type User struct {
    ID       bson.ObjectId `bson:"_id,omitempty"`
    Name     string        `bson:"name"`
    Admin    bool          `bson:"admin"`
    Email    string        `bson:"email"`
    Password string        `bson:"password"`
}

`

@toolateforteddy
Copy link

You should try parsing all your templates up front, before you start serving content. Particularly the ones that are constant across all views, like layout.

@Joker
Copy link
Owner

Joker commented Apr 18, 2018

jade.ParseFile() immediately closes parsed files.
The descriptor leakage is because of some other parts of code.

@Joker Joker closed this as completed Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants