multiple response.WriteHeader calls 解决办法

爱看 开发采用了 Negroni + Render的组合,上次发现老出现  multiple response.WriteHeader calls 的错误,于是看 log 发现致命异常,是数据层直接报的异常,于是写了《Go 开发的网站出现 multiple response.WriteHeader calls》,并修正数据层的异常,但是效果还是不明显,偶尔还会出现此类错误,于是到 Github Render 哪里咨询了一下,有位好心人,叫我把Render 的配置 IsDevelopment 设置为 false,现在终于没有此类错误了,后期还有观察了。


错误栈如下:

2015/03/04 20:33:00 http: multiple response.WriteHeader calls
[negroni] PANIC: template: redefinition of template "layout"
goroutine 73776 [running]:
github.com/codegangsta/negroni.func·003()
/var/GOPATH/src/github.com/codegangsta/negroni/recovery.go:34 +0x10d
html/template.Must(0x0, 0x7f9c8a8c1c00, 0xc208299440, 0x0)
/var/go/src/html/template/template.go:304 +0x50
github.com/unrolled/render.func·003(0xc2082f79c0, 0x15, 0x7f9c8a8c7ad0, 0xc2086f3bd0, 0x0, 0x0, 0x0, 0x0)
/var/GOPATH/src/github.com/unrolled/render/render.go:167 +0x539
path/filepath.walk(0xc2082f79c0, 0x15, 0x7f9c8a8c7ad0, 0xc2086f3bd0, 0xc208859428, 0x0, 0x0)
/var/go/src/path/filepath/path.go:347 +0x91
path/filepath.walk(0x98e970, 0x9, 0x7f9c8a8c7ad0, 0xc2080f2000, 0xc208859428, 0x0, 0x0)
/var/go/src/path/filepath/path.go:372 +0x51d
path/filepath.Walk(0x98e970, 0x9, 0xc208859428, 0x0, 0x0)
/var/go/src/path/filepath/path.go:394 +0xf2
github.com/unrolled/render.(Render).compileTemplates(0xc20809f860)
/var/GOPATH/src/github.com/unrolled/render/render.go:173 +0x106
github.com/unrolled/render.(
Render).HTML(0xc20809f860, 0x7f9c8a8d50d8, 0xc2087b1e80, 0xc8, 0x988150, 0x8, 0x7f0660, 0xc20848ddd0, 0xc208859640, 0x1, ...)
/var/GOPATH/src/github.com/unrolled/render/render.go:250 +0x3f



Render 的部分配置如下:

r = render.New(render.Options{
    Directory:  "templates",                                  
    Layout:     "layout",                                      
    Extensions: []string{".html", ".tmpl"},                     
    Funcs:      []template.FuncMap{AppHelpers},               
    Delims:     render.Delims{"{{", "}}"},                
    Charset:    "UTF-8",                                        
    IndentJSON: false,                                          
    IndentXML:  false,                                           
    PrefixJSON: []byte(""),                                      
    PrefixXML:  []byte("<?xml version='1.0' encoding='UTF-8'?>"), 
    IsDevelopment: false,
})

@tammomueller 的回复 帮了很大的忙:From my experience this may happen when you're running it in development mode with concurrent access. Try to set development mode to false

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。