@@ -12,9 +12,7 @@ import (
12
12
"golang.org/x/sync/semaphore"
13
13
)
14
14
15
- var updatesSemaphore = semaphore .NewWeighted (int64 (256 ))
16
-
17
- func handleUpdate (registry sources.Registry , fn func (source string , body io.Reader ) error ) http.HandlerFunc {
15
+ func handleUpdate (registry sources.Registry , fn func (source string , body io.Reader ) error , sem * semaphore.Weighted ) http.HandlerFunc {
18
16
return func (w http.ResponseWriter , r * http.Request ) {
19
17
ok , source , err := IsTokenValid (registry , r )
20
18
if err != nil {
@@ -28,12 +26,12 @@ func handleUpdate(registry sources.Registry, fn func(source string, body io.Read
28
26
}
29
27
30
28
{
31
- ok = updatesSemaphore .TryAcquire (1 )
29
+ ok = sem .TryAcquire (1 )
32
30
if ! ok {
33
31
ReplyWithTooManyRequests (w )
34
32
return
35
33
}
36
- defer updatesSemaphore .Release (1 )
34
+ defer sem .Release (1 )
37
35
38
36
if err = fn (source , r .Body ); err != nil {
39
37
ReplyWithInternalError (w , err )
@@ -50,7 +48,7 @@ func handleUpdate(registry sources.Registry, fn func(source string, body io.Read
50
48
}
51
49
52
50
// PutSchema upsert an asset into the graph of the data source
53
- func PutSchema (registry sources.Registry , graphUpdater * knowledge.GraphUpdater ) http.HandlerFunc {
51
+ func PutSchema (registry sources.Registry , graphUpdater * knowledge.GraphUpdater , sem * semaphore. Weighted ) http.HandlerFunc {
54
52
return handleUpdate (registry , func (source string , body io.Reader ) error {
55
53
requestBody := client.PutGraphSchemaRequestBody {}
56
54
if err := json .NewDecoder (body ).Decode (& requestBody ); err != nil {
@@ -60,11 +58,11 @@ func PutSchema(registry sources.Registry, graphUpdater *knowledge.GraphUpdater)
60
58
// TODO(c.michaud): verify compatibility of the schema with graph updates
61
59
graphUpdater .UpdateSchema (source , requestBody .Schema )
62
60
return nil
63
- })
61
+ }, sem )
64
62
}
65
63
66
64
// PutAsset upsert an asset into the graph of the data source
67
- func PutAsset (registry sources.Registry , graphUpdater * knowledge.GraphUpdater ) http.HandlerFunc {
65
+ func PutAsset (registry sources.Registry , graphUpdater * knowledge.GraphUpdater , sem * semaphore. Weighted ) http.HandlerFunc {
68
66
return handleUpdate (registry , func (source string , body io.Reader ) error {
69
67
requestBody := client.PutGraphAssetRequestBody {}
70
68
if err := json .NewDecoder (body ).Decode (& requestBody ); err != nil {
@@ -74,11 +72,11 @@ func PutAsset(registry sources.Registry, graphUpdater *knowledge.GraphUpdater) h
74
72
// TODO(c.michaud): verify compatibility of the schema with graph updates
75
73
graphUpdater .UpsertAsset (source , requestBody .Asset )
76
74
return nil
77
- })
75
+ }, sem )
78
76
}
79
77
80
78
// PutRelation upsert a relation into the graph of the data source
81
- func PutRelation (registry sources.Registry , graphUpdater * knowledge.GraphUpdater ) http.HandlerFunc {
79
+ func PutRelation (registry sources.Registry , graphUpdater * knowledge.GraphUpdater , sem * semaphore. Weighted ) http.HandlerFunc {
82
80
return handleUpdate (registry , func (source string , body io.Reader ) error {
83
81
requestBody := client.PutGraphRelationRequestBody {}
84
82
if err := json .NewDecoder (body ).Decode (& requestBody ); err != nil {
@@ -88,11 +86,11 @@ func PutRelation(registry sources.Registry, graphUpdater *knowledge.GraphUpdater
88
86
// TODO(c.michaud): verify compatibility of the schema with graph updates
89
87
graphUpdater .UpsertRelation (source , requestBody .Relation )
90
88
return nil
91
- })
89
+ }, sem )
92
90
}
93
91
94
92
// DeleteAsset delete an asset from the graph of the data source
95
- func DeleteAsset (registry sources.Registry , graphUpdater * knowledge.GraphUpdater ) http.HandlerFunc {
93
+ func DeleteAsset (registry sources.Registry , graphUpdater * knowledge.GraphUpdater , sem * semaphore. Weighted ) http.HandlerFunc {
96
94
return handleUpdate (registry , func (source string , body io.Reader ) error {
97
95
requestBody := client.DeleteGraphAssetRequestBody {}
98
96
if err := json .NewDecoder (body ).Decode (& requestBody ); err != nil {
@@ -102,11 +100,11 @@ func DeleteAsset(registry sources.Registry, graphUpdater *knowledge.GraphUpdater
102
100
// TODO(c.michaud): verify compatibility of the schema with graph updates
103
101
graphUpdater .RemoveAsset (source , requestBody .Asset )
104
102
return nil
105
- })
103
+ }, sem )
106
104
}
107
105
108
106
// DeleteRelation upsert a relation into the graph of the data source
109
- func DeleteRelation (registry sources.Registry , graphUpdater * knowledge.GraphUpdater ) http.HandlerFunc {
107
+ func DeleteRelation (registry sources.Registry , graphUpdater * knowledge.GraphUpdater , sem * semaphore. Weighted ) http.HandlerFunc {
110
108
return handleUpdate (registry , func (source string , body io.Reader ) error {
111
109
requestBody := client.DeleteGraphRelationRequestBody {}
112
110
if err := json .NewDecoder (body ).Decode (& requestBody ); err != nil {
@@ -116,5 +114,5 @@ func DeleteRelation(registry sources.Registry, graphUpdater *knowledge.GraphUpda
116
114
// TODO(c.michaud): verify compatibility of the schema with graph updates
117
115
graphUpdater .RemoveRelation (source , requestBody .Relation )
118
116
return nil
119
- })
117
+ }, sem )
120
118
}
0 commit comments