This repository has been archived by the owner on May 23, 2023. It is now read-only.
/
bucket.go
122 lines (104 loc) · 2.53 KB
/
bucket.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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package analytics
import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"sync"
)
func newBucket(m *manager, up uploader, tenant, dir string) (*bucket, error) {
b := &bucket{
manager: m,
uploader: up,
tenant: tenant,
dir: dir,
incoming: make(chan []Record, m.sendChannelSize),
}
var tempFileSpec string
if up.isGzipped() {
tempFileSpec = fmt.Sprintf("%d-*.gz", b.manager.now().Unix())
} else {
tempFileSpec = fmt.Sprintf("%d-*.txt", b.manager.now().Unix())
}
f, err := ioutil.TempFile(b.dir, tempFileSpec)
if err != nil {
m.log.Errorf("AX Records lost. Can't create bucket file: %s", err)
return nil, err
}
b.w = &fileWriter{
file: f,
writer: f,
}
if up.isGzipped() {
b.w.writer = gzip.NewWriter(f)
}
m.env.ScheduleDaemon(b.runLoop)
return b, nil
}
// A bucket writes analytics to a temp file
type bucket struct {
manager *manager
uploader uploader
tenant string
dir string
w *fileWriter
incoming chan []Record
wait *sync.WaitGroup
}
// write records to bucket
func (b *bucket) write(records []Record) {
if b != nil && len(records) > 0 {
b.incoming <- records
}
}
// close bucket
func (b *bucket) close(wait *sync.WaitGroup) {
b.wait = wait
close(b.incoming)
}
func (b *bucket) fileName() string {
return b.w.file.Name()
}
func (b *bucket) runLoop() {
log := b.manager.log
for records := range b.incoming {
b.uploader.write(records, b.w.writer)
}
if err := b.w.close(); err != nil {
log.Errorf("Can't close bucket file: %s", err)
}
b.manager.stageFile(b.tenant, b.fileName())
if b.wait != nil {
b.wait.Done()
}
log.Debugf("bucket closed: %s", b.fileName())
}
type fileWriter struct {
file *os.File
writer io.Writer
}
func (w *fileWriter) close() error {
if gzw, ok := w.writer.(*gzip.Writer); ok {
if err := gzw.Close(); err != nil {
return fmt.Errorf("gz.Close: %s", err)
}
}
if err := w.file.Close(); err != nil {
return fmt.Errorf("f.Close: %s", err)
}
return nil
}