From 1a93118e9bca1f9cfccef0630625526eea8d7930 Mon Sep 17 00:00:00 2001 From: Jmgr Date: Tue, 31 Aug 2021 12:07:53 +0100 Subject: [PATCH 01/15] Replace obsolete websocket library -- allow WebAssembly --- ably/websocket.go | 70 +++++++++++++++++++++++++++++++++++++---------- go.mod | 6 ++-- go.sum | 51 ++++++++++++++++++++++++++++++++-- 3 files changed, 109 insertions(+), 18 deletions(-) diff --git a/ably/websocket.go b/ably/websocket.go index e6d6653b..27f54fad 100644 --- a/ably/websocket.go +++ b/ably/websocket.go @@ -1,55 +1,94 @@ package ably import ( + "context" + "encoding/json" "errors" "net" "net/url" "time" "github.com/ably/ably-go/ably/internal/ablyutil" - "golang.org/x/net/websocket" + "nhooyr.io/websocket" +) + +type proto int + +const ( + jsonProto proto = iota + msgpackProto ) type websocketConn struct { conn *websocket.Conn - codec websocket.Codec + proto proto } func (ws *websocketConn) Send(msg *protocolMessage) error { - return ws.codec.Send(ws.conn, msg) + switch ws.proto { + case jsonProto: + p, err := json.Marshal(msg) + if err != nil { + return err + } + return ws.conn.Write(context.Background(), websocket.MessageText, p) + case msgpackProto: + p, err := ablyutil.MarshalMsgpack(msg) + if err != nil { + return err + } + return ws.conn.Write(context.Background(), websocket.MessageBinary, p) + } + return nil } func (ws *websocketConn) Receive(deadline time.Time) (*protocolMessage, error) { msg := &protocolMessage{} + var ( + ctx context.Context + cancel context.CancelFunc + ) if !deadline.IsZero() { - err := ws.conn.SetReadDeadline(deadline) - if err != nil { - return nil, err - } + ctx = context.Background() + } else { + ctx, cancel = context.WithDeadline(context.Background(), deadline) + defer cancel() } - err := ws.codec.Receive(ws.conn, &msg) + _, data, err := ws.conn.Read(ctx) if err != nil { return nil, err } + switch ws.proto { + case jsonProto: + err := json.Unmarshal(data, msg) + if err != nil { + return nil, err + } + case msgpackProto: + err := ablyutil.UnmarshalMsgpack(data, msg) + if err != nil { + return nil, err + } + } return msg, nil } func (ws *websocketConn) Close() error { - return ws.conn.Close() + return ws.conn.Close(websocket.StatusNormalClosure, "") } func dialWebsocket(proto string, u *url.URL, timeout time.Duration) (*websocketConn, error) { ws := &websocketConn{} switch proto { case "application/json": - ws.codec = websocket.JSON + ws.proto = jsonProto case "application/x-msgpack": - ws.codec = msgpackCodec + ws.proto = msgpackProto default: return nil, errors.New(`invalid protocol "` + proto + `"`) } // Starts a raw websocket connection with server - conn, err := dialWebsocketTimeout(u.String(), "", "https://"+u.Host, timeout) + conn, err := dialWebsocketTimeout(u.String(), "https://"+u.Host, timeout) if err != nil { return nil, err } @@ -58,8 +97,11 @@ func dialWebsocket(proto string, u *url.URL, timeout time.Duration) (*websocketC } // dialWebsocketTimeout dials the websocket with a timeout. -func dialWebsocketTimeout(uri, protocol, origin string, timeout time.Duration) (*websocket.Conn, error) { - config, err := websocket.NewConfig(uri, origin) +func dialWebsocketTimeout(uri, origin string, timeout time.Duration) (*websocket.Conn, error) { + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + c, _, err := websocket.Dial(ctx, uri, nil) if err != nil { return nil, err } diff --git a/go.mod b/go.mod index 97ff2d6d..84eb4e1c 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,14 @@ require ( github.com/ugorji/go/codec v1.1.7 golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 + nhooyr.io/websocket v1.8.7 ) require ( - github.com/davecgh/go-spew v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/klauspost/compress v1.10.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v2 v2.2.2 // indirect + gopkg.in/yaml.v2 v2.2.8 // indirect ) go 1.17 diff --git a/go.sum b/go.sum index fa9b8eeb..5a454696 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,47 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= +github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= +github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= +github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= +github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8= +github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo= +github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= +github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= +github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= @@ -11,14 +50,22 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= +nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= From bcb2c3b96deba09c668ca873ba7d52cc0ea0c045 Mon Sep 17 00:00:00 2001 From: Jonathan Mercier-Ganady Date: Tue, 2 Nov 2021 17:00:51 +0000 Subject: [PATCH 02/15] Add missing code --- ably/websocket.go | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/ably/websocket.go b/ably/websocket.go index 27f54fad..64814532 100644 --- a/ably/websocket.go +++ b/ably/websocket.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "net" "net/url" "time" @@ -102,25 +101,10 @@ func dialWebsocketTimeout(uri, origin string, timeout time.Duration) (*websocket defer cancel() c, _, err := websocket.Dial(ctx, uri, nil) + if err != nil { return nil, err } - config.Header.Set(ablyAgentHeader, ablyAgentIdentifier) - if protocol != "" { - config.Protocol = []string{protocol} - } - config.Dialer = &net.Dialer{ - Timeout: timeout, - } - return websocket.DialConfig(config) -} -var msgpackCodec = websocket.Codec{ - Marshal: func(v interface{}) ([]byte, byte, error) { - p, err := ablyutil.MarshalMsgpack(v) - return p, websocket.BinaryFrame, err - }, - Unmarshal: func(p []byte, _ byte, v interface{}) error { - return ablyutil.UnmarshalMsgpack(p, v) - }, + return c, nil } From b9c489ae4884a2af3e21b49eae4ffef317f543b7 Mon Sep 17 00:00:00 2001 From: Jmgr Date: Thu, 19 May 2022 12:45:03 +0100 Subject: [PATCH 03/15] Update go.mod --- go.mod | 3 +-- go.sum | 17 ++++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 07d9afbb..c49d8bef 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/ably/ably-go require ( github.com/stretchr/testify v1.7.1 github.com/ugorji/go/codec v1.1.9 - golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 nhooyr.io/websocket v1.8.7 ) @@ -12,7 +11,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/klauspost/compress v1.10.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v2 v2.2.8 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) go 1.17 diff --git a/go.sum b/go.sum index 5a454696..553607d4 100644 --- a/go.sum +++ b/go.sum @@ -42,22 +42,19 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go v1.1.9 h1:SObrQTaSuP8WOv2WNCj8gECiNSJIUvk3Q7N26c96Gws= +github.com/ugorji/go v1.1.9/go.mod h1:chLrngdsg43geAaeId+nXO57YsDdl5OZqd/QtBiD19g= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +github.com/ugorji/go/codec v1.1.9 h1:J/7hhpkQwgypRNvaeh/T5gzJ2gEI/l8S3qyRrdEa1fA= +github.com/ugorji/go/codec v1.1.9/go.mod h1:+SWgpdqOgdW5sBaiDfkHilQ1SxQ1hBkq/R+kHfL7Suo= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= @@ -67,5 +64,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= From a8cd02d318ab47737fcfdcdea939ba5bf60208fd Mon Sep 17 00:00:00 2001 From: Jmgr Date: Fri, 20 May 2022 11:35:58 +0100 Subject: [PATCH 04/15] Add missing agent header --- ably/websocket.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ably/websocket.go b/ably/websocket.go index 64814532..ddca35d6 100644 --- a/ably/websocket.go +++ b/ably/websocket.go @@ -100,7 +100,10 @@ func dialWebsocketTimeout(uri, origin string, timeout time.Duration) (*websocket ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() - c, _, err := websocket.Dial(ctx, uri, nil) + var ops websocket.DialOptions + ops.HTTPHeader.Set(ablyAgentHeader, ablyAgentIdentifier) + + c, _, err := websocket.Dial(ctx, uri, &ops) if err != nil { return nil, err From f0bba67e458de81a87e379c55b5d9ef258a59e0a Mon Sep 17 00:00:00 2001 From: Jmgr Date: Fri, 20 May 2022 11:43:52 +0100 Subject: [PATCH 05/15] Fix previous commit --- ably/websocket.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ably/websocket.go b/ably/websocket.go index ddca35d6..c2b2eb2b 100644 --- a/ably/websocket.go +++ b/ably/websocket.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "net/http" "net/url" "time" @@ -101,7 +102,8 @@ func dialWebsocketTimeout(uri, origin string, timeout time.Duration) (*websocket defer cancel() var ops websocket.DialOptions - ops.HTTPHeader.Set(ablyAgentHeader, ablyAgentIdentifier) + ops.HTTPHeader = make(http.Header) + ops.HTTPHeader.Add(ablyAgentHeader, ablyAgentIdentifier) c, _, err := websocket.Dial(ctx, uri, &ops) From 4638d1dae45dcdf35c6f670c9fc30047d14a72de Mon Sep 17 00:00:00 2001 From: Jmgr Date: Tue, 28 Jun 2022 14:30:40 +0100 Subject: [PATCH 06/15] Fix websocket internal test --- ably/websocket_internal_test.go | 36 +++++++++++---------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/ably/websocket_internal_test.go b/ably/websocket_internal_test.go index d5aecc1a..4b036880 100644 --- a/ably/websocket_internal_test.go +++ b/ably/websocket_internal_test.go @@ -95,32 +95,24 @@ func TestWebsocketDial(t *testing.T) { websocketUrl := fmt.Sprintf("ws%s", strings.TrimPrefix(ts.URL, "http")) testServerURL, _ := url.Parse(websocketUrl) - // Calculate the expected Ably-Agent header used by tests. - expectedAgentHeader := AblySDKIdentifier + " " + GoRuntimeIdentifier + " " + GoOSIdentifier() - tests := map[string]struct { - dialProtocol string - expectedPayloadType byte - expectedSubprotocol []string - expectedResult *websocketConn - expectedErr error + dialProtocol string + expectedErr error + expectedProto proto }{ "Can dial for protocol application/json": { - dialProtocol: "application/json", - expectedPayloadType: uint8(1), - expectedSubprotocol: []string(nil), - expectedErr: nil, + dialProtocol: "application/json", + expectedErr: nil, + expectedProto: 0, }, "Can dial for protocol application/x-msgpack": { - dialProtocol: "application/x-msgpack", - expectedPayloadType: uint8(1), - expectedSubprotocol: []string(nil), - expectedErr: nil, + dialProtocol: "application/x-msgpack", + expectedErr: nil, + expectedProto: 1, }, "Can handle an error when dialing for an invalid protocol": { - dialProtocol: "aProtocol", - expectedResult: nil, - expectedErr: errors.New(`invalid protocol "aProtocol"`), + dialProtocol: "aProtocol", + expectedErr: errors.New(`invalid protocol "aProtocol"`), }, } @@ -131,11 +123,7 @@ func TestWebsocketDial(t *testing.T) { assert.Equal(t, test.expectedErr, err) if result != nil { - assert.NotNil(t, result.codec) - assert.Equal(t, expectedAgentHeader, result.conn.Config().Header.Get("Ably-Agent")) - assert.Equal(t, test.expectedPayloadType, result.conn.PayloadType) - assert.Equal(t, test.expectedSubprotocol, result.conn.Config().Protocol) - assert.True(t, result.conn.IsClientConn()) + assert.Equal(t, test.expectedProto, result.proto) } }) } From cc0925581e6b05e78078c4356c0b4f6270cd1c74 Mon Sep 17 00:00:00 2001 From: Jonathan Mercier-Ganady Date: Tue, 28 Jun 2022 15:03:34 +0100 Subject: [PATCH 07/15] Update ably/websocket_internal_test.go Co-authored-by: Rosie Hamilton --- ably/websocket_internal_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ably/websocket_internal_test.go b/ably/websocket_internal_test.go index 4b036880..f0b27323 100644 --- a/ably/websocket_internal_test.go +++ b/ably/websocket_internal_test.go @@ -103,7 +103,7 @@ func TestWebsocketDial(t *testing.T) { "Can dial for protocol application/json": { dialProtocol: "application/json", expectedErr: nil, - expectedProto: 0, + expectedProto: jsonProto, }, "Can dial for protocol application/x-msgpack": { dialProtocol: "application/x-msgpack", From 562da816f6d2ef3d50c1373d28be4defe9c0dd64 Mon Sep 17 00:00:00 2001 From: Jonathan Mercier-Ganady Date: Tue, 28 Jun 2022 15:03:41 +0100 Subject: [PATCH 08/15] Update ably/websocket_internal_test.go Co-authored-by: Rosie Hamilton --- ably/websocket_internal_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ably/websocket_internal_test.go b/ably/websocket_internal_test.go index f0b27323..118db5c7 100644 --- a/ably/websocket_internal_test.go +++ b/ably/websocket_internal_test.go @@ -108,7 +108,7 @@ func TestWebsocketDial(t *testing.T) { "Can dial for protocol application/x-msgpack": { dialProtocol: "application/x-msgpack", expectedErr: nil, - expectedProto: 1, + expectedProto: msgpackProto, }, "Can handle an error when dialing for an invalid protocol": { dialProtocol: "aProtocol", From 63a093829462ea6ace23ad50a648650bec3a18fd Mon Sep 17 00:00:00 2001 From: Jmgr Date: Tue, 28 Jun 2022 15:06:15 +0100 Subject: [PATCH 09/15] Additional fixes after review --- ably/websocket_internal_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ably/websocket_internal_test.go b/ably/websocket_internal_test.go index 4b036880..b794b5a9 100644 --- a/ably/websocket_internal_test.go +++ b/ably/websocket_internal_test.go @@ -123,6 +123,7 @@ func TestWebsocketDial(t *testing.T) { assert.Equal(t, test.expectedErr, err) if result != nil { + assert.NotNil(t, result.conn) assert.Equal(t, test.expectedProto, result.proto) } }) @@ -141,8 +142,6 @@ func TestWebsocketSendAndReceive(t *testing.T) { tests := map[string]struct { dialProtocol string expectedMessageType string - expectedPayloadType byte - expectedResult *websocketConn expectedErr error }{ "Can send and receive a message using protocol application/json": { From fe85d9d705a9802e77f9920f5b054fde7c7b9bcd Mon Sep 17 00:00:00 2001 From: Jmgr Date: Wed, 29 Jun 2022 10:26:00 +0100 Subject: [PATCH 10/15] Slightly increase the timeout in ablytest This is a quick fix for enabling websocket tests to pass using nhooyr.io/websocket. In the long term we would probably want to remove all timeouts in tests though. --- ablytest/timeout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ablytest/timeout.go b/ablytest/timeout.go index f8347762..df57e593 100644 --- a/ablytest/timeout.go +++ b/ablytest/timeout.go @@ -7,7 +7,7 @@ import ( "time" ) -var Instantly = Before(10 * time.Millisecond) +var Instantly = Before(20 * time.Millisecond) var Soon = Before(Timeout) From 0df6fcc7069aeeb7c02f904694a15cee9ec33714 Mon Sep 17 00:00:00 2001 From: Jmgr Date: Wed, 29 Jun 2022 17:05:42 +0100 Subject: [PATCH 11/15] Fix websocket deadline --- ably/websocket.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ably/websocket.go b/ably/websocket.go index c2b2eb2b..36266d36 100644 --- a/ably/websocket.go +++ b/ably/websocket.go @@ -49,10 +49,10 @@ func (ws *websocketConn) Receive(deadline time.Time) (*protocolMessage, error) { cancel context.CancelFunc ) if !deadline.IsZero() { - ctx = context.Background() - } else { ctx, cancel = context.WithDeadline(context.Background(), deadline) defer cancel() + } else { + ctx = context.Background() } _, data, err := ws.conn.Read(ctx) if err != nil { From 90d3dfef516314a302b5ff83ce6e1e625e554386 Mon Sep 17 00:00:00 2001 From: Jmgr Date: Wed, 29 Jun 2022 17:49:34 +0100 Subject: [PATCH 12/15] Slightly increase the timeout in ablytest --- ablytest/timeout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ablytest/timeout.go b/ablytest/timeout.go index df57e593..0902e7a5 100644 --- a/ablytest/timeout.go +++ b/ablytest/timeout.go @@ -7,7 +7,7 @@ import ( "time" ) -var Instantly = Before(20 * time.Millisecond) +var Instantly = Before(40 * time.Millisecond) var Soon = Before(Timeout) From 62d0dc61f50b85eac2aeff3c94659240215da788 Mon Sep 17 00:00:00 2001 From: Rosie Hamilton Date: Fri, 1 Jul 2022 10:14:57 +0100 Subject: [PATCH 13/15] Send to allowDial channel in test outside of ablytest package and revert timeout changes --- ably/realtime_conn_integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ably/realtime_conn_integration_test.go b/ably/realtime_conn_integration_test.go index 75f2d545..b43e15a8 100644 --- a/ably/realtime_conn_integration_test.go +++ b/ably/realtime_conn_integration_test.go @@ -287,7 +287,7 @@ func TestRealtimeConn_SendErrorReconnects(t *testing.T) { ablytest.Instantly.NoRecv(t, nil, publishErr, t.Fatalf) // Reconnect should happen instantly as a result of transport closure. - ablytest.Instantly.Send(t, allowDial, struct{}{}, t.Fatalf) + allowDial <- struct{}{} // After reconnection, message should be published. ablytest.Soon.Recv(t, &err, publishErr, t.Fatalf) From f170c91ea7e1225dd0562efdf9e6d571f781228f Mon Sep 17 00:00:00 2001 From: Jmgr Date: Fri, 1 Jul 2022 10:27:44 +0100 Subject: [PATCH 14/15] Readability improvement --- ably/websocket.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ably/websocket.go b/ably/websocket.go index 36266d36..bef3a87d 100644 --- a/ably/websocket.go +++ b/ably/websocket.go @@ -48,11 +48,11 @@ func (ws *websocketConn) Receive(deadline time.Time) (*protocolMessage, error) { ctx context.Context cancel context.CancelFunc ) - if !deadline.IsZero() { + if deadline.IsZero() { + ctx = context.Background() + } else { ctx, cancel = context.WithDeadline(context.Background(), deadline) defer cancel() - } else { - ctx = context.Background() } _, data, err := ws.conn.Read(ctx) if err != nil { From aaa2962fcbc33485a8ff2fc91d76580d3f202bda Mon Sep 17 00:00:00 2001 From: Jonathan Mercier-Ganady Date: Fri, 1 Jul 2022 10:29:15 +0100 Subject: [PATCH 15/15] Revert timeout duration to previous value Co-authored-by: Rosie Hamilton --- ablytest/timeout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ablytest/timeout.go b/ablytest/timeout.go index 0902e7a5..f8347762 100644 --- a/ablytest/timeout.go +++ b/ablytest/timeout.go @@ -7,7 +7,7 @@ import ( "time" ) -var Instantly = Before(40 * time.Millisecond) +var Instantly = Before(10 * time.Millisecond) var Soon = Before(Timeout)