diff --git a/cmd/api/main.go b/cmd/api/main.go index 7cf9b0a..4827f4a 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -55,13 +55,13 @@ func main() { } func seedEndpoint(store storage.Store, cache storage.ModCacher) { - b, err := os.ReadFile("examples/js/index.js") + b, err := os.ReadFile("examples/go/app.wasm") if err != nil { log.Fatal(err) } endpoint := &types.Endpoint{ ID: uuid.MustParse("09248ef6-c401-4601-8928-5964d61f2c61"), - Runtime: "js", + Runtime: "go", Name: "Catfact parser", Environment: map[string]string{"FOO": "bar"}, CreatedAT: time.Now(), diff --git a/cmd/load/main.go b/cmd/load/main.go index 47dedf5..d15dd23 100644 --- a/cmd/load/main.go +++ b/cmd/load/main.go @@ -2,20 +2,27 @@ package main import ( "fmt" - "io" "log" "net/http" "time" ) func main() { - for { - makeRequest() - time.Sleep(time.Millisecond * 50) + n := 100 + for i := 0; i < n; i++ { + go func() { + for { + makeRequest() + time.Sleep(time.Millisecond * 100) + } + }() + time.Sleep(time.Millisecond * 100) } + time.Sleep(time.Second * 10) } func makeRequest() { + start := time.Now() req, err := http.NewRequest("get", "http://localhost:5000/live/09248ef6-c401-4601-8928-5964d61f2c61", nil) if err != nil { log.Fatal(err) @@ -24,11 +31,8 @@ func makeRequest() { if err != nil { log.Fatal(err) } - b, err := io.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) + if resp.StatusCode != http.StatusOK { + log.Fatal("status", resp.StatusCode) } - defer resp.Body.Close() - - fmt.Println(string(b)) + fmt.Println(time.Since(start)) } diff --git a/cmd/wasmserver/main.go b/cmd/wasmserver/main.go index f1bd5ca..da93362 100644 --- a/cmd/wasmserver/main.go +++ b/cmd/wasmserver/main.go @@ -10,7 +10,6 @@ import ( "github.com/anthdm/hollywood/actor" "github.com/anthdm/hollywood/cluster" - "github.com/anthdm/hollywood/remote" "github.com/anthdm/raptor/internal/actrs" "github.com/anthdm/raptor/internal/config" "github.com/anthdm/raptor/internal/storage" @@ -44,19 +43,14 @@ func main() { metricStore = store ) - remote := remote.New(config.Get().Cluster.WasmMemberAddr, nil) - engine, err := actor.NewEngine(&actor.EngineConfig{ - Remote: remote, - }) + clusterConfig := cluster.NewConfig(). + WithListenAddr(config.Get().Cluster.Address). + WithRegion(config.Get().Cluster.Region). + WithID(config.Get().Cluster.ID) + c, err := cluster.New(clusterConfig) if err != nil { log.Fatal(err) } - c, err := cluster.New(cluster.Config{ - Region: config.Get().Cluster.Region, - Engine: engine, - ID: config.Get().Cluster.ID, - ClusterProvider: cluster.NewSelfManagedProvider(), - }) c.RegisterKind(actrs.KindRuntime, actrs.NewRuntime(store, modCache), &cluster.KindConfig{}) c.Engine().Spawn(actrs.NewMetric, actrs.KindMetric, actor.WithID("1")) c.Start() diff --git a/examples/go/main.go b/examples/go/main.go index bec0a86..461718f 100644 --- a/examples/go/main.go +++ b/examples/go/main.go @@ -4,7 +4,6 @@ import ( "net/http" raptor "github.com/anthdm/raptor/sdk" - "github.com/go-chi/chi" ) func handleLogin(w http.ResponseWriter, r *http.Request) { @@ -18,8 +17,8 @@ func handleDashboard(w http.ResponseWriter, r *http.Request) { } func main() { - router := chi.NewMux() - router.Get("/dashboard", handleDashboard) - router.Get("/login", handleLogin) - raptor.Handle(router) + // router := chi.NewMux() + // router.Get("/dashboard", handleDashboard) + // router.Get("/login", handleLogin) + raptor.Handle(http.HandlerFunc(handleLogin)) } diff --git a/go.mod b/go.mod index 1b21e97..0746bf6 100644 --- a/go.mod +++ b/go.mod @@ -3,36 +3,39 @@ module github.com/anthdm/raptor go 1.21.0 require ( - github.com/anthdm/hollywood v0.0.0-20231230200740-54133c9bd2b4 + github.com/anthdm/hollywood v0.0.0-20240101185755-da5c2fd388a9 github.com/go-chi/chi/v5 v5.0.11 github.com/google/uuid v1.5.0 github.com/lib/pq v1.10.9 - github.com/redis/go-redis/v9 v9.3.1 github.com/stealthrocket/net v0.2.1 + github.com/stretchr/testify v1.8.4 github.com/tetratelabs/wazero v1.6.0 - github.com/vmihailenco/msgpack/v5 v5.4.1 google.golang.org/protobuf v1.30.0 ) require ( - github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/DataDog/gostackparse v0.7.0 // indirect + github.com/cenkalti/backoff v2.2.1+incompatible // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.3 // indirect + github.com/grandcat/zeroconf v1.0.0 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect + github.com/miekg/dns v1.1.27 // indirect github.com/planetscale/vtprotobuf v0.4.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/zeebo/errs v1.2.2 // indirect github.com/zeebo/xxh3 v1.0.2 // indirect + golang.org/x/crypto v0.14.0 // indirect golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect golang.org/x/text v0.13.0 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect google.golang.org/grpc v1.53.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect storj.io/drpc v0.0.32 // indirect ) require ( - github.com/go-chi/chi v1.5.5 github.com/pelletier/go-toml/v2 v2.1.1 - github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.14.0 // indirect ) diff --git a/go.sum b/go.sum index 6d30486..9f39f74 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,12 @@ -github.com/anthdm/hollywood v0.0.0-20231230200740-54133c9bd2b4 h1:AYMeagFMr0z4bZua2WwjZuxrSeNmzL/f+vqovBYlSyo= -github.com/anthdm/hollywood v0.0.0-20231230200740-54133c9bd2b4/go.mod h1:IIfczICTbLpVKJS97qqxVw7S3LiKHJbnsE9LFwgtta0= -github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= -github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= -github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= -github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/DataDog/gostackparse v0.7.0 h1:i7dLkXHvYzHV308hnkvVGDL3BR4FWl7IsXNPz/IGQh4= +github.com/DataDog/gostackparse v0.7.0/go.mod h1:lTfqcJKqS9KnXQGnyQMCugq3u1FP6UZMfWR0aitKFMM= +github.com/anthdm/hollywood v0.0.0-20240101185755-da5c2fd388a9 h1:c51Qh4Yw0uxbHYk3XJ2Ao58hFtO0L4Pq+U6+pCaYWxs= +github.com/anthdm/hollywood v0.0.0-20240101185755-da5c2fd388a9/go.mod h1:xDsfWspEY/ssG4bmHYFHTp2ts2q+6M0QbAXDS1J2Jss= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= 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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= -github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= -github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -23,47 +17,65 @@ 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/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grandcat/zeroconf v1.0.0 h1:uHhahLBKqwWBV6WZUDAT71044vwOTL+McW0mBJvo6kE= +github.com/grandcat/zeroconf v1.0.0/go.mod h1:lTKmG1zh86XyCoUeIHSA4FJMBwCJiQmGfcP2PdzytEs= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/miekg/dns v1.1.27 h1:aEH/kqUzUxGJ/UHcEKdJY+ugH6WEzsEBBSPa8zuy1aM= +github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/planetscale/vtprotobuf v0.4.0 h1:NEI+g4woRaAZgeZ3sAvbtyvMBRjIv5kE7EWYQ8m4JwY= github.com/planetscale/vtprotobuf v0.4.0/go.mod h1:wm1N3qk9G/4+VM1WhpkLbvY/d8+0PbwYYpP5P5VhTks= 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/redis/go-redis/v9 v9.3.1 h1:KqdY8U+3X6z+iACvumCNxnoluToB+9Me+TvyFa21Mds= -github.com/redis/go-redis/v9 v9.3.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/stealthrocket/net v0.2.1 h1:PehPGAAjuV46zaeHGlNgakFV7QDGUAREMcEQsZQ8NLo= github.com/stealthrocket/net v0.2.1/go.mod h1:VvoFod9pYC9mo+bEg2NQB/D+KVOjxfhZjZ5zyvozq7M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tetratelabs/wazero v1.6.0 h1:z0H1iikCdP8t+q341xqepY4EWvHEw8Es7tlqiVzlP3g= github.com/tetratelabs/wazero v1.6.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A= -github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= -github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= -github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= -github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= github.com/zeebo/errs v1.2.2 h1:5NFypMTuSdoySVTqlNs1dEoU21QVamMQJxW/Fii5O7g= github.com/zeebo/errs v1.2.2/go.mod h1:sgbWHsvVuTPHcqJJGQ1WhI5KbWlHYz+2+2C/LSEtCw4= github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0= github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4= golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w= @@ -74,6 +86,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +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.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/actrs/wasmserver.go b/internal/actrs/wasmserver.go index 1815d16..dd715e7 100644 --- a/internal/actrs/wasmserver.go +++ b/internal/actrs/wasmserver.go @@ -81,7 +81,7 @@ func (s *WasmServer) initialize(c *actor.Context) { } func (s *WasmServer) sendRequestToRuntime(req *proto.HTTPRequest) { - pid := s.cluster.Activate(KindRuntime, &cluster.ActivationConfig{}) + pid := s.cluster.Activate(KindRuntime, cluster.NewActivationConfig()) s.cluster.Engine().SendWithSender(pid, req, s.self) } diff --git a/internal/config/config.go b/internal/config/config.go index 3c7cad6..b92103c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,7 +16,7 @@ apiToken = "foobarbaz" authorization = false [cluster] -addr = "localhost:6666" +address = "localhost:6666" id = "wasm_member_1" region = "eu-west" @@ -42,9 +42,9 @@ type Storage struct { } type Cluster struct { - WasmMemberAddr string - ID string - Region string + Address string + ID string + Region string } type Config struct { diff --git a/internal/runtime/runtime_test.go b/internal/runtime/runtime_test.go new file mode 100644 index 0000000..3441bc5 --- /dev/null +++ b/internal/runtime/runtime_test.go @@ -0,0 +1,42 @@ +package runtime + +import ( + "bytes" + "context" + "fmt" + "os" + "testing" + + "github.com/anthdm/raptor/internal/shared" + "github.com/anthdm/raptor/proto" + "github.com/stretchr/testify/require" + "github.com/tetratelabs/wazero" + pb "google.golang.org/protobuf/proto" +) + +func TestRuntimeInvoke(t *testing.T) { + b, err := os.ReadFile("../../examples/go/app.wasm") + require.Nil(t, err) + ctx := context.Background() + + out := &bytes.Buffer{} + req := &proto.HTTPRequest{ + Method: "get", + URL: "/", + } + reqb, err := pb.Marshal(req) + require.Nil(t, err) + + args := InvokeArgs{ + Cache: wazero.NewCompilationCache(), + Env: map[string]string{}, + Blob: b, + Out: out, + In: bytes.NewReader(reqb), + } + err = Invoke(ctx, args) + require.Nil(t, err) + resp, status, err := shared.ParseRuntimeHTTPResponse(out.String()) + fmt.Println(resp) + fmt.Println(status) +} diff --git a/internal/shared/shared.go b/internal/shared/shared.go index ced3d30..31432e6 100644 --- a/internal/shared/shared.go +++ b/internal/shared/shared.go @@ -1,24 +1,33 @@ package shared import ( + "encoding/binary" + "encoding/hex" + "errors" "fmt" "io" "net/http" "net/url" - "strconv" "strings" "github.com/anthdm/raptor/proto" ) +var errInvalidHTTPResponse = errors.New("invalid HTTP response") + func ParseRuntimeHTTPResponse(in string) (resp string, status int, err error) { - lines := strings.Split(in, "\n") - if len(lines) < 3 { - err = fmt.Errorf("invalid response") + if len(in) < 16 { + err = fmt.Errorf("misformed HTTP response missing last 16 bytes") return } - resp = lines[len(lines)-3] - status, err = strconv.Atoi(lines[len(lines)-2]) + var b []byte + b, err = hex.DecodeString(in[len(in)-16:]) + if err != nil { + err = errInvalidHTTPResponse + } + status = int(binary.LittleEndian.Uint32(b[0:4])) + respLen := binary.LittleEndian.Uint32(b[4:8]) + resp = in[len(in)-16-int(respLen) : len(in)-16] return } diff --git a/internal/shared/shared_test.go b/internal/shared/shared_test.go new file mode 100644 index 0000000..89c9da8 --- /dev/null +++ b/internal/shared/shared_test.go @@ -0,0 +1,27 @@ +package shared + +import ( + "encoding/binary" + "encoding/hex" + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestParseRuntimeHTTPResponse(t *testing.T) { + t.Run("multiline response", func(t *testing.T) { + text := "This is the best.\nBut not always correct.\nThe big brown fox." + statusCode := uint32(500) + + buf := make([]byte, 8) + binary.LittleEndian.PutUint32(buf[0:4], statusCode) + binary.LittleEndian.PutUint32(buf[4:8], uint32(len(text))) + + in := fmt.Sprintf("%s%s", text, hex.EncodeToString(buf)) + resp, status, err := ParseRuntimeHTTPResponse(in) + require.Nil(t, err) + require.Equal(t, int(statusCode), status) + require.Equal(t, text, resp) + }) +} diff --git a/sdk/sdk.go b/sdk/sdk.go index 1fe7789..7631c93 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -2,12 +2,13 @@ package run import ( "bytes" + "encoding/binary" + "encoding/hex" "fmt" "io" "log" "net/http" "os" - "strings" "github.com/anthdm/raptor/proto" // _ "github.com/stealthrocket/net/http" @@ -45,9 +46,12 @@ func Handle(h http.Handler) { r.Header[k] = v.Fields } h.ServeHTTP(w, r) // execute the user's handler - out := strings.TrimRight(w.buffer.String(), "\n") - fmt.Println(out) - fmt.Println(w.statusCode) + fmt.Print(w.buffer.String()) + + buf := make([]byte, 8) + binary.LittleEndian.PutUint32(buf[0:4], uint32(w.statusCode)) + binary.LittleEndian.PutUint32(buf[4:8], uint32(w.buffer.Len())) + fmt.Print(hex.EncodeToString(buf)) } type ResponseWriter struct {