douhanxujiuji6098 2016-09-22 16:24
浏览 85

ImageMagick Go API HTTP挂在ReadImageBlob上

I have written an Beego HTTP server, that when a user hits an endpoint:

  • the server requests an image from another server (for instance imgur)
  • it reads the bytes of the image and passes them to gographics/imagick
  • this (should) resize the image and return the byte array of the result

what actually happens is my HTTP server completely hangs, I don't even get to the error handling, and I get a 502 bad gateway on all endpoints of the server.

My code looks like this:

func processContactImage(idx int, image []byte) ([]byte, error) {
    imagick.Initialize()
    defer imagick.Terminate()
    log.Println("idx: ", idx)
    mw := imagick.NewMagickWand()

    log.Println("reading image blob: ", image)
    err := mw.ReadImageBlob(image)
    if err != nil {
        log.Println("reading blob failed: ", err)
        return []byte{}, err
    }
//...
}

I can see in the terminal the log message "reading image blob: [bytes, bytes bytes]" and I have copied the bytes printed to another small program to test the bytes do indeed hold an image, they do. It completely hangs on err := mw.ReadImageBlob(image) and I don't think it even gets into the if err != nil as I never see that log message.

Tips on how I should debug this would be welcome. I have written a small program to test the image magick funtions work on the byte array in a standalone enviroment, and it all works fine.

My thoughts:

  • I don't completely understand how Go handles stack/heap, I thought it was able to move things to the heap if necessary and I didn't need to manage this. However I am storing an image in memory, I thought perhaps seg fault but Im not sure why it doesn't crash but hangs...
  • ReadImageBlob is expecting a type of image data, and its not getting it, although I would have then thought it got to the error

EDIT:

OK thanks for comments, after more research, it seems like it is related to the fact I am running this in Docker, it hadn't occured that this could be an issue however:

  1. I have moved the initialization of imagemagick to main and the error still occurs
  2. When I run the application without docker, and I pass a byte array to the handler, imagemagick code runs fine.
  3. When I attach to the docker container, add a small test program that adds a circle to an image using imagemagick (however not a web service, just a binary) it works, albeit very slowly

My dockerfile looks like this:

FROM golang:1.7-alpine

RUN apk update && apk add git && apk add g++ && apk add bzr && \
    rm -rf /var/cache/apk/*

# ENV GOPATH /go

# Install beego & bee
RUN go get github.com/astaxie/beego
RUN go get github.com/beego/bee
RUN go get github.com/tools/godep

RUN apk add --update alpine-sdk
RUN apk add imagemagick-dev
RUN go get gopkg.in/gographics/imagick.v2/imagick

I wonder if I am missing a library or something and its hanging inside the C api and Go is waiting for a response. Is there any way I can debug this?

OK... As it turns out the issue is something else... perhaps multiple requests at once or something... I'm not sure, but I have created this gist demonstrating using imagemagick, in Go, in a handler, and it works locally no problem inside my docker container. The mystery continues....

  • 写回答

2条回答 默认 最新

  • donglue8180 2016-09-22 22:34
    关注

    Don't do this in your handler:

    imagick.Initialize()
    defer imagick.Terminate()
    

    That is only supposed to be done once, in your main()

    Most likely you are conflicting with different requests, by tearing down the entire ImageMagick each time a request finishes.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)