-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconn.go
222 lines (194 loc) · 5.06 KB
/
conn.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package governor
import (
"bufio"
"context"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/github/spokes-receive-pack/internal/sockstat"
)
const (
connectTimeout = time.Second
)
func scheduleTimeout() time.Duration {
timeout := time.Second
if v := os.Getenv("SCHEDULE_CMD_TIMEOUT"); v != "" {
if d, err := strconv.ParseInt(v, 10, 64); err == nil {
timeout = time.Duration(d) * time.Millisecond
}
}
return timeout
}
func shouldFailClosed() bool {
return os.Getenv("FAIL_CLOSED") == "1"
}
// Start connects to governor and sends the "update" and "schedule" messages.
//
// If "schedule" says to wait, Start will pause for the specified time and try
// calling "schedule" again.
//
// If there is a connection or other low level error when talking to governor,
// Start will return (nil, nil).
func Start(ctx context.Context, gitDir string) (*Conn, error) {
sock, err := connect(ctx)
if err != nil {
return nil, nil
}
updateData := readSockstat(os.Environ())
updateData.PID = os.Getpid()
updateData.Program = "spokes-receive-pack"
updateData.GitDir = gitDir
if err := update(sock, updateData); err != nil {
sock.Close()
return nil, nil
}
timeout := scheduleTimeout()
failClosed := shouldFailClosed()
br := bufio.NewReader(sock)
for {
// Give governor a limited time to respond.
if err := sock.SetReadDeadline(time.Now().Add(timeout)); err != nil {
break
}
err := schedule(br, sock)
if err == nil {
break
}
switch e := err.(type) {
case WaitError:
time.Sleep(e.Duration)
case FailError:
sock.Close()
return nil, err
case net.Error:
sock.Close()
if e.Timeout() && failClosed {
return nil, err
}
return nil, nil
default:
sock.Close()
return nil, nil
}
}
return &Conn{sock: sock}, nil
}
// Conn is an active connection to governor.
type Conn struct {
sock net.Conn
finish finishData
}
// SetError stores an error to include with the finish message.
//
// It is safe to call SetError with a nil *Conn.
func (c *Conn) SetError(exitCode uint8, message string) {
if c == nil {
return
}
c.finish.ResultCode = exitCode
c.finish.Fatal = message
}
// SetReceivePackSize records the incoming packfile's size to include with the
// finish message.
//
// It is safe to call SetReceivePackSize with a nil *Conn.
func (c *Conn) SetReceivePackSize(size int64) {
if c == nil {
return
}
if size > 0 {
c.finish.ReceivePackSize = uint64(size)
}
}
// Finish sends the "finish" message to governor and closes the connection.
//
// It is safe to call Finish with a nil *Conn.
func (c *Conn) Finish(ctx context.Context) {
if c == nil || c.sock == nil {
return
}
stats := getProcStats()
c.finish.CPU = stats.CPU
c.finish.RSS = stats.RSS
c.finish.DiskReadBytes = stats.DiskReadBytes
c.finish.DiskWriteBytes = stats.DiskWriteBytes
_ = finish(c.sock, c.finish)
c.sock.Close()
c.sock = nil
}
type procStats struct {
CPU uint32
RSS uint64
DiskReadBytes uint64
DiskWriteBytes uint64
}
func connect(ctx context.Context) (net.Conn, error) {
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
defer cancel()
path := os.Getenv("GIT_SOCKSTAT_PATH")
if path == "" {
path = "/var/run/gitmon/gitstats.sock"
}
dialer := &net.Dialer{}
return dialer.DialContext(ctx, "unix", path)
}
func readSockstat(environ []string) updateData {
var res updateData
for _, env := range environ {
if !strings.HasPrefix(env, sockstat.Prefix) {
continue
}
env = env[len(sockstat.Prefix):]
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
continue
}
switch parts[0] {
case "repo_name":
res.RepoName = sockstat.StringValue(parts[1])
case "repo_id":
res.RepoID = sockstat.Uint32Value(parts[1])
case "network_id":
res.NetworkID = sockstat.Uint32Value(parts[1])
case "user_id":
res.UserID = sockstat.Uint32Value(parts[1])
case "real_ip":
res.RealIP = sockstat.StringValue(parts[1])
case "request_id":
res.RequestID = sockstat.StringValue(parts[1])
case "user_agent":
res.UserAgent = sockstat.StringValue(parts[1])
case "features":
res.Features = sockstat.StringValue(parts[1])
case "via":
res.Via = sockstat.StringValue(parts[1])
case "ssh_connection":
res.SSHConnection = sockstat.StringValue(parts[1])
case "babeld":
res.Babeld = sockstat.StringValue(parts[1])
case "git_protocol":
res.GitProtocol = sockstat.StringValue(parts[1])
case "pubkey_verifier_id":
res.PubkeyVerifierID = sockstat.Uint32Value(parts[1])
case "pubkey_creator_id":
res.PubkeyCreatorID = sockstat.Uint32Value(parts[1])
case "max_delay":
res.MaxDelay = sockstat.Uint32Value(parts[1])
case "command_id":
res.CommandID = sockstat.StringValue(parts[1])
case "group_id":
res.GroupID = sockstat.StringValue(parts[1])
case "group_leader":
res.GroupLeader = sockstat.GetBool(parts[1])
case "is_importing":
res.IsImporting = sockstat.BoolValue(parts[1])
case "import_skip_push_limit":
res.ImportSkipPushLimit = sockstat.BoolValue(parts[1])
case "import_soft_throttling":
res.ImportSoftThrottling = sockstat.BoolValue(parts[1])
}
}
return res
}