Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close keep-alived connections #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions bench/checker/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ const (
)

var (
targetHost string
getTimeout = 15 * time.Second
targetHost string
getTimeout = 15 * time.Second
postTimeout = 3 * time.Second
)

type Session struct {
Client *http.Client
Transport *http.Transport

logger *log.Logger
logger *log.Logger
}

var RedirectAttemptedError = fmt.Errorf("redirect attempted")
Expand All @@ -48,8 +47,8 @@ func NewSession() *Session {
Transport: w.Transport,
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return RedirectAttemptedError
},
return RedirectAttemptedError
},
}
return w
}
Expand Down Expand Up @@ -161,13 +160,17 @@ func (s *Session) NewFileUploadRequest(uri string, params map[string]string, par

func (s *Session) RefreshClient() {
jar, _ := cookiejar.New(&cookiejar.Options{})
s.Transport = &http.Transport{}
s.Transport.CloseIdleConnections()
s.Client = &http.Client{
Transport: s.Transport,
Jar: jar,
}
}

func (s *Session) CloseConns() {
s.Transport.CloseIdleConnections()
}

func (s *Session) SendRequest(req *http.Request) (*http.Response, error) {
s.Client.Timeout = getTimeout
if req.Method == http.MethodPost {
Expand Down
10 changes: 7 additions & 3 deletions bench/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func (sr *scenarioRunner) start(ch chan *scenarioRunner) {
}
func (sr *scenarioRunner) run() {
for _, fn := range sr.scenarios {
fn(checker.NewSession(), sr.bdata)
s := checker.NewSession()
fn(s, sr.bdata)
s.CloseConns()
}
sr.done <- struct{}{}
}
Expand Down Expand Up @@ -183,7 +185,9 @@ func start(bdata *benchdata) bool {
for _, fn := range initialScenarios {
go func(fn scenarioFn) {
defer wg.Done()
fn(checker.NewSession(), bdata)
s := checker.NewSession()
fn(s, bdata)
s.CloseConns()
}(fn)
}
wg.Wait()
Expand All @@ -193,7 +197,7 @@ func start(bdata *benchdata) bool {
score.GetInstance().SetFails(0)
score.GetFailErrorsInstance().Append(fmt.Errorf("初期チェックがタイムアウトしました"))
}
if (score.GetInstance().GetFails() - score.GetInstance().GetTimeouts()) > 0 {
if (score.GetInstance().GetFails() - score.GetInstance().GetTimeouts()) > 0 {
return false
}
log.Println("pre-check finished and start main benchmarking")
Expand Down