forked from jaracil/nexus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
123 lines (112 loc) · 3.05 KB
/
sync.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
package main
import (
"github.com/jaracil/ei"
r "gopkg.in/rethinkdb/rethinkdb-go.v3"
)
func (nc *NexusConn) handleSyncReq(req *JsonRpcReq) {
switch req.Method {
case "sync.lock":
lock, err := ei.N(req.Params).M("lock").Lower().F(checkRegexp, _prefixRegexp).F(checkNotEmptyLabels).String()
if err != nil {
req.Error(ErrInvalidParams, "lock", nil)
return
}
tags := nc.getTags(lock)
if !(ei.N(tags).M("@"+req.Method).BoolZ() || ei.N(tags).M("@admin").BoolZ()) {
req.Error(ErrPermissionDenied, "", nil)
return
}
res, err := r.Table("locks").
Insert(ei.M{"id": lock, "owner": nc.connId}).
RunWrite(db, r.RunOpts{Durability: "hard"})
if err != nil {
if r.IsConflictErr(err) {
req.Error(ErrLockNotOwned, "", nil)
} else {
req.Error(ErrInternal, "", nil)
}
return
}
if res.Inserted <= 0 {
req.Error(ErrLockNotOwned, "", nil)
return
}
req.Result(ei.M{"ok": true})
case "sync.unlock":
lock, err := ei.N(req.Params).M("lock").Lower().F(checkRegexp, _prefixRegexp).F(checkNotEmptyLabels).String()
if err != nil {
req.Error(ErrInvalidParams, "lock", nil)
return
}
tags := nc.getTags(lock)
if !(ei.N(tags).M("@"+req.Method).BoolZ() || ei.N(tags).M("@admin").BoolZ()) {
req.Error(ErrPermissionDenied, "", nil)
return
}
res, err := r.Table("locks").
GetAll(lock).
Filter(r.Row.Field("owner").Eq(nc.connId)).
Delete().
RunWrite(db, r.RunOpts{Durability: "hard"})
if err != nil {
req.Error(ErrInternal, err.Error(), nil)
return
}
if res.Deleted <= 0 {
req.Error(ErrLockNotOwned, "", nil)
return
}
req.Result(ei.M{"ok": true})
case "sync.list":
prefix, depth, filter, limit, skip := getListParams(req.Params)
tags := nc.getTags(prefix)
if !(ei.N(tags).M("@sync.list").BoolZ() || ei.N(tags).M("@admin").BoolZ()) {
req.Error(ErrPermissionDenied, "", nil)
return
}
term := getListTerm("locks", "", "id", prefix, depth, filter, limit, skip).
Pluck("id", "owner")
cur, err := term.Run(db)
defer cur.Close()
if err != nil {
req.Error(ErrInternal, err.Error(), nil)
return
}
var all []interface{}
if err := cur.All(&all); err != nil {
req.Error(ErrInternal, "", nil)
return
}
req.Result(all)
case "sync.count":
prefix := getPrefixParam(req.Params)
filter := ei.N(req.Params).M("filter").StringZ()
countSubprefixes := ei.N(req.Params).M("subprefixes").BoolZ()
tags := nc.getTags(prefix)
if !(ei.N(tags).M("@sync.count").BoolZ() || ei.N(tags).M("@admin").BoolZ()) {
req.Error(ErrPermissionDenied, "", nil)
return
}
term := getCountTerm("locks", "", "id", prefix, filter, countSubprefixes)
cur, err := term.Run(db)
defer cur.Close()
if err != nil {
req.Error(ErrInternal, err.Error(), nil)
return
}
var all []interface{}
if err := cur.All(&all); err != nil {
req.Error(ErrInternal, "", nil)
return
}
if countSubprefixes {
req.Result(all)
} else if len(all) > 0 {
req.Result(ei.M{"count": all[0]})
} else {
req.Result(ei.M{"count": 0})
}
default:
req.Error(ErrMethodNotFound, "", nil)
}
}