Skip to content

Commit

Permalink
修正异步 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Hentioe committed May 3, 2018
1 parent 3347d60 commit 6f14f6d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 22 deletions.
40 changes: 38 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
# go-tests = true
# unused-packages = true

[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/gin-gonic/gin"
branch = "master"

[prune]
go-tests = true
unused-packages = true
[[constraint]]
name = "github.com/go-resty/resty"
version = "1.4.0"
25 changes: 25 additions & 0 deletions concurrency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"strconv"
"errors"
"fmt"
"testing"

"github.com/go-resty/resty"
)

func TestConcurrency(t *testing.T) {
for i :=16; i > 0; i-- {
resp, err := resty.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"sentences":["` + strconv.Itoa(i) + `第一句","第二句","第三句","第四句","第五句","第六句","第七句","第八句","第九句"]}`).
Post("http://localhost:8080/task/generate/sorry")
if err != nil {
t.Error(err)
}
if resp.StatusCode() != 200 {
t.Error(errors.New(fmt.Sprintf("http err status: %d", resp.StatusCode())))
}
}
}
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"runtime"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"log"
"fmt"
"flag"
"runtime"

"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -40,6 +41,8 @@ func main() {
}
gin.SetMode(*mode)
server := Server{router: gin.Default(), bind: *bind}
go asyncMakeAction()
for i := 0; i < *cl; i++ {
go asyncMakeAction()
}
log.Fatal(server.Run())
}
2 changes: 1 addition & 1 deletion runner.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ running_args: -mode debug
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html
no_rebuild_ext: .tpl, .tmpl, .html
ignored: assets, tmp
ignored: assets, tmp, dist
build_delay: 600
colors: 1
log_color_main: cyan
Expand Down
24 changes: 13 additions & 11 deletions tasks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"runtime"
"sync"
)

Expand All @@ -23,11 +22,13 @@ var updateTaskStateMutex sync.Mutex
var loadTaskStateMutex sync.Mutex

// 执行任务的缓冲通道
var taskChan = make(chan Task, runtime.NumCPU())
var taskChan = make(chan Task, *cl)

// 储存任务状态的 map
var taskState = make(map[string]string)

var wg sync.WaitGroup

// Task 添加到队列的任务结构体
type Task struct {
TplKey string
Expand All @@ -40,7 +41,9 @@ type makeFunc func(string, Subs) (string, error)

// addMakeTask 添加一个生成任务
func addMakeTask(task Task) string {
taskChan <- task
go func() {
taskChan <- task
}()
hash := task.Subs.Hash(task.TplKey)
updateTaskState(hash, StateWaiting)
return hash
Expand Down Expand Up @@ -75,18 +78,17 @@ func loadTaskState(hash string) (state string) {
// asyncMakeAction 异步生成任务启动
// goroutine 函数
func asyncMakeAction() {
next:
for {
task := <-taskChan
var curTaskHash string
var err error
for _, f := range task.RunnableList {
if hash, err := f(task.TplKey, task.Subs); err != nil {
updateTaskState(hash, StateError)
continue next
}else{
curTaskHash = hash
}
curTaskHash, err = f(task.TplKey, task.Subs)
}
if err != nil {
updateTaskState(curTaskHash, StateError)
} else {
updateTaskState(curTaskHash, StateCompleted)
}
updateTaskState(curTaskHash, StateCompleted)
}
}

0 comments on commit 6f14f6d

Please sign in to comment.