Skip to content

Commit

Permalink
[WIP] attempt to use quick for ping-pong
Browse files Browse the repository at this point in the history
  • Loading branch information
andoks committed Oct 24, 2023
1 parent 431a326 commit 0289344
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 31 deletions.
31 changes: 31 additions & 0 deletions cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIFWTCCA0GgAwIBAgIUXXvzpUh5vcOQZnpOO8tVWAPr43AwDQYJKoZIhvcNAQEL
BQAwMDELMAkGA1UEBhMCTk8xDTALBgNVBAoMBE5vbmUxEjAQBgNVBAMMCWxvY2Fs
aG9zdDAgFw0yMzEwMjQyMTIwMDBaGA8yMTIzMDkzMDIxMjAwMFowMDELMAkGA1UE
BhMCTk8xDTALBgNVBAoMBE5vbmUxEjAQBgNVBAMMCWxvY2FsaG9zdDCCAiIwDQYJ
KoZIhvcNAQEBBQADggIPADCCAgoCggIBAJ7QRLGQbDFspVNTnmr8t4YgjYl5zZUq
Ylchtbwp9ftcAdpr9FXBqh2COf2WxrBi1j3O0W/MfwD4BGHI8/e1I4yJH8qDjTGy
AseIl3j9WN6V40iu2p8DQoeFREQUW4HqMn0tyZ7z4fOJMhC7V9BXO/mAmIppiGTz
5Ij2Kk4SuPIm/Rvkn0M5r3y++yXUPb11oyjcX6RSszSoaO1y7yLXa/niGPERBQ5d
hRHtKcP4mRqAxLXb6hukGO6Wze9Dsby0FgUhLH7nOU3LmIxgHfnhFAq8dIswIq3h
tzu74WgeV9/KVUK9sfaW9bTYp9mSEpXpBiT6LkDDWLUI29LX75HUV4DaFr05KOdB
UphNcQy6jOb9srYcszXBiPaxzIp0ZPeYVyHyYdcHFLYeXCQqbseg9ZqSJXCaT1/2
Ly5My+CwLT3jUhMvCGj5xp4FnJn0sDUG51+niGUI0VYPO/cKmE3p4u3s8c2RxfJq
YksOKGQNmL3ARduvJUjozB65Blj0Yim2czdxQPFXGPhh3s2HmLz8447mxDcPZit0
EyJ1Q/efEhw0Reqmhmx8fpGK1PL8h/C4NAuFA2CJzOMHN05UHaVZ8iO4BbDX4q2K
Ou3F/xEUqmk/BEPPRHd0niHrB6N2/02LYiu8eLDuoiHx5m2CfkmDED47qgef49Uw
o4ohiPfkNjChAgMBAAGjaTBnMB0GA1UdDgQWBBRYdmMTWlMEkKeDAzhIS3iMc2yw
YzAfBgNVHSMEGDAWgBRYdmMTWlMEkKeDAzhIS3iMc2ywYzAPBgNVHRMBAf8EBTAD
AQH/MBQGA1UdEQQNMAuCCWxvY2FsaG9zdDANBgkqhkiG9w0BAQsFAAOCAgEAWN/G
2E9BoWRQmdbhkV9y++k+KTrPr/wkPSf21fWJQJyV/0dv8uIrxSGO+9yKG6KmRK3F
7JIOtuoimtTNNAEgWapnzeh/G62749abUsU4s40iTGpmKuoJYyl8V2lkx083bw88
6HeEa/ADA3BJRnyxhk5UyPeHa2R7DauJ8w8m6d/i2a3mjwzEXmq3v3dFaL+Lp2Ln
lo4NMXATTNi73dpsjrn+klsfE9x/wETJ5HoSHr/FWWZhyD2M0NxMf5czMjO7pu0H
drZs2Wf3dViq7Aj5OmzfsYtBymykYvIHp83M+5RnrLMQ2FhJDszk8qZAc/xGX9LG
UWOpjCBgtRGIgYWsJ+5pnKfHNdwixcHn31LSpVJncYvAt238+M4TFZCgbu+DbOn2
dYanGJXDjO4Lu8vCRODIT8eKTn480dS61yhtL2WWvNcHNN6Ox3lw2qHjgobO9GW7
RVNBAOsRcpdYcFajSQLTpwLBies5jW9onTvpDvG3ctvSLqvUw0ixWDY51OCAl7cS
mUmGCoGiqF1yMX8UD4+7HfRU87/nVEPOsMdoLDZQIjhFaAauGcwa6nMHA9lQ1m0U
G4FMejtVwbI5pnLhBgb5UWykkg+HlUl0G2zE0KdvzbDxddsOhMzR5pFpnEbcQoQi
3A8pHdu3+yurHCjD5aLwqYE4HOiFZeLlDT8audU=
-----END CERTIFICATE-----
21 changes: 20 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@ module test-http-stream-duplex

go 1.21

require golang.org/x/sync v0.4.0
require (
github.com/quic-go/quic-go v0.39.1
golang.org/x/sync v0.4.0
)

require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/onsi/ginkgo/v2 v2.9.5 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.4 // indirect
go.uber.org/mock v0.3.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.1 // indirect
)
55 changes: 55 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q=
github.com/onsi/ginkgo/v2 v2.9.5/go.mod h1:tvAoo1QUJwNEU2ITftXTpR7R1RbCzoZUOs3RonqW57k=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-20 v0.3.4 h1:MfFAPULvst4yoMgY9QmtpYmfij/em7O8UUi+bNVm7Cg=
github.com/quic-go/qtls-go1-20 v0.3.4/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/quic-go/quic-go v0.39.1 h1:d/m3oaN/SD2c+f7/yEjZxe2zEVotXprnrCCJ2y/ZZFE=
github.com/quic-go/quic-go v0.39.1/go.mod h1:T09QsDQWjLiQ74ZmacDfqZmhY/NLnw5BC40MANNNZ1Q=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=
golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
52 changes: 52 additions & 0 deletions key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQCe0ESxkGwxbKVT
U55q/LeGII2Jec2VKmJXIbW8KfX7XAHaa/RVwaodgjn9lsawYtY9ztFvzH8A+ARh
yPP3tSOMiR/Kg40xsgLHiJd4/VjeleNIrtqfA0KHhUREFFuB6jJ9Lcme8+HziTIQ
u1fQVzv5gJiKaYhk8+SI9ipOErjyJv0b5J9DOa98vvsl1D29daMo3F+kUrM0qGjt
cu8i12v54hjxEQUOXYUR7SnD+JkagMS12+obpBjuls3vQ7G8tBYFISx+5zlNy5iM
YB354RQKvHSLMCKt4bc7u+FoHlffylVCvbH2lvW02KfZkhKV6QYk+i5Aw1i1CNvS
1++R1FeA2ha9OSjnQVKYTXEMuozm/bK2HLM1wYj2scyKdGT3mFch8mHXBxS2H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+rI7AJSBNRNKNFhkiYXW82Awv9NmnXzmtHokE
O/Q9vp1391WC5Ps9HtuP9gAuvPNvS96Z4PqJcnYoPnMLWTzGI4wr6mM9amvDDSly
L9FIOToyacdVgaYQMCI5ja7Jc2dTbPodAaxrG9QZE8ztlVGRjRPPDtkfn46dMfXA
hexdbstE4dv9/b/NwDrij30H+5lUlStH8CMA/eOQJRmsj5IzZdXdGKnXcWMQKRCp
AQKCAQBzg+syxfok28N2rkATf6IvrrRzMoKJZCofvlsN0eRrVhp2chGpgUzigXLK
V8ogPbBzp8aEcFYh1jySm8GWLNre+rT0gRM2C2cDlmfN21fbXX9QXqSJ1iyo2bJ8
B9qC5v5Q9+MQb4LhjLIbOXH9Q8w6ny7EQdt1Z/KlOs5hoHWSwT/JXbgzrFykdXk3
6uZ9WyCrf92zTMzfSTJJXFiKhzJ1DMXOVhl6AXuxxrEBtxN6mXC4nDVCCNrTuBaR
B1fdMTqB1da8BXrwFz3L2bEfgrmOLXVyh1TQX8mnuhpYkmOVFKE0ByshXmvut1P8
WwU+x+O/8x+chhKBQGswOkdZ5szs
-----END PRIVATE KEY-----
118 changes: 88 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package main

import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"time"

"github.com/quic-go/quic-go/http3"
"golang.org/x/sync/errgroup"
)

Expand All @@ -25,13 +29,26 @@ type responseMsg struct {

const ContentTypeNdJson = "application/x-ndjson"

func client(ctx context.Context, address string) error {
client := http.Client{
Transport: nil,
CheckRedirect: nil,
Jar: nil,
Timeout: 0,
}
func client(ctx context.Context, address string, runQuic bool) error {

client := func() interface {
Do(*http.Request) (*http.Response, error)
} {
var t http.RoundTripper = nil
if runQuic {
t = &http3.RoundTripper{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
return &http.Client{
Transport: t,
CheckRedirect: nil,
Jar: nil,
Timeout: 0,
}
}()

var resp *http.Response
var w *io.PipeWriter
Expand Down Expand Up @@ -109,7 +126,7 @@ func client(ctx context.Context, address string) error {
}
}

func server(ctx context.Context, hostPort string) error {
func server(ctx context.Context, hostPort string, runQuic bool) error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
if method := request.Method; method != http.MethodPost {
Expand All @@ -132,10 +149,14 @@ func server(ctx context.Context, hostPort string) error {
}

respCtl := http.NewResponseController(writer)
err := respCtl.EnableFullDuplex()
if err != nil {
slog.Warn("failed to enable full duplex on http writer", "error", err)
return

var err error
if !runQuic {
err = respCtl.EnableFullDuplex()
if err != nil {
slog.Warn("failed to enable full duplex on http writer", "error", err)
return
}
}

var inMsg requestMsg
Expand Down Expand Up @@ -196,21 +217,43 @@ func server(ctx context.Context, hostPort string) error {
}
})

server := http.Server{
Addr: hostPort,
Handler: mux,
DisableGeneralOptionsHandler: false,
TLSConfig: nil,
ReadTimeout: 0,
ReadHeaderTimeout: 0,
WriteTimeout: 0,
IdleTimeout: 0,
MaxHeaderBytes: 0,
TLSNextProto: nil,
ConnState: nil,
ErrorLog: nil,
BaseContext: nil,
ConnContext: nil,
server, err := func() (interface {
ListenAndServe() error
}, error) {
if runQuic {
certificate, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
if err != nil {
return nil, fmt.Errorf("failed to reading certificates, error was: %w", err)
}
_, port, err := net.SplitHostPort(hostPort)
if err != nil {
return nil, fmt.Errorf("failed to parse hostport \"%v\", error was: %w", hostPort, err)
}
pport, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("failed to parse the port part of hostport to a number, error was: %w", err)
}

return &http3.Server{
Addr: hostPort,
Port: pport,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{certificate},
InsecureSkipVerify: true,
SessionTicketKey: [32]byte{},
},
Handler: mux,
}, nil
} else {
return &http.Server{
Addr: hostPort,
Handler: mux,
}, nil
}
}()

if err != nil {
return fmt.Errorf("failed to create server, error was: %w", err)
}

eg, ctx := errgroup.WithContext(ctx)
Expand All @@ -227,7 +270,13 @@ func server(ctx context.Context, hostPort string) error {
timeoutCtx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()
defer slog.Info("server: finished shutting down")
return server.Shutdown(timeoutCtx)

// handle http shutdown if http/http2 server
if shutdowner, ok := server.(interface{ Shutdown(context.Context) error }); ok {
return shutdowner.Shutdown(timeoutCtx)
}

return nil
})

return eg.Wait()
Expand All @@ -236,8 +285,10 @@ func server(ctx context.Context, hostPort string) error {
func main() {
var level slog.Level = slog.LevelInfo
hostPort := "localhost:8080"
runQuic := true
flag.TextVar(&level, "log-level", level, "set log level")
flag.StringVar(&hostPort, "hostport", hostPort, "set hostPort")
flag.BoolVar(&runQuic, "runquic", runQuic, "run quic or not")
flag.Parse()
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
AddSource: false,
Expand All @@ -248,10 +299,17 @@ func main() {
ctx, cancelFunc := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancelFunc()
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { return server(ctx, hostPort) })
eg.Go(func() error { return client(ctx, "http://"+hostPort) })
requestAddress := func() string {
if runQuic {
return "https://" + hostPort
}
return "http://" + hostPort
}()
eg.Go(func() error { return server(ctx, hostPort, runQuic) })
eg.Go(func() error { return client(ctx, requestAddress, runQuic) })
eg.Go(func() error {
<-ctx.Done()
cancelFunc()
slog.Info("signal: interrupt signal received")
return nil
})
Expand Down

0 comments on commit 0289344

Please sign in to comment.