gpt4 book ai didi

go - 剥离文件并通过 TCP 将 block 同时写入服务器显示损坏的管道错误

转载 作者:IT王子 更新时间:2023-10-29 01:58:06 24 4
gpt4 key购买 nike

我的客户端将一个文件分成多个 block (每个 block 128mb),然后它会使用 goroutines 同时将 block 上传到多个服务器。

但是,当我使用超过 1 个 goroutine 时,我的客户端程序会出错。

write tcp [::1]:49324->[::1]:2001: write: broken pipe

在我的服务器中,错误是

EOF

请注意,broken pipe 错误和 EOF 错误发生在不同的 block 中。例如,在写入 chunk 1 时可能会发生 broken pipe 错误,而在服务器接收 chunk 2 时可能会发生 EOF 错误。

客户端代码如下:

//set maximum no. of goroutine running in the back
maxGoroutines := 3
guard := make(chan struct{}, maxGoroutines)

var sentByte int64

for i:= 0; i < chunkCount; i += 1{
guard <- struct{}{}

go func(i int){
index := i%len(serverList)
vsConnection, _ := net.Dial("tcp", serverList[index])

sentByte=0
file, _ := os.Open(fileName)
file.Seek(int64(i)*CHUNKSIZE,0) //CHUNKSIZE is 134217728
for {
n, _ := file.Read(sendBuffer)

n2, err2 := vsConnection.Write(sendBuffer[:n])
if err2 != nil {
fmt.Println("err2",err2,chunkName)
}
if(n2!=65536){ //65536 is size of sendBuffer
fmt.Println("n2",n2)
}
sentByte = sentByte+int64(n)
if(sentByte == CHUNKSIZE){
break;
}
}
vsConnection.Close()
file.Close()
<-guard
}(i)
}

服务器代码如下:

func main() {
mapping := cmap.New()
server, error := net.Listen("tcp", ":2001")
if error != nil {
fmt.Println("There was an error starting the server" + error.Error())
return
}

for {
connection, error := server.Accept()
if error != nil {
fmt.Println("There was am error with the connection" + error.Error())
return
}
//one goroutine per connection
go ConnectionHandler(connection,mapping)
}
}

func ConnectionHandler(connection net.Conn, mapping cmap.ConcurrentMap) {
fmt.Println("Connected")
//make a buffer to hold data
var bufferFile bytes.Buffer
writer := bufio.NewWriter(&bufferFile)

var receivedBytes int64
receivedBytes=0
for {

if(CHUNKSIZE<=receivedBytes){
break
}
n,err := io.CopyN(writer, connection, BUFFERSIZE)
receivedBytes += n
if err != nil {
fmt.Println("err", err.Error(), fileName)
break
}
}
mapping.Set(fileName,bufferFile.Bytes())
connection.Close()

}

非常感谢。

最佳答案

在您的客户端中,sentByte 应该是发送方 goroutine 的局部变量。由于您已将其声明为全局变量,因此您的代码中存在竞争条件。尝试以下修复:

go func(i int){
index := i%len(serverList)
vsConnection, _ := net.Dial("tcp", serverList[index])

sentByte := 0 // make sentByte a local variable, so each goroutine
// has its own copy
file, _ := os.Open(fileName)
file.Seek(int64(i)*CHUNKSIZE,0) //CHUNKSIZE is 134217728
for {
n, _ := file.Read(sendBuffer)
// ...

关于go - 剥离文件并通过 TCP 将 block 同时写入服务器显示损坏的管道错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43155017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com