-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the performance when sending on macOS
- Loading branch information
1 parent
cb262e1
commit 18ff216
Showing
11 changed files
with
323 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Build | ||
# Need GitHub secret: DOCKER_HUB_USER, DOCKER_HUB_SECRETS, GHCR_TOKEN | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
UnitTest: | ||
name: Test | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Set up Go 1.19 | ||
uses: actions/setup-go@v2.1.3 | ||
with: | ||
go-version: 1.19 | ||
id: go | ||
- name: Check out code into the Go module directory | ||
uses: actions/checkout@v2.3.4 | ||
- name: Test | ||
run: | | ||
make test | ||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v1 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
files: coverage.out | ||
flags: unittests | ||
name: codecov-umbrella | ||
fail_ci_if_error: true |
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,28 @@ | ||
package main | ||
|
||
import "sync" | ||
|
||
type Queue struct { | ||
sync.Mutex | ||
data []int | ||
} | ||
|
||
func (q *Queue) Push(value int) { | ||
q.Lock() | ||
defer q.Unlock() | ||
q.data = append(q.data, value) | ||
} | ||
|
||
func (q *Queue) Pop() (item *int) { | ||
q.Lock() | ||
defer q.Unlock() | ||
if len(q.data) > 0 { | ||
item = &q.data[0] | ||
q.data = q.data[1:] | ||
} | ||
return | ||
} | ||
|
||
func (q *Queue) Size() int { | ||
return len(q.data) | ||
} |
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestQueue(t *testing.T) { | ||
queue := &Queue{} | ||
|
||
val := queue.Pop() | ||
assert.Nil(t, val) | ||
|
||
queue.Push(1) | ||
val = queue.Pop() | ||
assert.NotNil(t, val) | ||
assert.Equal(t, 1, *val) | ||
|
||
// multi-threads case | ||
wg := sync.WaitGroup{} | ||
threadCounts := 1000 | ||
for i := 0; i < threadCounts; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
queue.Push(1) | ||
}() | ||
} | ||
wg.Wait() | ||
assert.Equal(t, threadCounts, queue.Size()) | ||
|
||
// Pop case | ||
for i := 0; i < threadCounts; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
queue.Pop() | ||
}() | ||
} | ||
wg.Wait() | ||
assert.Equal(t, 0, queue.Size()) | ||
} |
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,79 @@ | ||
package main | ||
|
||
import "sync" | ||
|
||
type SafeMap struct { | ||
sync.Mutex | ||
data map[int]string | ||
} | ||
|
||
func NewSafeMap(count int) (safeMap *SafeMap) { | ||
safeMap = &SafeMap{data: make(map[int]string, count)} | ||
for i := 0; i < count; i++ { | ||
safeMap.Put(i, "") | ||
} | ||
return | ||
} | ||
|
||
func (m *SafeMap) Put(k int, val string) { | ||
m.Lock() | ||
defer m.Unlock() | ||
m.data[k] = val | ||
} | ||
|
||
func (m *SafeMap) Remove(k int) { | ||
m.Lock() | ||
defer m.Unlock() | ||
delete(m.data, k) | ||
} | ||
|
||
func (m *SafeMap) Get() *int { | ||
m.Lock() | ||
defer m.Unlock() | ||
for k, _ := range m.data { | ||
return &k | ||
} | ||
return nil | ||
} | ||
|
||
func (m *SafeMap) GetKeys() (result []int) { | ||
m.Lock() | ||
defer m.Unlock() | ||
for k, _ := range m.data { | ||
result = append(result, k) | ||
} | ||
return | ||
} | ||
|
||
func (m *SafeMap) GetAndRemove() (target *int) { | ||
m.Lock() | ||
defer m.Unlock() | ||
for k, _ := range m.data { | ||
target = &k | ||
break | ||
} | ||
if target != nil { | ||
delete(m.data, *target) | ||
} | ||
return | ||
} | ||
|
||
func (m *SafeMap) GetLowestAndRemove() (target *int) { | ||
m.Lock() | ||
defer m.Unlock() | ||
for k := range m.data { | ||
if target == nil || k < *target { | ||
target = &k | ||
} | ||
} | ||
if target != nil { | ||
delete(m.data, *target) | ||
} | ||
return | ||
} | ||
|
||
func (m *SafeMap) Size() int { | ||
m.Lock() | ||
defer m.Unlock() | ||
return len(m.data) | ||
} |
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,35 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestSafeMap(t *testing.T) { | ||
safeMap := NewSafeMap(1000) | ||
assert.NotNil(t, safeMap) | ||
assert.Equal(t, 1000, safeMap.Size()) | ||
|
||
wg := sync.WaitGroup{} | ||
for i := 0; i < 500; i++ { | ||
wg.Add(1) | ||
go func(k int) { | ||
defer wg.Done() | ||
|
||
safeMap.Remove(k) | ||
}(i) | ||
} | ||
wg.Wait() | ||
assert.Equal(t, 500, safeMap.Size()) | ||
|
||
for i := 0; i < 500; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
_ = safeMap.GetAndRemove() | ||
}() | ||
} | ||
wg.Wait() | ||
assert.Equal(t, 0, safeMap.Size()) | ||
} |
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
Oops, something went wrong.