-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
64 lines (51 loc) · 1.05 KB
/
helpers.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
package goherence
import (
"fmt"
"io"
"net/http"
"strconv"
"sync"
"time"
)
type logLocker struct {
l sync.Locker
name string
}
func (ll logLocker) Lock() {
fmt.Printf("wait for lock %q\n", ll.name)
ll.l.Lock()
fmt.Printf("acquired lock %q\n", ll.name)
}
func (ll logLocker) Unlock() {
ll.l.Unlock()
fmt.Printf("released lock %q\n", ll.name)
}
func getSince(r *http.Request) (int64, *Error) {
sinceStr := r.URL.Query()["since"][0]
if sinceStr == "" {
return 0, newError(`missing query parameter "since"`, nil, 400)
}
since, err := strconv.Atoi(sinceStr)
if err != nil {
return 0, newError(
`error parsing query parameter "since"`,
err, 400)
}
return int64(since), nil
}
func millis(t time.Time) int64 {
return t.UnixNano() / 1000000
}
type writerToFunc func(w io.Writer) (int64, error)
func (wt writerToFunc) WriteTo(w io.Writer) (int64, error) {
return wt(w)
}
type countWriter struct {
n int64
w io.Writer
}
func (cw *countWriter) Write(data []byte) (int, error) {
n, err := cw.w.Write(data)
cw.n += int64(n)
return n, err
}