-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
66 lines (55 loc) · 2.15 KB
/
request.go
File metadata and controls
66 lines (55 loc) · 2.15 KB
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
package tract
import (
"context"
"time"
)
// Request is the object that is passed along the tract.
// It keeps track of state by storing data via context values
type Request context.Context
// requestTimeStart is the key to retreive the generation time from a request.
// Request value type is time.Time
type requestTimeStartKey struct{}
// GetRequestStartTime get the time the request was generated.
// If there is no start time, the zero value of time.Time is returned.
func GetRequestStartTime(r Request) time.Time {
startTime, _ := r.Value(requestTimeStartKey{}).(time.Time)
return startTime
}
func setRequestStartTime(r Request, t time.Time) Request {
return context.WithValue(r, requestTimeStartKey{}, t)
}
// Request value type is cleanups
type cleanupKey struct{}
type cleanups []func(r Request, success bool)
// AddRequestCleanup add a function to the request that will be run when the request dies.
// This happens either when it reaches the end of a pool with no user set output, or a worker
// specified that the request should no longer continue.
func AddRequestCleanup(r Request, f func(Request, bool)) Request {
if f == nil {
return r
}
cleanupFuncs, _ := r.Value(cleanupKey{}).(cleanups)
cleanupFuncs = append(cleanupFuncs, f)
return context.WithValue(r, cleanupKey{}, cleanupFuncs)
}
// RemoveAllRequestCleanups removes all of the cleanups attached to the request.
// This does not run the cleanups.
func RemoveAllRequestCleanups(r Request) Request {
return context.WithValue(r, cleanupKey{}, nil)
}
// CleanupRequest manually calls all the cleanup functions attached to the request.
// This does not remove the cleanups.
func CleanupRequest(r Request, success bool) {
cleanupRequest(r, success)
}
func cleanupRequest(r Request, success bool) {
cleanupFuncs, _ := r.Value(cleanupKey{}).(cleanups)
for _, f := range cleanupFuncs {
f(r, success)
}
}
// swapCleanups sets the request cleanup ot be the provided cleanup, and retunrs the old cleanup.
func swapCleanups(r Request, cleanupFuncs cleanups) (Request, cleanups) {
oldCleaupFuncs, _ := r.Value(cleanupKey{}).(cleanups)
return context.WithValue(r, cleanupKey{}, cleanupFuncs), oldCleaupFuncs
}