发生恐慌 – 未收到数据 – ERR_EMPTY_RESPONSE

huangapple go评论125阅读模式
英文:

go panic - No data received - ERR_EMPTY_RESPONSE

问题

我正在使用gorethink驱动程序,在模型中编写了一个查询函数,像这样:

func GetQuotesByUser(idUser string) []Quote{
    ids:=GetQuoteIdsByUser(idUser)
    if (len(ids)>0){
        result,err:=r.Table("quote").GetAll(ids...).Run(config.Connection())
        defer result.Close()
        
        if err!=nil{
            fmt.Println(err)
            return []Quote{}
        }
        var quotes []Quote
        err=result.All(&quotes)
        if err!=nil{
            fmt.Println(err)
            return []Quote{}
        }
        fmt.Println(quotes)
        return quotes
    } else{
        return []Quote{}
    }
}

我编写了一个路由处理程序来调用上述函数:

import (
    "corate/util"
    "corate/model"
    "github.com/fatih/structs"
    "net/http"
)

func DashboardHandler(w http.ResponseWriter,r *http.Request){
    files:=[]string{"base","dashboard"}
    session,_:=util.GlobalSessions.SessionStart(w,r)
    defer session.SessionRelease(w)
    quotes:=model.GetQuotesByUser("ca8a2e14-f65b-43b1-b655-97d7c29190ec")
    q:=structs.Map(quotes)
    util.RenderTemplate(w,q,files...)
}

这是main函数:

func main(){

    // 设置数据库

    config.Setupdb()

    // 初始化会话管理器

    util.InitSessionManager()

    // 服务静态文件夹

    http.Handle("/public/",http.StripPrefix("/public/",http.FileServer(http.Dir("public"))))

    // 设置路由器和中间件

    http.HandleFunc("/",route.IndexHandler)
    http.HandleFunc("/login",route.GoogleLoginHandler)
    http.HandleFunc("/auth/google",route.GoogleCallbackHandler)

    http.Handle("/dashboard",negroni.New(
        negroni.HandlerFunc(route.IsAuthenticated),
        negroni.Wrap(http.HandlerFunc(route.DashboardHandler)),
    ))

    // 开始监听

    http.ListenAndServe(":3000",nil)
}

我使用astaxie/beego/session库来管理此服务器的会话:

import (
    "github.com/astaxie/beego/session"
)

var (
    GlobalSessions *session.Manager
)

func InitSessionManager() {
    GlobalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":3600}`)
    go GlobalSessions.GC()
}

当我启动服务器并登录并请求dashboard路径时,将处理DashboardHandler,调用GetQuotesByUser,它工作正常,它确实打印出查询的正确结果。但是在浏览器中,浏览器会抛出错误:

未接收到数据

ERR_EMPTY_RESPONSE

控制台还记录了一些错误:

http: panic serving 127.0.0.1:51229: not struct
goroutine 84 [running]:
net/http.(*conn).serve.func1(0xc82015fc00)
    /home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1389 +0xc1
panic(0x7f58e0, 0xc8204489f0)
    /home/kuro/.gvm/gos/go1.6/src/runtime/panic.go:426 +0x4e9
github.com/fatih/structs.strctVal(0x7de700, 0xc82045a340, 0x0, 0x0, 0x0)
    /home/kuro/Workspace/Go/src/github.com/fatih/structs/structs.go:426 +0x136
github.com/fatih/structs.New(0x7de700, 0xc82045a340, 0x0)
    /home/kuro/Workspace/Go/src/github.com/fatih/structs/structs.go:30 +0x2f
github.com/fatih/structs.Map(0x7de700, 0xc82045a340, 0xc82045a340)
    /home/kuro/Workspace/Go/src/github.com/fatih/structs/structs.go:435 +0x2b
corate/route.DashboardHandler(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
    /home/kuro/Workspace/Go/src/corate/route/user.go:19 +0x190
net/http.HandlerFunc.ServeHTTP(0xa47820, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
    /home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1618 +0x3a
github.com/codegangsta/negroni.Wrap.func1(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a60)
    /home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:41 +0x50
github.com/codegangsta/negroni.HandlerFunc.ServeHTTP(0xc82012ace0, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a60)
    /home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:24 +0x44
github.com/codegangsta/negroni.middleware.ServeHTTP(0x7fe2c2e6ade0, 0xc82012ace0, 0xc82012ad40, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
    /home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:33 +0xaa
github.com/codegangsta/negroni.(middleware).ServeHTTP-fm(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
    /home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:33 +0x53
corate/route.IsAuthenticated(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a00)
    /home/kuro/Workspace/Go/src/corate/route/isAuthenticated.go:14 +0x174
github.com/codegangsta/negroni.HandlerFunc.ServeHTTP(0xa47840, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a00)
    /home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:24 +0x44
github.com/codegangsta/negroni.middleware.ServeHTTP(0x7fe2c2e6ade0, 0xa47840, 0xc82012ad20, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
    /home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:33 +0xaa
github.com/codegangsta/negroni.(*Negroni).ServeHTTP(0xc8200cfe90, 0x7fe2c2e6af70, 0xc820154680, 0xc820372460)
    /home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:73 +0x122
net/http.(*ServeMux).ServeHTTP(0xc820013740, 0x7fe2c2e6af70, 0xc820154680, 0xc820372460)
    /home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1910 +0x17d
net/http.serverHandler.ServeHTTP(0xc82000e380, 0x7fe2c2e6af70, 0xc820154680, 0xc820372460)
    /home/kuro/.gvm/gos/go1.6/src/net/http/server.go:2081 +0x19e
net/http.(*conn).serve(0xc82015fc00)
    /home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
    /home/kuro/.gvm/gos/go1.6/src/net/http/server.go:2137 +0x44e

为什么会发生这种情况?然后我注意到,如果我不使用fatih/structs库,就不会出错。但是我想使用那个库,它会对我有很大帮助。我想知道这种奇怪行为的原因,以及如何在仍然使用fatih/structs库的情况下修复它。

英文:

I'm using gorethink driver, I write a query function like this in model

func GetQuotesByUser(idUser string) []Quote{
ids:=GetQuoteIdsByUser(idUser)
if (len(ids)>0){
result,err:=r.Table("quote").GetAll(ids...).Run(config.Connection())
defer result.Close()
if err!=nil{
fmt.Println(err)
return []Quote{}
}
var quotes []Quote
err=result.All(&quotes)
if err!=nil{
fmt.Println(err)
return []Quote{}
}
fmt.Println(quotes)
return quotes
} else{
return []Quote{}
}
}

I write a route handler that calls the above function

import (
"corate/util"
"corate/model"
"github.com/fatih/structs"
"net/http"
)
func DashboardHandler(w http.ResponseWriter,r *http.Request){
files:=[]string{"base","dashboard"}
session,_:=util.GlobalSessions.SessionStart(w,r)
defer session.SessionRelease(w)
quotes:=model.GetQuotesByUser("ca8a2e14-f65b-43b1-b655-97d7c29190ec")
q:=structs.Map(quotes)
util.RenderTemplate(w,q,files...)
}

This is main function

func main(){
// Setup database
config.Setupdb()
// Initialize session manager
util.InitSessionManager()
// Serve static folder
http.Handle("/public/",http.StripPrefix("/public/",http.FileServer(http.Dir("public"))))
// Setup routers and middlewares
http.HandleFunc("/",route.IndexHandler)
http.HandleFunc("/login",route.GoogleLoginHandler)
http.HandleFunc("/auth/google",route.GoogleCallbackHandler)
http.Handle("/dashboard",negroni.New(
negroni.HandlerFunc(route.IsAuthenticated),
negroni.Wrap(http.HandlerFunc(route.DashboardHandler)),
))
// Start listening
http.ListenAndServe(":3000",nil)
}

I use astaxie/beego/session library to manage sessions for this server

import (
"github.com/astaxie/beego/session"
)
var (
GlobalSessions *session.Manager
)
func InitSessionManager() {
GlobalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":3600}`)
go GlobalSessions.GC()
}

When I start the server, and login and request to dashboard path, DashboardHandler is processed,GetQuotesByUser is called, and it works, it works, it does print out true results of query. But in browser, the browser throws error:

No data received

ERR_EMPTY_RESPONSE

The console logs some errors as well

http: panic serving 127.0.0.1:51229: not struct
goroutine 84 [running]:
net/http.(*conn).serve.func1(0xc82015fc00)
/home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1389 +0xc1
panic(0x7f58e0, 0xc8204489f0)
/home/kuro/.gvm/gos/go1.6/src/runtime/panic.go:426 +0x4e9
github.com/fatih/structs.strctVal(0x7de700, 0xc82045a340, 0x0, 0x0, 0x0)
/home/kuro/Workspace/Go/src/github.com/fatih/structs/structs.go:426 +0x136
github.com/fatih/structs.New(0x7de700, 0xc82045a340, 0x0)
/home/kuro/Workspace/Go/src/github.com/fatih/structs/structs.go:30 +0x2f
github.com/fatih/structs.Map(0x7de700, 0xc82045a340, 0xc82045a340)
/home/kuro/Workspace/Go/src/github.com/fatih/structs/structs.go:435 +0x2b
corate/route.DashboardHandler(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
/home/kuro/Workspace/Go/src/corate/route/user.go:19 +0x190
net/http.HandlerFunc.ServeHTTP(0xa47820, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
/home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1618 +0x3a
github.com/codegangsta/negroni.Wrap.func1(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a60)
/home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:41 +0x50
github.com/codegangsta/negroni.HandlerFunc.ServeHTTP(0xc82012ace0, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a60)
/home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:24 +0x44
github.com/codegangsta/negroni.middleware.ServeHTTP(0x7fe2c2e6ade0, 0xc82012ace0, 0xc82012ad40, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
/home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:33 +0xaa
github.com/codegangsta/negroni.(middleware).ServeHTTP-fm(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
/home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:33 +0x53
corate/route.IsAuthenticated(0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a00)
/home/kuro/Workspace/Go/src/corate/route/isAuthenticated.go:14 +0x174
github.com/codegangsta/negroni.HandlerFunc.ServeHTTP(0xa47840, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460, 0xc8203f0a00)
/home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:24 +0x44
github.com/codegangsta/negroni.middleware.ServeHTTP(0x7fe2c2e6ade0, 0xa47840, 0xc82012ad20, 0x7fe2c2e6dfc8, 0xc8202d49c0, 0xc820372460)
/home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:33 +0xaa
github.com/codegangsta/negroni.(*Negroni).ServeHTTP(0xc8200cfe90, 0x7fe2c2e6af70, 0xc820154680, 0xc820372460)
/home/kuro/Workspace/Go/src/github.com/codegangsta/negroni/negroni.go:73 +0x122
net/http.(*ServeMux).ServeHTTP(0xc820013740, 0x7fe2c2e6af70, 0xc820154680, 0xc820372460)
/home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1910 +0x17d
net/http.serverHandler.ServeHTTP(0xc82000e380, 0x7fe2c2e6af70, 0xc820154680, 0xc820372460)
/home/kuro/.gvm/gos/go1.6/src/net/http/server.go:2081 +0x19e
net/http.(*conn).serve(0xc82015fc00)
/home/kuro/.gvm/gos/go1.6/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
/home/kuro/.gvm/gos/go1.6/src/net/http/server.go:2137 +0x44e

Why does it happen? Then I notice that if I don't utilize fatih/structs library, nothing will go wrong. But I want to use that library, it will help me much. I want to know reason of this strange behavior and how to fix it while still using fatih/structs library

答案1

得分: 1

GetQuotesByUser 返回的是一个切片,而不是一个结构体。所以你不能直接将它传递给 structs.Map。你需要遍历切片,并将每个元素单独转换为一个映射。

quotes := model.GetQuotesByUser("ca8a2e14-f65b-43b1-b655-97d7c29190ec")

qs := []map[string]interface{}{}
for _, q := range quotes {
    qs = append(qs, structs.Map(q))
}

util.RenderTemplate(w, qs, files...)

这样应该接近你想要的结果。

英文:

GetQuotesByUser returns a slice, not a struct. So you can't pass it directly to structs.Map. You'll have to iterate the slice and convert each element to a map individually.

quotes := model.GetQuotesByUser("ca8a2e14-f65b-43b1-b655-97d7c29190ec")
qs := []map[string]interface{}{}
for _, q := range quotes {
qs = append(qs, structs.Map(q))
}
util.RenderTemplate(w,qs,files...)

Should get you close to what you're after.

huangapple
  • 本文由 发表于 2016年3月17日 21:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/36062509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定