main.go 1.0 KB

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