Skip to content

Commit

Permalink
update quic implementation
Browse files Browse the repository at this point in the history
update examples for CallAsync
  • Loading branch information
lesismal committed Dec 31, 2023
1 parent 03c172d commit f4ea71f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
37 changes: 20 additions & 17 deletions examples/bench_pool/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,32 @@ func main() {
go func() {
ticker := time.NewTicker(time.Second)
for i := 0; true; i++ {
select {
case <-ticker.C:
req := &HelloReq{Msg: fmt.Sprintf("[%v] %v", client.Conn.LocalAddr(), i)}
rsp := &HelloRsp{}
err := client.CallAsync(method, req, func(ctx *arpc.Context) {
err := ctx.Bind(rsp)
if err != nil || rsp.Msg != req.Msg {
log.Printf("CallAsync failed: %v", err)
atomic.AddUint64(&failedTotal, 1)
} else {
//log.Printf("Call Response: \"%v\"", rsp.Msg)
atomic.AddUint64(&qpsSec, 1)
atomic.AddUint64(&asyncTimes, 1)
}
}, time.Second*5)
if err != nil {
log.Printf("CallAsync failed: %v", err)
<-ticker.C
req := &HelloReq{Msg: fmt.Sprintf("[%v] %v", client.Conn.LocalAddr(), i)}
rsp := &HelloRsp{}
err := client.CallAsync(method, req, func(ctx *arpc.Context, er error) {
if er != nil {
log.Printf("CallAsync failed: %v", er)
atomic.AddUint64(&failedTotal, 1)
return
}
er = ctx.Bind(rsp)
if er != nil || rsp.Msg != req.Msg {
log.Printf("CallAsync failed: %v", er)
atomic.AddUint64(&failedTotal, 1)
} else {
//log.Printf("Call Response: \"%v\"", rsp.Msg)
atomic.AddUint64(&qpsSec, 1)
atomic.AddUint64(&asyncTimes, 1)
}
}, time.Second*5)
if err != nil {
log.Printf("CallAsync failed: %v", err)
atomic.AddUint64(&failedTotal, 1)
} else {
//log.Printf("Call Response: \"%v\"", rsp.Msg)
atomic.AddUint64(&qpsSec, 1)
atomic.AddUint64(&asyncTimes, 1)
}
}
}()
Expand Down
13 changes: 8 additions & 5 deletions examples/rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ func main() {
log.Printf("Call /echo/async Response: \"%v\"", rsp)
}
done := make(chan string)
err = client.CallAsync("/echo/async", &req, func(ctx *arpc.Context) {
err = client.CallAsync("/echo/async", &req, func(ctx *arpc.Context, er error) {
if er != nil {
log.Fatalf("Call /echo/async failed: %v", err)
}
rsp := ""
err = ctx.Bind(&rsp)
if err != nil {
log.Fatalf("Call /echo/async Bind failed: %v", err)
er = ctx.Bind(&rsp)
if er != nil {
log.Fatalf("Call /echo/async Bind failed: %v", er)
}
if rsp != req {
log.Fatalf("Call /echo/async failed: %v", err)
log.Fatalf("Call /echo/async failed: %v", er)
}
done <- rsp
}, time.Second*5)
Expand Down
14 changes: 7 additions & 7 deletions extension/protocol/quic/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import (
"net"
"time"

quic "github.com/lucas-clemente/quic-go"
quic "github.com/quic-go/quic-go"
)

// Listener wraps quick.Listener to net.Listener
type Listener struct {
quic.Listener
*quic.Listener
}

// Accept waits for and returns the next connection to the listener.
func (ln *Listener) Accept() (net.Conn, error) {
session, err := ln.Listener.Accept(context.Background())
conn, err := ln.Listener.Accept(context.Background())
if err != nil {
return nil, err
}

stream, err := session.AcceptStream(context.Background())
stream, err := conn.AcceptStream(context.Background())
if err != nil {
return nil, err
}

return &Conn{session, stream}, err
return &Conn{conn, stream}, err
}

// Conn wraps quick.Session to net.Conn
type Conn struct {
quic.Session
quic.Connection
quic.Stream
}

Expand All @@ -59,7 +59,7 @@ func Dial(addr string, tlsConf *tls.Config, quicConf *quic.Config, timeout time.
defer cancel()
}

session, err := quic.DialAddr(addr, tlsConf, quicConf)
session, err := quic.DialAddr(ctx, addr, tlsConf, quicConf)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f4ea71f

Please sign in to comment.