-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyfile.go
196 lines (154 loc) · 3.07 KB
/
tinyfile.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package tinyfile
import (
"context"
"errors"
"os"
"os/signal"
"sync"
"syscall"
)
type OpenFileFunc func(string, int, os.FileMode) (*os.File, error)
var OpenFile OpenFileFunc = os.OpenFile
var (
FileFlg = os.O_CREATE | os.O_WRONLY | os.O_APPEND
FilePermission os.FileMode = 0664
)
// TinyWriter is zapcore.WriteSyncer
type TinyWriter struct {
mu sync.Mutex
fp *os.File
}
func (s *TinyWriter) ReOpen() error {
if s.fp == nil {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
s.fp.Close()
file, err := OpenFile(s.fp.Name(), FileFlg, FilePermission)
if err != nil {
return err
}
s.fp = file
return nil
}
func (s *TinyWriter) Close() error {
if s.fp == nil {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
return s.fp.Close()
}
// write to file
func (s *TinyWriter) Write(p []byte) (n int, err error) {
if s.fp == nil {
return 0, errors.New("file not found")
}
s.mu.Lock()
defer s.mu.Unlock()
return s.fp.Write(p)
}
func (s *TinyWriter) Sync() error {
if s.fp == nil {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
return s.fp.Sync()
}
var defaultRepo = NewRepo()
func NewRepo() *SyncRepo {
return &SyncRepo{
files: []*TinyWriter{},
}
}
// create sync object
func NewWriter(path string) (*TinyWriter, error) {
return defaultRepo.New(path)
}
// watch SIGHUP
func Watch(ctx context.Context) {
defaultRepo.WatchWithSignal(ctx, syscall.SIGHUP)
}
// watch signal
func WatchWithSignal(ctx context.Context, sig ...os.Signal) {
defaultRepo.WatchWithSignal(ctx, sig...)
}
// Close is close all files
func Close() {
defaultRepo.Close()
}
// SyncRepo is TinyFile struct controller.
// that wach some signal and ma
type SyncRepo struct {
mu sync.Mutex
files []*TinyWriter
cancel func()
}
// create SyncRepository
func (sr *SyncRepo) New(path string) (*TinyWriter, error) {
sr.mu.Lock()
defer sr.mu.Unlock()
file, err := OpenFile(path, FileFlg, FilePermission)
if err != nil {
return nil, err
}
snk := &TinyWriter{
fp: file,
}
sr.files = append(sr.files, snk)
return snk, nil
}
// Watch start SIGHUP watchng process
func (sr *SyncRepo) Watch(ctx context.Context) {
sr.WatchWithSignal(ctx, syscall.SIGHUP)
}
// WatchWithSignal start signal watching function
func (sr *SyncRepo) WatchWithSignal(ctx context.Context, sig ...os.Signal) {
ctx, sr.cancel = context.WithCancel(ctx)
go func() {
sigchan := make(chan os.Signal, 1)
defer func() {
close(sigchan)
}()
signal.Notify(sigchan, sig...)
LOOP:
for {
select {
case <-sigchan:
sr.ReOpen()
case <-ctx.Done():
break LOOP
}
}
}()
}
// ReOpen all management under TinyWriter
func (sr *SyncRepo) ReOpen() []error {
sr.mu.Lock()
defer sr.mu.Unlock()
ers := []error{}
for _, snk := range sr.files {
err := snk.ReOpen()
if err != nil {
ers = append(ers, err)
}
}
if len(ers) == 0 {
return nil
}
return ers
}
// Close is close all management under TinyWriter
func (sr *SyncRepo) Close() {
sr.mu.Lock()
defer sr.mu.Unlock()
if sr.cancel != nil {
sr.cancel()
sr.cancel = nil
}
for _, snk := range sr.files {
snk.Close()
}
}