Skip to content

Commit

Permalink
Transport with metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 13, 2022
1 parent 988dda7 commit 65e7761
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions core/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package core

import (
"net"
"net/http"
"time"
)

type Transport struct {
rtp http.RoundTripper
dialer *net.Dialer
connStart time.Time
connEnd time.Time
reqStart time.Time
reqEnd time.Time
}

func NewTransport(timeout time.Duration) *Transport {
tr := &Transport{
dialer: &net.Dialer{
Timeout: timeout,
KeepAlive: timeout,
},
}
tr.rtp = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: tr.Dial,
TLSHandshakeTimeout: timeout,
}
return tr
}

func (tr *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
tr.reqStart = time.Now()
resp, err := tr.rtp.RoundTrip(r)
tr.reqEnd = time.Now()
return resp, err
}

func (tr *Transport) Dial(network, addr string) (net.Conn, error) {
tr.connStart = time.Now()
cn, err := tr.dialer.Dial(network, addr)
tr.connEnd = time.Now()
return cn, err
}

func (tr *Transport) ReqDuration() time.Duration {
return tr.Duration() - tr.ConnDuration()
}

func (tr *Transport) ConnDuration() time.Duration {
return tr.connEnd.Sub(tr.connStart)
}

func (tr *Transport) Duration() time.Duration {
return tr.reqEnd.Sub(tr.reqStart)
}

0 comments on commit 65e7761

Please sign in to comment.