Skip to content

Commit

Permalink
Client settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrr committed Jul 25, 2021
1 parent 8ed1ca7 commit 919397b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 138 deletions.
6 changes: 4 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http2
import (
"container/list"
"sync"
"time"

"github.com/valyala/fasthttp"
)
Expand All @@ -20,6 +21,8 @@ type Ctx struct {
type Client struct {
d *Dialer

onRTT func(time.Duration)

lck sync.Mutex
conns list.List
}
Expand All @@ -44,8 +47,7 @@ getConn:
} else {
var err error

c, err = cl.d.Dial()
if err != nil {
if c, err = cl.d.Dial(); err != nil {
cl.lck.Unlock()
return err
}
Expand Down
22 changes: 12 additions & 10 deletions fasthttp2/client_test.go → client_test.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
package fasthttp2
package http2

import (
"bufio"
"bytes"
"io"
"math/rand"
"net"
"testing"

"github.com/dgrr/http2"
)

func TestClientWriteOrder(t *testing.T) {
bf := bytes.NewBuffer(nil)

c := &http2.Client{}
c.writer = make(chan *Frame, 1)
c := &Conn{
c: &net.TCPConn{},
}
c.out = make(chan *FrameHeader, 1)
c.bw = bufio.NewWriter(bf)

go c.writeLoop()

framesToTest := 32

id := uint32(1)
frames := make([]*Frame, 0, framesToTest)
frames := make([]*FrameHeader, 0, framesToTest)

for i := 0; i < framesToTest; i++ {
fr := AcquireFrame()
fr := AcquireFrameHeader()
fr.SetStream(id)
fr.SetBody(&Data{})
id += 2
frames = append(frames, fr)
}

for len(frames) > 0 {
i := rand.Intn(len(frames))

c.writeFrame(frames[i])
c.out <- frames[i]
frames = append(frames[:i], frames[i+1:]...)
}

br := bufio.NewReader(bf)
fr := AcquireFrame()
fr := AcquireFrameHeader()

expected := uint32(1)
for i := 0; i < framesToTest; i++ {
Expand All @@ -58,5 +60,5 @@ func TestClientWriteOrder(t *testing.T) {
expected += 2
}

close(c.writer)
close(c.out)
}
11 changes: 10 additions & 1 deletion configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"errors"
"net"
"time"

"github.com/valyala/fasthttp"
)
Expand All @@ -13,6 +14,12 @@ var (
ErrServerSupport = errors.New("server doesn't support HTTP/2")
)

type ClientOpts struct {
// OnRTT is assigned to every client after creation, and the handler
// will be called after every RTT measurement (after receiving a PONG mesage).
OnRTT func(time.Duration)
}

func configureDialer(d *Dialer) {
if d.TLSConfig == nil {
d.TLSConfig = &tls.Config{
Expand All @@ -37,7 +44,7 @@ func configureDialer(d *Dialer) {
}

// ConfigureClient configures the fasthttp.HostClient to run over HTTP/2.
func ConfigureClient(c *fasthttp.HostClient) error {
func ConfigureClient(c *fasthttp.HostClient, opts ClientOpts) error {
emptyServerName := c.TLSConfig != nil && len(c.TLSConfig.ServerName) == 0

d := &Dialer{
Expand Down Expand Up @@ -66,6 +73,8 @@ func ConfigureClient(c *fasthttp.HostClient) error {
c.TLSConfig = d.TLSConfig

cl := createClient(d)
cl.onRTT = opts.OnRTT

cl.conns.Init()

c.Transport = cl.Do
Expand Down
2 changes: 1 addition & 1 deletion examples/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
IsTLS: true,
}

if err := http2.ConfigureClient(c); err != nil {
if err := http2.ConfigureClient(c, http2.ClientOpts{}); err != nil {
panic(err)
}

Expand Down
124 changes: 0 additions & 124 deletions fasthttp2/client.go

This file was deleted.

0 comments on commit 919397b

Please sign in to comment.