main.go 856 B

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