-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
96 lines (83 loc) · 1.93 KB
/
job.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
package replicator
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"time"
"github.com/goydb/replicator/client"
)
type Job struct {
ID string `json:"_id"`
Rev string `json:"_rev"`
UserCtx UserCtx `json:"user_ctx"`
Source *client.Remote `json:"source"`
Target *client.Remote `json:"target"`
CreateTarget bool `json:"create_target"`
Continuous bool `json:"continuous"`
Owner string `json:"owner"`
Config
}
type Config struct {
// Heartbeat For Continuous Replication the heartbeat parameter defines the heartbeat period in milliseconds. The RECOMMENDED value by default is 10000 (10 seconds).
Heartbeat time.Duration
}
func (c Config) HeartbeatOrFallback() time.Duration {
if c.Heartbeat == 0 {
return time.Second * 10
}
return c.Heartbeat
}
// GenerateReplicationID generates a replication id
// using the given name, name could be a hostame.
// https://docs.couchdb.org/en/stable/replication/protocol.html#generate-replication-id
func (j *Job) GenerateReplicationID(name string) string {
hash := sha256.New()
b := bufio.NewWriter(hash)
_, err := b.WriteString(name)
if err != nil {
panic(err)
}
_, err = b.WriteString("|")
if err != nil {
panic(err)
}
j.Source.GenerateReplicationID(b)
_, err = b.WriteString("|")
if err != nil {
panic(err)
}
j.Target.GenerateReplicationID(b)
_, err = b.WriteString("|")
if err != nil {
panic(err)
}
if j.CreateTarget {
_, err = b.WriteString("T")
if err != nil {
panic(err)
}
} else {
_, err = b.WriteString("F")
if err != nil {
panic(err)
}
}
if j.Continuous {
_, err = b.WriteString("T")
if err != nil {
panic(err)
}
} else {
_, err = b.WriteString("F")
if err != nil {
panic(err)
}
}
b.Flush()
final := hash.Sum(nil)
return hex.EncodeToString(final)
}
type UserCtx struct {
Name string `json:"name"`
Roles []string `json:"roles"`
}