-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
153 lines (132 loc) · 3.09 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package pprofs
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
const (
EnvPprofPrefix = "PPROF_PREFIX"
EnvPprofDir = "PPROF_DIR"
EnvPprofTtl = "PPROF_TTL"
)
const (
pprofFileSuffix = ".pb.gz"
)
// Storage is an abstraction of storage output.
type Storage interface {
// WriteCloser should return an io.WriteCloser according to the profile name and current time.
WriteCloser(name string, t time.Time) (io.WriteCloser, error)
}
// FileStorage stores the profile results to file with automatic cleaning.
type FileStorage struct {
prefix string
dir string
ttl time.Duration
cleaning bool
cleaningM *sync.Mutex
}
// NewFileStorage return a FileStorage,
// prefix indicates the prefix of the files,
// dir indicates where to save the files,
// ttl indicates the time-to-live of the files.
func NewFileStorage(prefix, dir string, ttl time.Duration) *FileStorage {
return &FileStorage{
prefix: prefix,
dir: dir,
ttl: ttl,
cleaning: false,
cleaningM: &sync.Mutex{},
}
}
// NewFileStorageFromEnv return a FileStorage,
// it read arguments from environment variables.
func NewFileStorageFromEnv() *FileStorage {
prefix := filepath.Base(os.Args[0])
if v := os.Getenv(EnvPprofPrefix); v != "" {
prefix = v
}
dir := filepath.Join(os.TempDir(), "pprofs")
if v := os.Getenv(EnvPprofDir); v != "" {
dir = v
}
ttl := 24 * time.Hour
if v := os.Getenv(EnvPprofTtl); v != "" {
if t, err := time.ParseDuration(v); err != nil {
ttl = t
}
}
return NewFileStorage(prefix, dir, ttl)
}
// WriteCloser implements Storage.
func (s *FileStorage) WriteCloser(name string, t time.Time) (io.WriteCloser, error) {
if err := os.MkdirAll(s.dir, 0755); err != nil {
return nil, fmt.Errorf("mkdir %q: %w", s.dir, err)
}
file := fmt.Sprintf("%s-%s.%s"+pprofFileSuffix, s.prefix, t.Format("20060102T150405"), name)
path := filepath.Join(s.dir, file)
return s.newWriteCloseCleaner(path), nil
}
func (s *FileStorage) clean() {
cleaning := false
s.cleaningM.Lock()
cleaning = s.cleaning
if !s.cleaning {
s.cleaning = true
}
s.cleaningM.Unlock()
if cleaning {
return
}
files, err := os.ReadDir(s.dir)
if err != nil {
return
}
for _, v := range files {
if v.IsDir() {
continue
}
if name := v.Name(); strings.HasSuffix(name, pprofFileSuffix) {
if info, err := v.Info(); err == nil {
if time.Since(info.ModTime()) > s.ttl {
_ = os.Remove(filepath.Join(s.dir, name))
}
}
}
}
s.cleaningM.Lock()
s.cleaning = false
s.cleaningM.Unlock()
}
func (s *FileStorage) newWriteCloseCleaner(path string) *writeCloseCleaner {
return &writeCloseCleaner{
path: path,
clean: s.clean,
}
}
type writeCloseCleaner struct {
path string
file *os.File
clean func()
}
func (c *writeCloseCleaner) Write(p []byte) (n int, err error) {
if c.file == nil {
f, err := os.Create(c.path)
if err != nil {
return 0, fmt.Errorf("create %q: %w", c.path, err)
}
c.file = f
}
return c.file.Write(p)
}
func (c *writeCloseCleaner) Close() error {
var err error
if c.file != nil {
err = c.file.Close()
}
c.clean()
return err
}