Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Write multiple packages at once #42

Closed
jococi opened this issue Jun 22, 2020 · 3 comments
Closed

Bug: Write multiple packages at once #42

jococi opened this issue Jun 22, 2020 · 3 comments
Assignees

Comments

@jococi
Copy link

jococi commented Jun 22, 2020

`// Write multiple packages at once. so we invoke write sys.call just one time.
func (s *session) WriteBytesArray(pkgs ...[]byte) error {
if s.IsClosed() {
return ErrSessionClosed
}
// s.conn.SetWriteTimeout(time.Now().Add(s.wTimeout))
if len(pkgs) == 1 {
// return s.Connection.Write(pkgs[0])
return s.WriteBytes(pkgs[0])
}

// reduce syscall and memcopy for multiple packages
if _, ok := s.Connection.(*gettyTCPConn); ok {
       // 第一次发送
	if _, err := s.Connection.send(pkgs); err != nil {
		return jerrors.Annotatef(err, "s.Connection.Write(pkgs num:%d)", len(pkgs))
	}
}

// get len
var (
	l      int
	err    error
	length int
	arrp   *[]byte
	arr    []byte
)
length = 0
for i := 0; i < len(pkgs); i++ {
	length += len(pkgs[i])
}

// merge the pkgs
//arr = make([]byte, length)
arrp = gxbytes.GetBytes(length)
defer gxbytes.PutBytes(arrp)
arr = *arrp

l = 0
for i := 0; i < len(pkgs); i++ {
	copy(arr[l:], pkgs[i])
	l += len(pkgs[i])
}
 // 第二次发送
if err = s.WriteBytes(arr); err != nil {
	return jerrors.Trace(err)
}

num := len(pkgs) - 1
for i := 0; i < num; i++ {
	s.incWritePkgNum()
}

return nil

}
`

TCP模式下,pkgs数量超过1,那么数据包会被发送两次

@watermelo watermelo self-assigned this Jun 22, 2020
@watermelo
Copy link
Collaborator

@jococi 感谢老哥,已修复 #44

@AlexStocks AlexStocks changed the title 重复发包 Bug: Write multiple packages at once Jun 23, 2020
@AlexStocks
Copy link
Owner

AlexStocks commented Jun 23, 2020

@jococi I have merged @watermelo 's pr and release a new version v1.2.9 . Pls test it and give us a feedback. thx.

@jococi jococi closed this as completed Jun 23, 2020
@AlexStocks
Copy link
Owner

@jococi would u like to register ur org info? If so, pls register it at #40.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants