-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
126 lines (113 loc) · 2.59 KB
/
server.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
package main
import (
"bytes"
"errors"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
// exit server after this much idle time
idleTime = time.Hour
// delete files that are this old
cacheTTL = 60 * 24 * time.Hour
)
func getSystemdSocket() (net.Listener, error) {
if pid, _ := strconv.Atoi(os.Getenv("LISTEN_PID")); pid != os.Getpid() {
return nil, errors.New("wrong pid")
} else if n, _ := strconv.Atoi(os.Getenv("LISTEN_FDS")); n == 0 {
return nil, errors.New("no fds provided by systemd")
} else if n > 1 {
return nil, errors.New("too many fds provided by systemd")
}
f := os.NewFile(3, "sock")
listener, err := net.FileListener(f)
if err != nil {
return nil, err
}
f.Close()
return listener, nil
}
func checkIdle(activity chan struct{}, idle time.Duration, fn func()) {
for {
select {
case <-activity:
case <-time.After(idle):
fn()
}
}
}
func cleanBuildDirs(cacheDir string) {
ents, err := os.ReadDir(cacheDir)
if err != nil {
return
}
for _, ent := range ents {
if strings.HasPrefix(ent.Name(), BuildIDPrefix) {
os.RemoveAll(filepath.Join(cacheDir, ent.Name()))
}
}
}
func isMountedNoatime(dir string) bool {
out, err := exec.Command("findmnt", "-T", dir, "-o", "options").Output()
if err != nil {
return false
}
return bytes.Contains(out, []byte("noatime"))
}
func serverMain() {
log.SetFlags(log.Lshortfile)
listener, err := getSystemdSocket()
if err != nil {
log.Fatalln("get listen socket:", err)
}
cacheDir := os.Getenv("CACHE_DIRECTORY")
if cacheDir == "" {
log.Fatalln("systemd didn't set CACHE_DIRECTORY")
}
objDir := filepath.Join(cacheDir, "obj")
os.MkdirAll(objDir, 0755)
dc := &DiskCache{Dir: objDir, ManualATime: isMountedNoatime(cacheDir)}
exitServer := func() {
cleanBuildDirs(cacheDir)
dc.Clean(cacheTTL)
os.Exit(0)
}
activity := make(chan struct{}, 1)
go checkIdle(activity, idleTime, exitServer)
// TODO: shut down after a while
for {
activity <- struct{}{}
conn, err := listener.Accept()
if err != nil {
log.Println("Accept:", err)
continue
}
var p *Process
p = &Process{
In: conn,
Out: conn,
CacheDir: cacheDir,
Get: dc.Get,
Put: dc.Put,
Close: func() error {
log.Printf("cache: %d gets (%d hits, %d misses, %d errors); %d puts (%d errors)",
p.Gets.Load(), p.GetHits.Load(), p.GetMisses.Load(), p.GetErrors.Load(), p.Puts.Load(), p.PutErrors.Load())
return nil
},
}
go func() {
err := p.Run()
if err != nil {
log.Println("run returned", err)
}
conn.Close()
activity <- struct{}{}
}()
}
}