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

Allow setting different responses per request #31 #102

Closed
wants to merge 17 commits into from
Closed
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions internal/server/http/response_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package http

import "math/rand"
import (
"math/rand"
"sync"
)

// ResponseMode represents random/burst mode for the response
type ResponseMode int
Expand All @@ -13,6 +16,8 @@ type ResponseHandler struct {
counter int // to keep count of served requests (wrapping after totalResp)
currentInd int // index/key of current response in scheduleMap
scheduleMap []int // prefix array of repeating request

mutex *sync.Mutex
}

const (
Expand Down Expand Up @@ -54,14 +59,20 @@ func (rh *ResponseHandler) fillDefaults(imposter *Imposter) {
rh.counter = 1
rh.currentInd = 0
}

rh.mutex = new(sync.Mutex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, there's no need to make the rh.mutex a pointer neither to initialize it since rh is already a pointer to a ResponseHandler. See this example from the Go Tour. Thanks!

}

// GetIndex is responsible for getting index for current request
func (rh *ResponseHandler) GetIndex() int {
if rh.Mode == RandomMode {
return rh.getRandomIndex()
}
return rh.getBurstIndex()
rh.mutex.Lock()
ind := rh.getBurstIndex()
rh.mutex.Unlock()

return ind
}

// getRandomIndex generates random indexes in random mode
Expand Down