Repository: syncthing/syncthing
File: cmd/syncthing/gui.go (Go)

Code before:
	runtime.GC()
	pprof.WriteHeapProfile(&heapBuffer)
	arrayFile = append(arrayFile, fileEntry{name: filename, data: heapBuffer.Bytes()})

	const duration = 4 * time.Second
	filename = fmt.Sprintf("syncthing-cpu-%s-%s-%s-%s.pprof", runtime.GOOS, runtime.GOARCH, Version, time.Now().Format("150405")) // hhmmss
	pprof.StartCPUProfile(&cpuBuffer)
	time.Sleep(duration)
	pprof.StopCPUProfile()
	arrayFile = append(arrayFile, fileEntry{name: filename, data: cpuBuffer.Bytes()})

	path := filepath.Join(baseDirs["config"], "support_bundle")
	os.MkdirAll(path, 0700)

	output := time.Now().Format("2006-01-02T15.04.05") + ".zip"
	newfilepath := filepath.Join(path, output)
	newfile, _ := os.Create(newfilepath)
	defer newfile.Close()

	// Add files to zip
	zipBuffer(newfile, arrayFile)

	// Delete old zips over 10 (fifo)
	eraseSupportBundleZip(path)

	from, _ := os.Open(newfilepath)
	w.Header().Set("Content-Type", "application/zip")
	w.Header().Set("Content-Disposition", "attachment; filename="+output)
	http.ServeFile(w, r, newfilepath)
	io.Copy(w, from)
}

func (s *apiService) getSystemHTTPMetrics(w http.ResponseWriter, r *http.Request) {
	stats := make(map[string]interface{})
	metrics.Each(func(name string, intf interface{}) {
		if m, ok := intf.(*metrics.StandardTimer); ok {
			pct := m.Percentiles([]float64{0.50, 0.95, 0.99})
			for i := range pct {
				pct[i] /= 1e6 // ns to ms
			}
			stats[name] = map[string]interface{}{
				"count":         m.Count(),
				"sumMs":         m.Sum() / 1e6, // ns to ms
				"ratesPerS":     []float64{m.Rate1(), m.Rate5(), m.Rate15()},
				"percentilesMs": pct,
			}
		}
	})
	bs, _ := json.MarshalIndent(stats, "", "  ")
	w.Write(bs)
}

Review comment (suggestion):
You're doing something odd here, I think maybe serving the file twice. `http.ServeFile` serves directly from the filesystem so no need to open first. (You do however need to close the writing side, above.) Alternately we could do this in memory - have `zipBuffer` zip *into* another buffer, write those to file and also write the same bytes to the request.

Code after:
		"os":          runtime.GOOS,
		"arch":        runtime.GOARCH,
	}, "", "  "); err == nil {
		files = append(files, fileEntry{name: "version-platform.json.txt", data: versionPlatform})
	} else {
		l.Warnln("Failed to create versionPlatform.json: ", err)
	}

	// Report Data as a JSON
	if usageReportingData, err := json.MarshalIndent(reportData(s.cfg, s.model, s.connectionsService, usageReportVersion, true), "", "  "); err != nil {
		l.Warnln("Support bundle: failed to create versionPlatform.json:", err)
	} else {
		files = append(files, fileEntry{name: "usage-reporting.json.txt", data: usageReportingData})
	}

	// Heap and CPU Proofs as a pprof extension
	var heapBuffer, cpuBuffer bytes.Buffer
	filename := fmt.Sprintf("syncthing-heap-%s-%s-%s-%s.pprof", runtime.GOOS, runtime.GOARCH, Version, time.Now().Format("150405")) // hhmmss
	runtime.GC()
	pprof.WriteHeapProfile(&heapBuffer)
	files = append(files, fileEntry{name: filename, data: heapBuffer.Bytes()})

	const duration = 4 * time.Second
	filename = fmt.Sprintf("syncthing-cpu-%s-%s-%s-%s.pprof", runtime.GOOS, runtime.GOARCH, Version, time.Now().Format("150405")) // hhmmss
	pprof.StartCPUProfile(&cpuBuffer)
	time.Sleep(duration)
	pprof.StopCPUProfile()
	files = append(files, fileEntry{name: filename, data: cpuBuffer.Bytes()})

	// Add buffer files to buffer zip
	var zipFilesBuffer bytes.Buffer
	if err := writeZip(&zipFilesBuffer, files); err != nil {
		l.Warnln("Support bundle: failed to create support bundle zip:", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Set zip file name and path
	zipFileName := fmt.Sprintf("support-bundle-%s.zip", time.Now().Format("2018-01-02T15.04.05"))
	zipFilePath := filepath.Join(baseDirs["config"], zipFileName)

	// Write buffer zip to local zip file (back up)
	if err := ioutil.WriteFile(zipFilePath, zipFilesBuffer.Bytes(), 0600); err != nil {
		l.Warnln("Support bundle: support bundle zip could not be created:", err)
	}

	// Serve the buffer zip to client for download
	w.Header().Set("Content-Type", "application/zip")
	w.Header().Set("Content-Disposition", "attachment; filename="+zipFileName)
	io.Copy(w, &zipFilesBuffer)
}