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

Return RequestId on Queue method #179

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions cycletls/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cycletls
import (
"encoding/json"
"flag"
"fmt"
http "github.com/Danny-Dasilva/fhttp"
"github.com/gorilla/websocket"
"io/ioutil"
Expand Down Expand Up @@ -35,22 +36,22 @@ type cycleTLSRequest struct {
Options Options `json:"options"`
}

//rename to request+client+options
// rename to request+client+options
type fullRequest struct {
req *http.Request
client http.Client
options cycleTLSRequest
}

//Response contains Cycletls response data
// Response contains Cycletls response data
type Response struct {
RequestID string
Status int
Body string
Headers map[string]string
}

//JSONBody converts response body to json
// JSONBody converts response body to json
func (re Response) JSONBody() map[string]interface{} {
var data map[string]interface{}
err := json.Unmarshal([]byte(re.Body), &data)
Expand All @@ -60,10 +61,11 @@ func (re Response) JSONBody() map[string]interface{} {
return data
}

//CycleTLS creates full request and response
// CycleTLS creates full request and response
type CycleTLS struct {
ReqChan chan fullRequest
RespChan chan Response
ReqChan chan fullRequest
RespChan chan Response
queuedRequestCounter int
}

// ready Request
Expand Down Expand Up @@ -205,14 +207,16 @@ func dispatcher(res fullRequest) (response Response, err error) {
}

// Queue queues request in worker pool
func (client CycleTLS) Queue(URL string, options Options, Method string) {
func (client CycleTLS) Queue(URL string, options Options, Method string) string {

options.URL = URL
options.Method = Method
//TODO add timestamp to request
opt := cycleTLSRequest{"Queued Request", options}
opt := cycleTLSRequest{fmt.Sprintf("Queued Request %d", client.queuedRequestCounter), options}
client.queuedRequestCounter++
response := processRequest(opt)
client.ReqChan <- response
return opt.RequestID
}

// Do creates a single request
Expand Down Expand Up @@ -242,7 +246,7 @@ func Init(workers ...bool) CycleTLS {
go workerPool(reqChan, respChan)
log.Println("Worker Pool Started")

return CycleTLS{ReqChan: reqChan, RespChan: respChan}
return CycleTLS{ReqChan: reqChan, RespChan: respChan, queuedRequestCounter: 0}
}
return CycleTLS{}

Expand Down