Skip to content

Commit

Permalink
fixed type
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Jan 3, 2024
1 parent 3a4b951 commit eec9be2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 40 deletions.
21 changes: 3 additions & 18 deletions examples/go/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
package main

import (
"fmt"
"math/rand"
"net/http"

ffaas "github.com/anthdm/ffaas/sdk"
"github.com/go-chi/chi"
)

func handleHome(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("x-request-id")
w.Write([]byte(requestID))
}

func handleLogin(w http.ResponseWriter, r *http.Request) {
num := rand.Intn(100)
fmt.Println(r.URL)
w.Write([]byte(fmt.Sprintf("from /login => %d", num)))
}

func main() {
router := chi.NewMux()
router.Get("/", handleHome)
router.Get("/login", handleLogin)
ffaas.Handle(router)
ffaas.Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("all good!"))
}))
}
26 changes: 9 additions & 17 deletions pkg/actrs/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log/slog"
"net/http"
"strconv"
"time"

"github.com/anthdm/ffaas/pkg/storage"
Expand Down Expand Up @@ -97,10 +98,11 @@ func (r *Runtime) exec(ctx context.Context, blob []byte, cache wazero.Compilatio
slog.Warn("compiling module failed", "err", err)
return
}
fd := -1
fd := -1 // TODO: for capturing logs..
requestLen := strconv.Itoa(len(httpmod.requestBytes))
builder := imports.NewBuilder().
WithName("ffaas").
WithArgs().
WithName("run").
WithArgs(requestLen).
WithStdio(fd, fd, fd).
WithEnv(envMapToSlice(env)...).
// TODO: we want to mount this to some virtual folder?
Expand All @@ -115,16 +117,16 @@ func (r *Runtime) exec(ctx context.Context, blob []byte, cache wazero.Compilatio
var system wasi.System
ctx, system, err = builder.Instantiate(ctx, runtime)
if err != nil {
slog.Warn("failed to instanciate wasi module", "err", err)
slog.Warn("failed to instantiate wasi module", "err", err)
return
}
defer system.Close(ctx)

httpmod.Instanciate(ctx, runtime)
httpmod.Instantiate(ctx, runtime)

_, err = runtime.InstantiateModule(ctx, mod, wazero.NewModuleConfig())
if err != nil {
slog.Warn("failed to instanciate guest module", "err", err)
slog.Warn("failed to instantiate guest module", "err", err)
}
}

Expand Down Expand Up @@ -158,11 +160,8 @@ func (r *RequestModule) WriteResponse(w io.Writer) (int, error) {
return w.Write(r.responseBytes)
}

func (r *RequestModule) Instanciate(ctx context.Context, runtime wazero.Runtime) error {
func (r *RequestModule) Instantiate(ctx context.Context, runtime wazero.Runtime) error {
_, err := runtime.NewHostModuleBuilder("env").
NewFunctionBuilder().
WithGoModuleFunction(r.moduleMalloc(), []wapi.ValueType{}, []wapi.ValueType{wapi.ValueTypeI32}).
Export("malloc").
NewFunctionBuilder().
WithGoModuleFunction(r.moduleWriteRequest(), []wapi.ValueType{wapi.ValueTypeI32}, []wapi.ValueType{}).
Export("write_request").
Expand All @@ -179,13 +178,6 @@ func (r *RequestModule) Close(ctx context.Context) error {
return nil
}

func (r *RequestModule) moduleMalloc() wapi.GoModuleFunc {
return func(ctx context.Context, module wapi.Module, stack []uint64) {
size := uint64(len(r.requestBytes))
stack[0] = uint64(wapi.DecodeU32(size))
}
}

func (r *RequestModule) moduleWriteRequest() wapi.GoModuleFunc {
return func(ctx context.Context, module wapi.Module, stack []uint64) {
offset := wapi.DecodeU32(stack[0])
Expand Down
13 changes: 8 additions & 5 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"log"
"net/http"
"os"
"strconv"
"unsafe"

"github.com/anthdm/ffaas/proto"
Expand All @@ -23,10 +25,6 @@ type request struct {
URL string
}

//go:wasmimport env malloc
//go:noescape
func malloc() uint32

//go:wasmimport env write_request
//go:noescape
func writeRequest(ptr uint32)
Expand All @@ -36,7 +34,12 @@ func writeRequest(ptr uint32)
func writeResponse(ptr uint32, size uint32)

func Handle(h http.Handler) {
requestSize := malloc()
// the spec makes sure the size of the incoming request is passed as args
// to skip invocations. [run, len]
requestSize, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal(err)
}
requestBuffer = make([]byte, requestSize)

ptr := &requestBuffer[0]
Expand Down

0 comments on commit eec9be2

Please sign in to comment.