diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-08 21:17:06 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-08 21:17:06 +0900 |
| commit | 3b13a61123becc63823ab0c0941aaff2048b020e (patch) | |
| tree | e2e48d8d92e2601d65e3ce7d794d8727026c14e0 /worker | |
| parent | d94c9eb68e65d53f09bf3dd26ec4d61d245882ad (diff) | |
| download | iosdc-japan-2025-albatross-3b13a61123becc63823ab0c0941aaff2048b020e.tar.gz iosdc-japan-2025-albatross-3b13a61123becc63823ab0c0941aaff2048b020e.tar.zst iosdc-japan-2025-albatross-3b13a61123becc63823ab0c0941aaff2048b020e.zip | |
feat(backend/worker): calculate code hash in api-server
Diffstat (limited to 'worker')
| -rw-r--r-- | worker/exec.go | 23 | ||||
| -rw-r--r-- | worker/handlers.go | 5 | ||||
| -rw-r--r-- | worker/models.go | 5 |
3 files changed, 14 insertions, 19 deletions
diff --git a/worker/exec.go b/worker/exec.go index bd49162..9e937f7 100644 --- a/worker/exec.go +++ b/worker/exec.go @@ -3,7 +3,6 @@ package main import ( "bytes" "context" - "crypto/md5" "fmt" "os" "os/exec" @@ -36,10 +35,6 @@ func prepareDirectories() error { return nil } -func calcHash(code string) string { - return fmt.Sprintf("%x", md5.Sum([]byte(code))) -} - func calcFilePath(hash, ext string) string { return fmt.Sprintf("%s/%s/%s.%s", dataRootDir, ext, hash, ext) } @@ -85,11 +80,11 @@ func convertCommandErrorToResultType(err error) string { func execSwiftCompile( ctx context.Context, code string, + codeHash string, maxDuration time.Duration, ) swiftCompileResponseData { - hash := calcHash(code) - inPath := calcFilePath(hash, "swift") - outPath := calcFilePath(hash, "wasm") + inPath := calcFilePath(codeHash, "swift") + outPath := calcFilePath(codeHash, "wasm") if err := os.WriteFile(inPath, []byte(code), 0644); err != nil { return swiftCompileResponseData{ @@ -122,12 +117,11 @@ func execSwiftCompile( func execWasmCompile( ctx context.Context, - code string, + codeHash string, maxDuration time.Duration, ) wasmCompileResponseData { - hash := calcHash(code) - inPath := calcFilePath(hash, "wasm") - outPath := calcFilePath(hash, "cwasm") + inPath := calcFilePath(codeHash, "wasm") + outPath := calcFilePath(codeHash, "cwasm") stdout, stderr, err := execCommandWithTimeout( ctx, @@ -154,12 +148,11 @@ func execWasmCompile( func execTestRun( ctx context.Context, - code string, + codeHash string, stdin string, maxDuration time.Duration, ) testRunResponseData { - hash := calcHash(code) - inPath := calcFilePath(hash, "cwasm") + inPath := calcFilePath(codeHash, "cwasm") stdout, stderr, err := execCommandWithTimeout( ctx, diff --git a/worker/handlers.go b/worker/handlers.go index e7ceef6..ac9701f 100644 --- a/worker/handlers.go +++ b/worker/handlers.go @@ -23,6 +23,7 @@ func handleSwiftCompile(c echo.Context) error { res := execSwiftCompile( c.Request().Context(), req.Code, + req.CodeHash, req.maxDuration(), ) @@ -40,7 +41,7 @@ func handleWasmCompile(c echo.Context) error { res := execWasmCompile( c.Request().Context(), - req.Code, + req.CodeHash, req.maxDuration(), ) @@ -58,7 +59,7 @@ func handleTestRun(c echo.Context) error { res := execTestRun( c.Request().Context(), - req.Code, + req.CodeHash, req.Stdin, req.maxDuration(), ) diff --git a/worker/models.go b/worker/models.go index c60002c..9f60eb0 100644 --- a/worker/models.go +++ b/worker/models.go @@ -19,6 +19,7 @@ var ( type swiftCompileRequestData struct { MaxDurationMilliseconds int `json:"max_duration_ms"` Code string `json:"code"` + CodeHash string `json:"code_hash"` } func (req *swiftCompileRequestData) maxDuration() time.Duration { @@ -40,7 +41,7 @@ type swiftCompileResponseData struct { type wasmCompileRequestData struct { MaxDurationMilliseconds int `json:"max_duration_ms"` - Code string `json:"code"` + CodeHash string `json:"code_hash"` } type wasmCompileResponseData struct { @@ -62,7 +63,7 @@ func (req *wasmCompileRequestData) validate() error { type testRunRequestData struct { MaxDurationMilliseconds int `json:"max_duration_ms"` - Code string `json:"code"` + CodeHash string `json:"code_hash"` Stdin string `json:"stdin"` } |
