diff --git a/cycletls/index.go b/cycletls/index.go index 915b19ad..67881130 100644 --- a/cycletls/index.go +++ b/cycletls/index.go @@ -3,6 +3,7 @@ package cycletls import ( "encoding/json" "flag" + "fmt" http "github.com/Danny-Dasilva/fhttp" "github.com/gorilla/websocket" "io/ioutil" @@ -35,14 +36,14 @@ 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 @@ -50,7 +51,7 @@ type Response struct { 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) @@ -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 @@ -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 @@ -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{}