-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/github_actions/google-github-acti…
…ons/setup-gcloud-1
- Loading branch information
Showing
17 changed files
with
581 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gtask | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func DefaultPool() *Pool { | ||
return pool | ||
} | ||
|
||
var ( | ||
// pool is the global task pool. | ||
pool = NewPool() | ||
) | ||
|
||
type taskFunc func(ctx context.Context) | ||
|
||
// Pool is the task pool for managing some async jobs. | ||
type Pool struct { | ||
stopCh chan struct{} | ||
wg sync.WaitGroup | ||
lock sync.Mutex | ||
tasks []*task | ||
} | ||
|
||
func NewPool() *Pool { | ||
return &Pool{ | ||
stopCh: make(chan struct{}), | ||
} | ||
} | ||
|
||
type task struct { | ||
fn taskFunc | ||
interval time.Duration | ||
} | ||
|
||
func (p *Pool) AddTask(fn taskFunc, interval time.Duration) { | ||
t := &task{ | ||
fn: fn, | ||
interval: interval, | ||
} | ||
|
||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
p.tasks = append(p.tasks, t) | ||
} | ||
|
||
func (p *Pool) Start(ctx context.Context) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
|
||
for _, task := range p.tasks { | ||
p.wg.Add(1) | ||
go p.doTask(ctx, task) | ||
} | ||
} | ||
|
||
func (p *Pool) doTask(ctx context.Context, task *task) { | ||
defer p.wg.Done() | ||
for { | ||
select { | ||
// wait for stop signal | ||
case <-ctx.Done(): | ||
return | ||
case <-p.stopCh: | ||
return | ||
default: | ||
task.fn(ctx) | ||
// interval is 0 means it's a one time job, return directly | ||
if task.interval == 0 { | ||
return | ||
} | ||
time.Sleep(task.interval) | ||
} | ||
} | ||
} | ||
|
||
func (p *Pool) Stop() { | ||
close(p.stopCh) | ||
p.wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gtask | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAddTask(t *testing.T) { | ||
pool := NewPool() | ||
|
||
taskNum := 3 | ||
taskInterval := time.Duration(0) | ||
for i := 0; i < taskNum; i++ { | ||
fn := func(ctx context.Context) { | ||
t.Logf("Task %d is running...", i) | ||
} | ||
|
||
pool.AddTask(fn, taskInterval) | ||
} | ||
|
||
if len(pool.tasks) != taskNum { | ||
t.Errorf("Expected %d tasks but found %d", taskNum, len(pool.tasks)) | ||
} | ||
} | ||
|
||
func TestStartAndStop(t *testing.T) { | ||
// test normal case | ||
{ | ||
pool := NewPool() | ||
// create channel with buffer | ||
ch1 := make(chan struct{}, 5) | ||
ch2 := make(chan struct{}, 5) | ||
// test one-time job | ||
t1 := &task{ | ||
interval: 0, | ||
fn: func(ctx context.Context) { | ||
ch1 <- struct{}{} | ||
}, | ||
} | ||
// test interval job | ||
t2 := &task{ | ||
interval: 100 * time.Millisecond, | ||
fn: func(ctx context.Context) { | ||
ch2 <- struct{}{} | ||
}, | ||
} | ||
|
||
pool.tasks = []*task{t1, t2} | ||
|
||
ctx1, cancel1 := context.WithCancel(context.Background()) | ||
defer cancel1() | ||
pool.Start(ctx1) | ||
|
||
// Let it run for a bit | ||
time.Sleep(300 * time.Millisecond) | ||
// ch1 should only have one element as it's a one time job | ||
assert.Equal(t, 1, len(ch1)) | ||
// ch2 should have elements over 2 as sleep 300ms and interval is 100ms | ||
assert.Greater(t, len(ch2), 2) | ||
pool.Stop() | ||
close(ch1) | ||
close(ch2) | ||
} | ||
|
||
// test context timeout case | ||
{ | ||
pool := NewPool() | ||
ch1 := make(chan struct{}, 2) | ||
t1 := &task{ | ||
interval: 100 * time.Millisecond, | ||
fn: func(ctx context.Context) { | ||
ch1 <- struct{}{} | ||
}, | ||
} | ||
|
||
pool.tasks = []*task{t1} | ||
ctx1, cancel1 := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
defer cancel1() | ||
pool.Start(ctx1) | ||
// Let it run for a bit | ||
time.Sleep(200 * time.Millisecond) | ||
assert.Equal(t, 1, len(ch1)) | ||
pool.Stop() | ||
close(ch1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package lib | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// ShuffleStringSlice shuffles the string slice in place. | ||
func ShuffleStringSlice(slice []string) { | ||
rd := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
rd.Shuffle(len(slice), func(i, j int) { | ||
slice[i], slice[j] = slice[j], slice[i] | ||
}) | ||
} |
Oops, something went wrong.