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

Opt: split large packet to n*16KB packet #82

Merged
merged 10 commits into from
Nov 17, 2021

Conversation

Lvnszn
Copy link

@Lvnszn Lvnszn commented Oct 30, 2021

What this PR does:
split large packet to 16KB packet and send one by one for reduce risk when send large packet.
recv will compact small packet in a large one.
Which issue(s) this PR fixes:

Fixes #79

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


session.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Oct 30, 2021

Codecov Report

Merging #82 (a3f9370) into master (a2461f8) will increase coverage by 0.27%.
The diff coverage is 81.81%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #82      +/-   ##
==========================================
+ Coverage   68.87%   69.14%   +0.27%     
==========================================
  Files           8        8              
  Lines        1420     1439      +19     
==========================================
+ Hits          978      995      +17     
- Misses        347      348       +1     
- Partials       95       96       +1     
Impacted Files Coverage Δ
session.go 69.83% <81.81%> (+0.38%) ⬆️
connection.go 80.39% <0.00%> (+0.65%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a2461f8...a3f9370. Read the comment docs.

session.go Outdated
writeSize += maxPacketLen
}
pkgs = append(pkgs, pkg[writeSize:])
lg, err := s.Connection.send(pkgs)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u need not to change the codes so quickly. pls test it firstly.

@AlexStocks
Copy link

pr 我看了,没多大毛病。关键还是发送大包时,要不要加锁的问题。

@Lvnszn Lvnszn changed the title WIP: Opt: split large packet to n*16KB packet Opt: split large packet to n*16KB packet Nov 9, 2021
session.go Outdated
@@ -129,6 +130,7 @@ type session struct {
// goroutines sync
grNum uatomic.Int32
lock sync.RWMutex
wlock sync.Mutex

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u should use this wlock before every write action/funcs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

session.go Outdated
totalSize -= maxPacketLen
writeSize += maxPacketLen
}
lg, err := s.Connection.send(pkg[writeSize:])
Copy link

@wongoo wongoo Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lvnszn does it need to send if the totalSize = 0?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the packet needn't to be send. Addressed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lvnszn yes, should return if totalSize already decreased to 0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

session.go Outdated
writeSize := 0
s.wlock.Lock()
defer s.wlock.Unlock()
for totalSize >= maxPacketLen {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totalSize change to leftPackageSize or packageSize should be more readable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed.

session.go Outdated
}
return lg, nil

return writeSize + lg, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here can return totalsize directly ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a var for return, PTAL.

session.go Outdated
@@ -418,11 +422,28 @@ func (s *session) WriteBytes(pkg []byte) (int, error) {
return 0, ErrSessionClosed
}

lg, err := s.Connection.send(pkg)
leftPackageSize, totalSize, writeSize := len(pkg), len(pkg), 0
s.wlock.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, the wlock used for protect big package send, so may be u can change the wlock to rwMutex, and only when package size bigger than 16kb, try to get Lock, other times and other places you can use Rlock.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address

lg, err := s.Connection.send(pkg)
leftPackageSize, totalSize, writeSize := len(pkg), len(pkg), 0
if leftPackageSize >= maxPacketLen {
s.packetLock.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only leftPackageSize > maxPacketLen need lock, equal not needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address

Copy link
Member

@wenxuwan wenxuwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AlexStocks AlexStocks merged commit 54a2fad into apache:master Nov 17, 2021
@Lvnszn Lvnszn deleted the feature/split branch November 17, 2021 05:30
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

Successfully merging this pull request may close these issues.

Send large package safely
6 participants