Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-workhorse
Commits
9e96ee75
Commit
9e96ee75
authored
Oct 04, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve readability of tests
parent
3f90655c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
14 deletions
+26
-14
README.md
README.md
+6
-0
internal/queueing/queue.go
internal/queueing/queue.go
+2
-5
internal/queueing/requests_test.go
internal/queueing/requests_test.go
+18
-9
No files found.
README.md
View file @
9e96ee75
...
@@ -13,6 +13,12 @@ gitlab-workhorse'][brief-history-blog].
...
@@ -13,6 +13,12 @@ gitlab-workhorse'][brief-history-blog].
gitlab-workhorse [OPTIONS]
gitlab-workhorse [OPTIONS]
Options:
Options:
-apiLimit uint
Number of API requests allowed at single time
-apiQueueDuration duration
Maximum queueing duration of requests (default 30s)
-apiQueueLimit uint
Number of API requests allowed to be queued
-authBackend string
-authBackend string
Authentication/authorization backend (default "http://localhost:8080")
Authentication/authorization backend (default "http://localhost:8080")
-authSocket string
-authSocket string
...
...
internal/queueing/queue.go
View file @
9e96ee75
...
@@ -23,7 +23,7 @@ type Queue struct {
...
@@ -23,7 +23,7 @@ type Queue struct {
func
NewQueue
(
limit
,
queueLimit
uint
)
*
Queue
{
func
NewQueue
(
limit
,
queueLimit
uint
)
*
Queue
{
return
&
Queue
{
return
&
Queue
{
busyCh
:
make
(
chan
struct
{},
limit
),
busyCh
:
make
(
chan
struct
{},
limit
),
waitingCh
:
make
(
chan
struct
{},
limit
+
queueLimit
),
waitingCh
:
make
(
chan
struct
{},
queueLimit
),
}
}
}
}
...
@@ -41,9 +41,7 @@ func (s *Queue) Acquire(timeout time.Duration) (err error) {
...
@@ -41,9 +41,7 @@ func (s *Queue) Acquire(timeout time.Duration) (err error) {
}
}
defer
func
()
{
defer
func
()
{
if
err
!=
nil
{
<-
s
.
waitingCh
<-
s
.
waitingCh
}
}()
}()
// fast path: push item to current processed items (non-blocking)
// fast path: push item to current processed items (non-blocking)
...
@@ -71,6 +69,5 @@ func (s *Queue) Acquire(timeout time.Duration) (err error) {
...
@@ -71,6 +69,5 @@ func (s *Queue) Acquire(timeout time.Duration) (err error) {
// It triggers next request to be processed if it's in queue
// It triggers next request to be processed if it's in queue
func
(
s
*
Queue
)
Release
()
{
func
(
s
*
Queue
)
Release
()
{
// dequeue from queue to allow next request to be processed
// dequeue from queue to allow next request to be processed
<-
s
.
waitingCh
<-
s
.
busyCh
<-
s
.
busyCh
}
}
internal/queueing/requests_test.go
View file @
9e96ee75
...
@@ -12,14 +12,14 @@ var httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)
...
@@ -12,14 +12,14 @@ var httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)
fmt
.
Fprintln
(
w
,
"OK"
)
fmt
.
Fprintln
(
w
,
"OK"
)
})
})
func
slowHttpHandler
(
clo
seCh
chan
struct
{})
http
.
Handler
{
func
pausedHttpHandler
(
pau
seCh
chan
struct
{})
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
<-
clo
seCh
<-
pau
seCh
fmt
.
Fprintln
(
w
,
"OK"
)
fmt
.
Fprintln
(
w
,
"OK"
)
})
})
}
}
func
Test
QueueRequests
(
t
*
testing
.
T
)
{
func
Test
NormalRequestProcessing
(
t
*
testing
.
T
)
{
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
h
:=
QueueRequests
(
httpHandler
,
1
,
1
,
time
.
Second
)
h
:=
QueueRequests
(
httpHandler
,
1
,
1
,
time
.
Second
)
h
.
ServeHTTP
(
w
,
nil
)
h
.
ServeHTTP
(
w
,
nil
)
...
@@ -28,28 +28,34 @@ func TestQueueRequests(t *testing.T) {
...
@@ -28,28 +28,34 @@ func TestQueueRequests(t *testing.T) {
}
}
}
}
// testSlowRequestProcessing creates a new queue,
// then it runs a number of requests that are going through queue,
// we return the response of first finished request,
// where status of request can be 200, 429 or 503
func
testSlowRequestProcessing
(
count
,
limit
,
queueLimit
uint
,
queueTimeout
time
.
Duration
)
*
httptest
.
ResponseRecorder
{
func
testSlowRequestProcessing
(
count
,
limit
,
queueLimit
uint
,
queueTimeout
time
.
Duration
)
*
httptest
.
ResponseRecorder
{
clo
seCh
:=
make
(
chan
struct
{})
pau
seCh
:=
make
(
chan
struct
{})
defer
close
(
clo
seCh
)
defer
close
(
pau
seCh
)
handler
:=
QueueRequests
(
slowHttpHandler
(
clo
seCh
),
limit
,
queueLimit
,
queueTimeout
)
handler
:=
QueueRequests
(
pausedHttpHandler
(
pau
seCh
),
limit
,
queueLimit
,
queueTimeout
)
respCh
:=
make
(
chan
*
httptest
.
ResponseRecorder
,
count
)
respCh
:=
make
(
chan
*
httptest
.
ResponseRecorder
,
count
)
// queue requests to use up the queue
// queue requests to use up the queue
for
count
>
0
{
for
i
:=
0
;
i
<
count
;
i
++
{
go
func
()
{
go
func
()
{
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
nil
)
handler
.
ServeHTTP
(
w
,
nil
)
respCh
<-
w
respCh
<-
w
}()
}()
count
--
}
}
// dequeue first request
// dequeue first request
return
<-
respCh
return
<-
respCh
}
}
// TestQueueingTimeout performs 2 requests
// the queue limit and length is 1,
// the second request gets timed-out
func
TestQueueingTimeout
(
t
*
testing
.
T
)
{
func
TestQueueingTimeout
(
t
*
testing
.
T
)
{
w
:=
testSlowRequestProcessing
(
2
,
1
,
1
,
time
.
Microsecond
)
w
:=
testSlowRequestProcessing
(
2
,
1
,
1
,
time
.
Microsecond
)
...
@@ -58,7 +64,10 @@ func TestQueueingTimeout(t *testing.T) {
...
@@ -58,7 +64,10 @@ func TestQueueingTimeout(t *testing.T) {
}
}
}
}
func
TestQueuedRequests
(
t
*
testing
.
T
)
{
// TestQueueingTooManyRequests performs 3 requests
// the queue limit and length is 1,
// so the third request has to be rejected with 429
func
TestQueueingTooManyRequests
(
t
*
testing
.
T
)
{
w
:=
testSlowRequestProcessing
(
3
,
1
,
1
,
time
.
Minute
)
w
:=
testSlowRequestProcessing
(
3
,
1
,
1
,
time
.
Minute
)
if
w
.
Code
!=
429
{
if
w
.
Code
!=
429
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment