-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Achille Roussel <[email protected]>
- Loading branch information
1 parent
be47365
commit 8087b8a
Showing
5 changed files
with
157 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !durable | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//go:build durable | ||
|
||
package main | ||
|
||
import ( | ||
http "net/http" | ||
coroutine "github.com/stealthrocket/coroutine" | ||
fmt "fmt" | ||
) | ||
import _types "github.com/stealthrocket/coroutine/types" | ||
|
||
type yieldingRoundTripper struct { | ||
} | ||
//go:noinline | ||
func RoundTrip(req *http.Request) (_ *http.Response, _ error) { | ||
_c := coroutine.LoadContext[*http.Request, *http.Response]() | ||
_f, _fp := _c.Push() | ||
var _f0 *struct { | ||
X0 *http.Request | ||
X1 *http.Response | ||
} | ||
if _f.IP == 0 { | ||
_f0 = &struct { | ||
X0 *http.Request | ||
X1 *http.Response | ||
}{X0: req} | ||
} else { | ||
_f0 = _f.Get(0).(*struct { | ||
X0 *http.Request | ||
X1 *http.Response | ||
}) | ||
} | ||
defer func() { | ||
if _c.Unwinding() { | ||
_f.Set(0, _f0) | ||
_c.Store(_fp, _f) | ||
} else { | ||
_c.Pop() | ||
} | ||
}() | ||
switch { | ||
case _f.IP < 2: | ||
_f0.X1 = coroutine.Yield[*http.Request, *http.Response](_f0.X0) | ||
_f.IP = 2 | ||
fallthrough | ||
case _f.IP < 3: | ||
return _f0.X1, nil | ||
} | ||
return | ||
} | ||
//go:noinline | ||
func work() { | ||
_c := coroutine.LoadContext[*http.Request, *http.Response]() | ||
_f, _fp := _c.Push() | ||
var _f0 *struct { | ||
X0 *http.Response | ||
X1 error | ||
} | ||
if _f.IP == 0 { | ||
_f0 = &struct { | ||
X0 *http.Response | ||
X1 error | ||
}{} | ||
} else { | ||
_f0 = _f.Get(0).(*struct { | ||
X0 *http.Response | ||
X1 error | ||
}) | ||
} | ||
defer func() { | ||
if _c.Unwinding() { | ||
_f.Set(0, _f0) | ||
_c.Store(_fp, _f) | ||
} else { | ||
_c.Pop() | ||
} | ||
}() | ||
switch { | ||
case _f.IP < 2: | ||
_f0.X0, _f0.X1 = http.Get("http://example.com") | ||
_f.IP = 2 | ||
fallthrough | ||
case _f.IP < 3: | ||
if _f0.X1 != nil { | ||
panic(_f0.X1) | ||
} | ||
_f.IP = 3 | ||
fallthrough | ||
case _f.IP < 4: | ||
fmt.Println(_f0.X0.StatusCode) | ||
} | ||
} | ||
func main() { | ||
http.DefaultTransport = &yieldingRoundTripper{} | ||
c := coroutine.New[*http.Request, *http.Response](work) | ||
for c.Next() { | ||
req := c.Recv() | ||
fmt.Println("Requesting", req.URL.String()) | ||
c.Send(&http.Response{StatusCode: 200}) | ||
} | ||
} | ||
func init() { | ||
_types.RegisterFunc[func(req *http.Request) (_ *http.Response, _ error)]("github.com/stealthrocket/coroutine/compiler/testdata/http.RoundTrip") | ||
_types.RegisterFunc[func()]("github.com/stealthrocket/coroutine/compiler/testdata/http.main") | ||
_types.RegisterFunc[func()]("github.com/stealthrocket/coroutine/compiler/testdata/http.work") | ||
} |