Skip to content

Commit 612081e

Browse files
authored
fstthttp,interal,x: Add v2 versions of Handoff Websocket and Fanout (#210)
1 parent 1c1f4dc commit 612081e

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

fsthttp/request.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,30 @@ type DecompressResponseOptions struct {
10871087
Gzip bool
10881088
}
10891089

1090+
// HandoffWebsocket passes the WebSocket directly to a backend.
1091+
//
1092+
// This can only be used on services that have the WebSockets feature
1093+
// enabled and on requests that are valid WebSocket requests. The sending
1094+
// completes in the background.
1095+
//
1096+
// Once this method has been called, no other response can be sent to this
1097+
// request, and the application can exit without affecting the send.
1098+
func (r *Request) HandoffWebsocket(backend string) error {
1099+
return r.downstream.req.HandoffWebsocket(backend)
1100+
}
1101+
1102+
// HandoffFanout passes the request through the Fanout GRIP proxy and on to
1103+
// a backend.
1104+
//
1105+
// This can only be used on services that have the Fanout feature enabled.
1106+
//
1107+
// The sending completes in the background. Once this method has been
1108+
// called, no other response can be sent to this request, and the
1109+
// application can exit without affecting the send.
1110+
func (r *Request) HandoffFanout(backend string) error {
1111+
return r.downstream.req.HandoffWebsocket(backend)
1112+
}
1113+
10901114
// nopCloser is functionally the same as io.NopCloser, except that we
10911115
// can get to the underlying io.Reader.
10921116
type nopCloser struct {

internal/abi/fastly/hostcalls_noguest.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ func (r *HTTPRequest) SetFramingHeadersMode(manual bool) error {
233233
return fmt.Errorf("not implemented")
234234
}
235235

236+
func (r *HTTPRequest) HandoffWebsocket(backend string) error {
237+
return fmt.Errorf("not implemented")
238+
}
239+
240+
func (r *HTTPRequest) HandoffFanout(backend string) error {
241+
return fmt.Errorf("not implemented")
242+
}
243+
236244
type HTTPRequestPromise struct{}
237245

238246
func DownstreamNextRequest(opts *NextRequestOptions) (*HTTPRequestPromise, error) {

internal/abi/fastly/http_guest.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,35 @@ import (
1616
// (module $fastly_http_req
1717

1818
// witx:
19+
20+
// (@interface func (export "redirect_to_websocket_proxy_v2")
1921
//
20-
// (@interface func (export "redirect_to_websocket_proxy")
21-
//
22+
// (param $h $request_handle)
2223
// (param $backend_name string)
2324
// (result $err (expected (error $fastly_status)))
2425
//
2526
// )
2627
//
28+
//go:wasmimport fastly_http_req redirect_to_websocket_proxy_v2
29+
//go:noescape
30+
func fastlyHTTPReqRedirectToWebsocketProxyV2(
31+
r requestHandle,
32+
backendData prim.Pointer[prim.U8], backendLen prim.Usize,
33+
) FastlyStatus
34+
35+
func (r *HTTPRequest) HandoffWebsocket(backend string) error {
36+
backendBuffer := prim.NewReadBufferFromString(backend).Wstring()
37+
38+
if err := fastlyHTTPReqRedirectToWebsocketProxyV2(
39+
r.h,
40+
backendBuffer.Data, backendBuffer.Len,
41+
).toError(); err != nil {
42+
return err
43+
}
44+
45+
return nil
46+
}
47+
2748
//go:wasmimport fastly_http_req redirect_to_websocket_proxy
2849
//go:noescape
2950
func fastlyHTTPReqRedirectToWebsocketProxy(
@@ -44,13 +65,34 @@ func HandoffWebsocket(backend string) error {
4465

4566
// witx:
4667
//
47-
// (@interface func (export "redirect_to_grip_proxy")
68+
// (@interface func (export "redirect_to_grip_proxy_v2")
4869
//
70+
// (param $h $request_handle)
4971
// (param $backend_name string)
5072
// (result $err (expected (error $fastly_status)))
5173
//
5274
// )
5375
//
76+
//go:wasmimport fastly_http_req redirect_to_grip_proxy_v2
77+
//go:noescape
78+
func fastlyHTTPReqRedirectToGripProxyV2(
79+
r requestHandle,
80+
backendData prim.Pointer[prim.U8], backendLen prim.Usize,
81+
) FastlyStatus
82+
83+
func (r *HTTPRequest) HandoffFanout(backend string) error {
84+
backendBuffer := prim.NewReadBufferFromString(backend).Wstring()
85+
86+
if err := fastlyHTTPReqRedirectToGripProxyV2(
87+
r.h,
88+
backendBuffer.Data, backendBuffer.Len,
89+
).toError(); err != nil {
90+
return err
91+
}
92+
93+
return nil
94+
}
95+
5496
//go:wasmimport fastly_http_req redirect_to_grip_proxy
5597
//go:noescape
5698
func fastlyHTTPReqRedirectToGripProxy(

x/exp/handoff/handoff.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Deprecated: Use fsthttp.Request.Handoff* methods instead.
12
package handoff
23

34
import "github.com/fastly/compute-sdk-go/internal/abi/fastly"

0 commit comments

Comments
 (0)