From 3b13a61123becc63823ab0c0941aaff2048b020e Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 8 Aug 2024 21:17:06 +0900 Subject: feat(backend/worker): calculate code hash in api-server --- worker/exec.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'worker/exec.go') 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, -- cgit v1.2.3-70-g09d2