main.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "os"
  4. "os/signal"
  5. "runtime"
  6. "runtime/debug"
  7. "syscall"
  8. "time"
  9. "net/http"
  10. _ "net/http/pprof"
  11. )
  12. const version = "2.2.13"
  13. type ctxKey string
  14. func initialize() {
  15. initSyslog()
  16. configure()
  17. initNewrelic()
  18. initPrometheus()
  19. initDownloading()
  20. initErrorsReporting()
  21. initVips()
  22. }
  23. func main() {
  24. initialize()
  25. go func() {
  26. var logMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
  27. for range time.Tick(time.Duration(conf.FreeMemoryInterval) * time.Second) {
  28. debug.FreeOSMemory()
  29. if logMemStats {
  30. var m runtime.MemStats
  31. runtime.ReadMemStats(&m)
  32. logNotice("[MEMORY USAGE] Sys: %d; HeapIdle: %d; HeapInuse: %d", m.Sys/1024/1024, m.HeapIdle/1024/1024, m.HeapInuse/1024/1024)
  33. }
  34. }
  35. }()
  36. if len(os.Getenv("IMGPROXY_PPROF_BIND")) > 0 {
  37. go func() {
  38. http.ListenAndServe(os.Getenv("IMGPROXY_PPROF_BIND"), nil)
  39. }()
  40. }
  41. s := startServer()
  42. stop := make(chan os.Signal, 1)
  43. signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
  44. <-stop
  45. shutdownServer(s)
  46. shutdownVips()
  47. }