Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
0de12368
Commit
0de12368
authored
Jan 26, 2024
by
Michael Yang
Browse files
add new LimitGroup for dynamic concurrency
parent
917bd610
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
2 deletions
+40
-2
server/download.go
server/download.go
+40
-2
No files found.
server/download.go
View file @
0de12368
...
...
@@ -20,6 +20,7 @@ import (
"time"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
"github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/format"
...
...
@@ -150,8 +151,7 @@ func (b *blobDownload) Run(ctx context.Context, requestURL *url.URL, opts *regis
_
=
file
.
Truncate
(
b
.
Total
)
g
,
inner
:=
errgroup
.
WithContext
(
ctx
)
g
.
SetLimit
(
numDownloadParts
)
g
,
inner
:=
NewLimitGroup
(
ctx
,
numDownloadParts
)
for
i
:=
range
b
.
Parts
{
part
:=
b
.
Parts
[
i
]
if
part
.
Completed
==
part
.
Size
{
...
...
@@ -378,3 +378,41 @@ func downloadBlob(ctx context.Context, opts downloadOpts) error {
return
download
.
Wait
(
ctx
,
opts
.
fn
)
}
type
LimitGroup
struct
{
*
errgroup
.
Group
context
.
Context
Semaphore
*
semaphore
.
Weighted
weight
,
max_weight
int64
}
func
NewLimitGroup
(
ctx
context
.
Context
,
n
int64
)
(
*
LimitGroup
,
context
.
Context
)
{
g
,
ctx
:=
errgroup
.
WithContext
(
ctx
)
return
&
LimitGroup
{
Group
:
g
,
Context
:
ctx
,
Semaphore
:
semaphore
.
NewWeighted
(
n
),
weight
:
n
,
max_weight
:
n
,
},
ctx
}
func
(
g
*
LimitGroup
)
Go
(
fn
func
()
error
)
{
weight
:=
g
.
weight
g
.
Semaphore
.
Acquire
(
g
.
Context
,
weight
)
if
g
.
Context
.
Err
()
!=
nil
{
return
}
g
.
Group
.
Go
(
func
()
error
{
defer
g
.
Semaphore
.
Release
(
weight
)
return
fn
()
})
}
func
(
g
*
LimitGroup
)
SetLimit
(
n
int64
)
{
if
n
>
0
{
g
.
weight
=
g
.
max_weight
/
n
}
}
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