This commit is contained in:
xing 2022-12-07 13:26:52 +08:00
parent cb2de22b13
commit 309c660187
6 changed files with 126 additions and 8 deletions

View File

@ -1,10 +1,10 @@
FROM golang:latest
FROM golang:latest as gobulidIso
COPY ./ /go/src/wp-go
WORKDIR /go/src/wp-go
#ENV GOPROXY="https://goproxy.cn"
RUN go build -tags netgo
ENV GOPROXY="https://goproxy.cn"
RUN go build -ldflags "-w" -tags netgo
FROM alpine:latest
WORKDIR /opt/wp-go
COPY --from=0 /go/src/wp-go/wp-go ./
COPY --from=gobulidIso /go/src/wp-go/wp-go ./
ENTRYPOINT ["./wp-go"]

4
cache/map.go vendored
View File

@ -168,7 +168,7 @@ func (m *MapCache[K, V]) GetCache(c context.Context, key K, timeout time.Duratio
if timeout > 0 {
ctx, cancel := context.WithTimeout(c, timeout)
defer cancel()
done := make(chan struct{})
done := make(chan struct{}, 1)
go func() {
call()
done <- struct{}{}
@ -230,7 +230,7 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.
if timeout > 0 {
ctx, cancel := context.WithTimeout(c, timeout)
defer cancel()
done := make(chan struct{})
done := make(chan struct{}, 1)
go func() {
call()
done <- struct{}{}

2
cache/slice.go vendored
View File

@ -72,7 +72,7 @@ func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, par
if timeout > 0 {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
done := make(chan struct{})
done := make(chan struct{}, 1)
go func() {
call()
done <- struct{}{}

View File

@ -353,7 +353,6 @@ func Slice[T any](arr []T, offset, length int) (r []T) {
}
func Comb[T any](arr []T, m int) (r [][]T) {
r = make([][]T, 0)
if m == 1 {
for _, t := range arr {
r = append(r, []T{t})

76
taskPools/pools.go Normal file
View File

@ -0,0 +1,76 @@
package taskPools
import (
"context"
"log"
"sync"
"time"
)
type Pools struct {
ch chan struct{}
wg *sync.WaitGroup
}
func NewPools(n int) *Pools {
if n <= 0 {
panic("n must >= 1")
}
c := make(chan struct{}, n)
for i := 0; i < n; i++ {
c <- struct{}{}
}
return &Pools{
ch: c,
wg: &sync.WaitGroup{},
}
}
func (p *Pools) ExecuteWithTimeOut(timeout time.Duration, fn func(), args ...string) {
if timeout <= 0 {
p.Execute(fn)
return
}
p.wg.Add(1)
q := <-p.ch
go func() {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
done := make(chan struct{}, 1)
defer func() {
cancel()
p.wg.Done()
p.ch <- q
}()
go func() {
fn()
done <- struct{}{}
}()
select {
case <-ctx.Done():
if len(args) > 0 && args[0] != "" {
log.Printf("执行%s超时", args[0])
}
case <-done:
}
}()
}
func (p *Pools) Execute(fn func()) {
if cap(p.ch) == 1 {
fn()
return
}
p.wg.Add(1)
q := <-p.ch
go func() {
defer func() {
p.wg.Done()
p.ch <- q
}()
fn()
}()
}
func (p *Pools) Wait() {
p.wg.Wait()
}

43
taskPools/pools_test.go Normal file
View File

@ -0,0 +1,43 @@
package taskPools
import (
"fmt"
"log"
"testing"
"time"
)
func TestPools_Execute(t *testing.T) {
t.Run("pools", func(t *testing.T) {
p := NewPools(3)
for i := 0; i < 10; i++ {
i := i
p.Execute(func() {
time.Sleep(time.Second)
log.Printf("task[%d] done", i)
})
}
p.Wait()
})
}
func TestPools_ExecuteWithTimeOut(t *testing.T) {
t.Run("timeout test", func(t *testing.T) {
p := NewPools(3)
for i := 0; i < 10; i++ {
i := i
p.ExecuteWithTimeOut(2*time.Second, func() {
log.Printf("start task[%d]", i)
tt := time.Second
if i == 0 {
tt = 7 * time.Second
}
time.Sleep(tt)
log.Printf("task[%d] done", i)
}, fmt.Sprintf("task [%d]", i))
}
p.Wait()
})
}