Skip to content

Commit

Permalink
处理 ctrl+c 信号
Browse files Browse the repository at this point in the history
  • Loading branch information
link1st committed Apr 25, 2024
1 parent e713a0f commit 9a5fb73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"

"github.com/link1st/go-stress-testing/model"
Expand All @@ -21,7 +24,7 @@ func (a *array) String() string {
return fmt.Sprint(*a)
}

// Set set
// Set arr set
func (a *array) Set(s string) error {
*a = append(*a, s)

Expand Down Expand Up @@ -97,6 +100,15 @@ func main() {
fmt.Printf(" deadline %s", deadline)
}
}

// 处理 ctrl+c 信号
ctx, cancelFunc := context.WithCancel(ctx)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
<-c
cancelFunc()
}()
server.Dispose(ctx, concurrency, totalNumber, request)
return
}
3 changes: 1 addition & 2 deletions server/golink/http_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package golink
import (
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"sync"
Expand All @@ -22,7 +21,7 @@ func HTTP(ctx context.Context, chanID uint64, ch chan<- *model.RequestResults, t
// fmt.Printf("启动协程 编号:%05d \n", chanID)
for i := uint64(0); i < totalNumber; i++ {
if ctx.Err() != nil {
fmt.Printf("ctx.Err err: %v \n", ctx.Err())
// fmt.Printf("ctx.Err err: %v \n", ctx.Err())
break
}

Expand Down

0 comments on commit 9a5fb73

Please sign in to comment.