我正在使用 Go 创建一个服务器,它允许客户端上传文件,然后使用服务器函数来解析文件。目前,我正在使用两个单独的请求:1)第一个请求发送用户上传的文件 2)第二个请求将参数发送到服务器需要解析文件的服务器。
但是,我意识到由于程序的性质,如果多个用户尝试同时使用服务器,可能会出现并发问题。我的解决方案是使用互斥锁。但是,我正在接收文件,发送响应,然后接收参数,当互斥锁被锁定时,Go 似乎无法发回响应。我正在考虑通过在一个 HTTP 请求中同时发送文件和参数来解决这个问题。有没有办法做到这一点?谢谢
示例代码(仅相关部分):
从客户端发送文件的代码:
handleUpload() {
const data = new FormData()
for(var x = 0; x < this.state.selectedFile.length; x++) {
data.append('myFile', this.state.selectedFile[x])
}
var self = this;
let url = *the appropriate url*
axios.post(url, data, {})
.then(res => {
//other logic
self.handleParser();
})
}
handleParser() 的代码:
handleNessusParser(){
let parserParameter = {
SourcePath : location,
ProjectName : this.state.projectName
}
// fetch the the response from the server
let self = this;
let url = *url*
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(parserParameter),
}).then( (response) => {
if(response.status === 200) {
//success logic
}
}).catch (function (error) {
console.log("error: ", error);
});
}