How to use GotConn method of httpext Package

Best K6 code snippet using httpext.GotConn

tracer.go

Source:tracer.go Github

copy

Full Screen

...102 ConnectStart: t.ConnectStart,103 ConnectDone: t.ConnectDone,104 TLSHandshakeStart: t.TLSHandshakeStart,105 TLSHandshakeDone: t.TLSHandshakeDone,106 GotConn: t.GotConn,107 WroteRequest: t.WroteRequest,108 GotFirstResponseByte: t.GotFirstResponseByte,109 }110}111func now() int64 {112 return time.Now().UnixNano()113}114// GetConn is called before a connection is created or115// retrieved from an idle pool. The hostPort is the116// "host:port" of the target or proxy. GetConn is called even117// if there's already an idle cached connection available.118//119// Keep in mind that GetConn won't be called if a connection120// is reused though, for example when there's a redirect.121// If it's called, it will be called before all other hooks.122func (t *Tracer) GetConn(hostPort string) {123 t.getConn = now()124}125// ConnectStart is called when a new connection's Dial begins.126// If net.Dialer.DualStack (IPv6 "Happy Eyeballs") support is127// enabled, this may be called multiple times.128//129// If the connection is reused, this won't be called. Otherwise,130// it will be called after GetConn() and before ConnectDone().131func (t *Tracer) ConnectStart(network, addr string) {132 // If using dual-stack dialing, it's possible to get this133 // multiple times, so the atomic compareAndSwap ensures134 // that only the first call's time is recorded135 atomic.CompareAndSwapInt64(&t.connectStart, 0, now())136}137// ConnectDone is called when a new connection's Dial138// completes. The provided err indicates whether the139// connection completedly successfully.140// If net.Dialer.DualStack ("Happy Eyeballs") support is141// enabled, this may be called multiple times.142//143// If the connection is reused, this won't be called. Otherwise,144// it will be called after ConnectStart() and before either145// TLSHandshakeStart() (for TLS connections) or GotConn().146func (t *Tracer) ConnectDone(network, addr string, err error) {147 // If using dual-stack dialing, it's possible to get this148 // multiple times, so the atomic compareAndSwap ensures149 // that only the first call's time is recorded150 if err == nil {151 atomic.CompareAndSwapInt64(&t.connectDone, 0, now())152 }153 // if there is an error it either is happy eyeballs related and doesn't matter or it will be154 // returned by the http call155}156// TLSHandshakeStart is called when the TLS handshake is started. When157// connecting to a HTTPS site via a HTTP proxy, the handshake happens after158// the CONNECT request is processed by the proxy.159//160// If the connection is reused, this won't be called. Otherwise,161// it will be called after ConnectDone() and before TLSHandshakeDone().162func (t *Tracer) TLSHandshakeStart() {163 atomic.CompareAndSwapInt64(&t.tlsHandshakeStart, 0, now())164}165// TLSHandshakeDone is called after the TLS handshake with either the166// successful handshake's connection state, or a non-nil error on handshake167// failure.168//169// If the connection is reused, this won't be called. Otherwise,170// it will be called after TLSHandshakeStart() and before GotConn().171// If the request was cancelled, this could be called after the172// RoundTrip() method has returned.173func (t *Tracer) TLSHandshakeDone(state tls.ConnectionState, err error) {174 if err == nil {175 atomic.CompareAndSwapInt64(&t.tlsHandshakeDone, 0, now())176 }177 // if there is an error it will be returned by the http call178}179// GotConn is called after a successful connection is180// obtained. There is no hook for failure to obtain a181// connection; instead, use the error from Transport.RoundTrip.182//183// This is the fist hook called for reused connections. For new184// connections, it's called either after TLSHandshakeDone()185// (for TLS connections) or after ConnectDone()186func (t *Tracer) GotConn(info httptrace.GotConnInfo) {187 now := now()188 // This shouldn't be called multiple times so no synchronization here,189 // it's better for the race detector to panic if we're wrong.190 t.gotConn = now191 t.connReused = info.Reused192 t.connRemoteAddr = info.Conn.RemoteAddr()193 // The Go stdlib's http module can start connecting to a remote server, only194 // to abandon that connection even before it was fully established and reuse195 // a recently freed already existing connection.196 // We overwrite the different timestamps here, so the other callbacks don't197 // put incorrect values in them (they use CompareAndSwap)198 _, isConnTLS := info.Conn.(*tls.Conn)199 if info.Reused {200 atomic.SwapInt64(&t.connectStart, now)201 atomic.SwapInt64(&t.connectDone, now)202 if isConnTLS {203 atomic.SwapInt64(&t.tlsHandshakeStart, now)204 atomic.SwapInt64(&t.tlsHandshakeDone, now)205 }206 } else {207 // There's a bug in the Go stdlib where an HTTP/2 connection can be reused208 // but the httptrace.GotConnInfo struct will contain a false Reused property...209 // That's probably from a previously made connection that was abandoned and210 // directly put in the connection pool in favor of a just-freed already211 // established connection...212 //213 // Using CompareAndSwap here because the HTTP/2 roundtripper has retries and214 // it's possible this isn't actually the first request attempt...215 atomic.CompareAndSwapInt64(&t.connectStart, 0, now)216 atomic.CompareAndSwapInt64(&t.connectDone, 0, now)217 if isConnTLS {218 atomic.CompareAndSwapInt64(&t.tlsHandshakeStart, 0, now)219 atomic.CompareAndSwapInt64(&t.tlsHandshakeDone, 0, now)220 }221 }222}223// WroteRequest is called with the result of writing the224// request and any body. It may be called multiple times225// in the case of retried requests.226func (t *Tracer) WroteRequest(info httptrace.WroteRequestInfo) {227 if info.Err == nil {228 atomic.StoreInt64(&t.wroteRequest, now())229 }230 // if there is an error it will be returned by the http call231}232// GotFirstResponseByte is called when the first byte of the response233// headers is available.234// If the request was cancelled, this could be called after the235// RoundTrip() method has returned.236func (t *Tracer) GotFirstResponseByte() {237 atomic.CompareAndSwapInt64(&t.gotFirstResponseByte, 0, now())238}239// Done calculates all metrics and should be called when the request is finished.240func (t *Tracer) Done() *Trail {241 done := time.Now()242 trail := Trail{243 ConnReused: t.connReused,244 ConnRemoteAddr: t.connRemoteAddr,245 }246 if t.gotConn != 0 && t.getConn != 0 && t.gotConn > t.getConn {247 trail.Blocked = time.Duration(t.gotConn - t.getConn)248 }249 // It's possible for some of the methods of httptrace.ClientTrace to250 // actually be called after the http.Client or http.RoundTripper have251 // already returned our result and we've called Done(). This happens252 // mostly for cancelled requests, but we have to use atomics here as253 // well (or use global Tracer locking) so we can avoid data races.254 connectStart := atomic.LoadInt64(&t.connectStart)255 connectDone := atomic.LoadInt64(&t.connectDone)256 tlsHandshakeStart := atomic.LoadInt64(&t.tlsHandshakeStart)257 tlsHandshakeDone := atomic.LoadInt64(&t.tlsHandshakeDone)258 gotConn := atomic.LoadInt64(&t.gotConn)259 wroteRequest := atomic.LoadInt64(&t.wroteRequest)260 gotFirstResponseByte := atomic.LoadInt64(&t.gotFirstResponseByte)261 if connectDone != 0 && connectStart != 0 {262 trail.Connecting = time.Duration(connectDone - connectStart)263 }264 if tlsHandshakeDone != 0 && tlsHandshakeStart != 0 {265 trail.TLSHandshaking = time.Duration(tlsHandshakeDone - tlsHandshakeStart)266 }267 if wroteRequest != 0 {268 if tlsHandshakeDone != 0 {269 // If the request was sent over TLS, we need to use270 // TLS Handshake Done time to calculate sending duration271 trail.Sending = time.Duration(wroteRequest - tlsHandshakeDone)272 } else if connectDone != 0 {273 // Otherwise, use the end of the normal connection274 trail.Sending = time.Duration(wroteRequest - connectDone)275 } else {276 // Finally, this handles the strange HTTP/2 case where the GotConn() hook277 // gets called first, but with Reused=false278 trail.Sending = time.Duration(wroteRequest - gotConn)279 }280 if gotFirstResponseByte != 0 {281 // We started receiving at least some response back282 if gotFirstResponseByte > wroteRequest {283 // For some requests, especially HTTP/2, the server starts responding before the284 // client has finished sending the full request285 trail.Waiting = time.Duration(gotFirstResponseByte - wroteRequest)286 }287 } else {288 // The server never responded to our request289 trail.Waiting = done.Sub(time.Unix(0, wroteRequest))290 }...

Full Screen

Full Screen

GotConn

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := &http.Client{4 Transport: &http.Transport{5 },6 }7 for {8 if err != nil {9 fmt.Println(err)10 }11 time.Sleep(1 * time.Second)12 }13}14import (15func main() {16 c := &http.Client{17 }18 for {19 if err != nil {20 fmt.Println(err)21 }22 time.Sleep(1 * time.Second)23 }24}25import (26func main() {27 c := &http.Client{28 }29 for {30 if err != nil {31 fmt.Println(err)32 }33 _, err = c.Do(req)34 if err != nil {35 fmt.Println(err)36 }37 time.Sleep(1 * time.Second)38 }39}40import (41func main() {42 c := &http.Client{43 }44 for {45 if err != nil {46 fmt.Println(err)47 }48 resp, err := c.Do(req)49 if err != nil {50 fmt.Println(err)51 }52 resp.Body.Close()53 time.Sleep(1 * time.Second)54 }55}56import (57func main() {58 c := &http.Client{

Full Screen

Full Screen

GotConn

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if len(os.Args) < 2 {4 fmt.Println("Usage: 1.exe url")5 os.Exit(1)6 }7 url, err := url.Parse(os.Args[1])8 if err != nil {9 fmt.Println("Error parsing url")10 os.Exit(1)11 }12 jar, err := cookiejar.New(nil)13 if err != nil {14 fmt.Println("Error creating cookie jar")15 os.Exit(1)16 }17 client := &http.Client{18 }19 httpext := &httpext{20 }21 req, err := http.NewRequest("GET", url.String(), nil)22 if err != nil {23 fmt.Println("Error creating request")24 os.Exit(1)25 }26 resp, err := httpext.GotConn(req, nil)27 if err != nil {28 fmt.Println("Error creating response")29 os.Exit(1)30 }31 dump, err := httputil.DumpResponse(resp, true)32 if err != nil {33 fmt.Println("Error dumping response")34 os.Exit(1)35 }36 fmt.Println(string(dump))37}38type httpext struct {39}40func (h *httpext) GotConn(req *http.Request, conn *http.Conn) (*http.Response, error) {41 request, err := http.NewRequest(req.Method, req.URL.String(), req.Body)42 if err != nil {43 fmt.Println("Error creating request")44 os.Exit(1)45 }46 request.Header.Add("Cookie", strings.Join(req.Cookies(), ";"))47 resp, err := h.client.Do(request)48 if err != nil {49 fmt.Println("Error creating response")50 os.Exit(1)51 }52}53type ReadCloser interface {54}

Full Screen

Full Screen

GotConn

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 request := gorequest.New()4 request.Transport = &http.Transport{5 Dial: (&net.Dialer{6 }).Dial,7 }8 request.GotConn = func(conn net.Conn, got time.Time) {9 fmt.Println("Got connection after ", time.Since(got))10 }11 _, body, errs := request.End()12 if errs != nil {13 fmt.Println(errs)14 }15 fmt.Println(body)16}17import (18func main() {19 request := gorequest.New()20 request.Transport = &http.Transport{21 Dial: (&net.Dialer{22 }).Dial,23 }24 request.GotConn = func(conn net.Conn, got time.Time) {25 fmt.Println("Got connection after ", time.Since(got))26 }27 _, body, errs := request.End()28 if errs != nil {29 fmt.Println(errs)30 }31 fmt.Println(body)32}33import (34func main() {35 request := gorequest.New()36 request.Transport = &http.Transport{37 Dial: (&net.Dialer{38 }).Dial,39 }40 request.GotConn = func(conn net.Conn, got time.Time) {41 fmt.Println("Got connection after ", time.Since(got))42 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful