vmware-labs / wasm-workers-server

🚀 Develop and run serverless applications on WebAssembly

Home Page:https://workers.wasmlabs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The Go kit cannot update K/V values

ereslibre opened this issue · comments

Describe the bug

When using the Go kit, it's not possible to update K/V values. An example worker as documented here:

package main

import (
	"fmt"
	"net/http"
	"strconv"

	"github.com/vmware-labs/wasm-workers-server/kits/go/worker"
)

func main() {
	worker.ServeFunc(func(w http.ResponseWriter, r *http.Request) {
		cache, _ := r.Context().Value(worker.CacheKey).(map[string]string)

		var countNum uint32

		if count, ok := cache["counter"]; ok {
			n, _ := strconv.ParseUint(count, 10, 32)
			countNum = uint32(n)
		}

		body := fmt.Sprintf("<!DOCTYPE html>"+
			"<body>"+
			"<h1>Key / Value store in Go</h1>"+
			"<p>Counter: %d</p>"+
			"<p>This page was generated by a Wasm module built from Go.</p>"+
			"</body>", countNum)

		cache["counter"] = fmt.Sprintf("%d", countNum+1)

		w.Header().Set("x-generated-by", "wasm-workers-server")
		w.Write([]byte(body))
	})
}

Won't update the cache counter number. The Go Kit should have functions that allow inserting values into the K/V store, as the Rust Kit exposes.

Expected behavior

It's possible to update the K/V store from a Go worker.

Additional context

No response

@ereslibre I updated the label to bug as this is an expected behavior from the kit :)

@ereslibre Are you saying that it's not possible to increase the counter via browser access? I tried using a browser and found that I can increase the count normally.

image

Just double checked the example program that triggered this bug report. It was lacking the cache update:

diff --git a/docker-wasm/apps-src/user-generation-go/main.go b/docker-wasm/apps-src/user-generation-go/main.go
index 0d27c232..5b7175a2 100644
--- a/docker-wasm/apps-src/user-generation-go/main.go
+++ b/docker-wasm/apps-src/user-generation-go/main.go
@@ -57,11 +57,12 @@ func main() {
                }
                fileContents := string(fileContents_)

-               generatedUserCount := uint32(0)
+               generatedUserCount := uint32(1)
                if count, ok := cache["generated_users_counter"]; ok {
                        n, _ := strconv.ParseUint(count, 10, 32)
-                       generatedUserCount = uint32(n)
+                       generatedUserCount = uint32(n) + 1
                }
+               cache["generated_users_counter"] = fmt.Sprintf("%d", generatedUserCount)

                responseData := ResponseData{
                        User:             user

Closing, sorry for the noise.