1
0

processing_handler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. responseGzipBufPool *bufPool
  12. responseGzipPool *gzipPool
  13. processingSem chan struct{}
  14. headerVaryValue string
  15. )
  16. func initProcessingHandler() {
  17. processingSem = make(chan struct{}, conf.Concurrency)
  18. if conf.GZipCompression > 0 {
  19. responseGzipBufPool = newBufPool("gzip", conf.Concurrency, conf.GZipBufferSize)
  20. responseGzipPool = newGzipPool(conf.Concurrency)
  21. }
  22. vary := make([]string, 0)
  23. if conf.EnableWebpDetection || conf.EnforceWebp {
  24. vary = append(vary, "Accept")
  25. }
  26. if conf.GZipCompression > 0 {
  27. vary = append(vary, "Accept-Encoding")
  28. }
  29. if conf.EnableClientHints {
  30. vary = append(vary, "DPR", "Viewport-Width", "Width")
  31. }
  32. headerVaryValue = strings.Join(vary, ", ")
  33. }
  34. func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, data []byte) {
  35. po := getProcessingOptions(ctx)
  36. rw.Header().Set("Expires", time.Now().Add(time.Second*time.Duration(conf.TTL)).Format(http.TimeFormat))
  37. rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", conf.TTL))
  38. rw.Header().Set("Content-Type", po.Format.Mime())
  39. rw.Header().Set("Content-Disposition", po.Format.ContentDisposition(getImageURL(ctx)))
  40. if len(headerVaryValue) > 0 {
  41. rw.Header().Set("Vary", headerVaryValue)
  42. }
  43. if conf.GZipCompression > 0 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  44. buf := responseGzipBufPool.Get(0)
  45. defer responseGzipBufPool.Put(buf)
  46. gz := responseGzipPool.Get(buf)
  47. defer responseGzipPool.Put(gz)
  48. gz.Write(data)
  49. gz.Close()
  50. rw.Header().Set("Content-Encoding", "gzip")
  51. rw.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
  52. rw.WriteHeader(200)
  53. rw.Write(buf.Bytes())
  54. } else {
  55. rw.Header().Set("Content-Length", strconv.Itoa(len(data)))
  56. rw.WriteHeader(200)
  57. rw.Write(data)
  58. }
  59. logResponse(reqID, 200, fmt.Sprintf("Processed in %s: %s; %+v", getTimerSince(ctx), getImageURL(ctx), po))
  60. }
  61. func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
  62. ctx := context.Background()
  63. if newRelicEnabled {
  64. var newRelicCancel context.CancelFunc
  65. ctx, newRelicCancel = startNewRelicTransaction(ctx, rw, r)
  66. defer newRelicCancel()
  67. }
  68. if prometheusEnabled {
  69. prometheusRequestsTotal.Inc()
  70. defer startPrometheusDuration(prometheusRequestDuration)()
  71. }
  72. processingSem <- struct{}{}
  73. defer func() { <-processingSem }()
  74. ctx, timeoutCancel := startTimer(ctx, time.Duration(conf.WriteTimeout)*time.Second)
  75. defer timeoutCancel()
  76. ctx, err := parsePath(ctx, r)
  77. if err != nil {
  78. panic(err)
  79. }
  80. ctx, downloadcancel, err := downloadImage(ctx)
  81. defer downloadcancel()
  82. if err != nil {
  83. if newRelicEnabled {
  84. sendErrorToNewRelic(ctx, err)
  85. }
  86. if prometheusEnabled {
  87. incrementPrometheusErrorsTotal("download")
  88. }
  89. panic(err)
  90. }
  91. checkTimeout(ctx)
  92. if conf.ETagEnabled {
  93. eTag := calcETag(ctx)
  94. rw.Header().Set("ETag", eTag)
  95. if eTag == r.Header.Get("If-None-Match") {
  96. logResponse(reqID, 304, "Not modified")
  97. rw.WriteHeader(304)
  98. return
  99. }
  100. }
  101. checkTimeout(ctx)
  102. imageData, processcancel, err := processImage(ctx)
  103. defer processcancel()
  104. if err != nil {
  105. if newRelicEnabled {
  106. sendErrorToNewRelic(ctx, err)
  107. }
  108. if prometheusEnabled {
  109. incrementPrometheusErrorsTotal("processing")
  110. }
  111. panic(err)
  112. }
  113. checkTimeout(ctx)
  114. respondWithImage(ctx, reqID, r, rw, imageData)
  115. }