forked from dropbox/llama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
92 lines (78 loc) · 1.93 KB
/
Copy pathutil.go
File metadata and controls
92 lines (78 loc) · 1.93 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
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
package udprobe
import (
"log"
"net"
"os"
"time"
"github.com/google/uuid"
)
const (
// Listens on any addr to an automatically assigned port number
DefaultAddrStr = "0.0.0.0:0"
DefaultTos = byte(0)
DefaultRcvBuff = 2097600 // 2MiB
DefaultReadTimeout = 200 * time.Millisecond
DefaultCacheTimeout = 2 * time.Second
DefaultCacheCleanRate = 5 * time.Second
ExpireNow = time.Nanosecond
)
// NewID returns 10 bytes of a new UUID4 as a string.
//
// This should be unique enough for short-lived cases, but as it's only a
// partial UUID.
func NewID() string {
fullUUID := uuid.New()
last10 := fullUUID[len(fullUUID)-10:]
return string(last10)
}
// IDTo10Bytes converts a string to a 10 byte array.
func IDToBytes(id string) [10]byte {
var arr [10]byte
copy(arr[:], id)
return arr
}
// NowUint64 returns the current time in nanoseconds as a uint64.
func NowUint64() uint64 {
return uint64(time.Now().UnixNano())
}
func HandleError(err error) {
if err != nil {
log.Output(2, "FATAL ERROR: "+err.Error())
os.Exit(1)
}
}
func HandleMinorError(err error) {
if err != nil {
log.Output(2, "MINOR ERROR: "+err.Error())
}
}
func HandleMinorErrorMsg(err error, msg string) {
if err != nil {
log.Output(2, "MINOR ERROR: "+msg+": "+err.Error())
}
}
// HandleFatalError receives an error, then logs and exits if not nil.
func HandleFatalError(err error) {
if err != nil {
log.Output(2, "FATAL ERROR: "+err.Error())
os.Exit(1)
}
}
func HandleFatalErrorMsg(err error, msg string) {
if err != nil {
log.Output(2, "FATAL ERROR: "+msg+": "+err.Error())
os.Exit(1)
}
}
func LogWarning(msg string) {
log.Output(2, "WARNING: "+msg)
}
func LogInfo(msg string) {
log.Output(2, "INFO: "+msg)
}
// SetRecvBufferSize sets the size of the receive buffer for the conn to the
// provided size in bytes.
func SetRecvBufferSize(conn *net.UDPConn, size int) {
err := conn.SetReadBuffer(size)
HandleError(err)
}