Repository navigation
Refactor: Start using filexfer - #432
puellanivis wants to merge 28 commits into
Conversation
|
It’s a spring cleaning, some touch up (came across a few todos that I have good experience in and could roll it out really quick while I was there already) along with the refactor. So far, I’ve mostly only refactored the client side so far. There have been some changes to the server code to remove some utility functions that were mostly used by the client code, so it was more of a cleanup rather than a focused refactor. I’ve been sure to keep the code working on both sides the whole way, so I’m thinking that since this will probably already be a pretty chonky PR, I’ll push the client refactor first, then we can do the server side work in a separate PR. |
a5a8a59 to
c2d5f8e
Compare
|
From: to: |
|
benchstats: |
puellanivis
left a comment
There was a problem hiding this comment.
I don’t just thoroughly code-review other people’s code, I also nitpick my own!
| // XXX_UnmarshalByFlags uses the pre-existing a.Flags field to determine which fields to decode. | ||
| // DO NOT USE THIS: it is an anti-corruption function to implement existing internal usage in pkg/sftp. | ||
| // This function is not a part of any compatibility promise. | ||
| func (a *Attributes) XXX_UnmarshalByFlags(flags uint32, b *Buffer) (err error) { |
There was a problem hiding this comment.
So, in request-attrs.go the code is called here, and unfortunately in the Request exported object, there is the Flags field fully parsed out, but the Attr field is the attributes data in undecoded binary.
I think fixing Request to hold a rich attributes object would likely end up breaking some compatibility somwhere. So, I had to break up the UmarshalFrom for this packet to allow for taking Request.Flags, Request..Attrs as parameters. But we also don’t want to advertise this function as something people should be using, so thus the XXX_ prefix, which despite breaking the linter, is common to indicate that a field/function is inappropriate to be relied upon.
| // | ||
| // ExtendedPacket types will often only implement this interface, | ||
| // since decoding the whole packet body of an ExtendedPacket can only be done dependent on the ExtendedRequest field. | ||
| type PacketMarshaller interface { |
There was a problem hiding this comment.
I had to break up the marshalling from the unmarshalling. The ExtendedPacket implementations have enough information to marshal themselves like any other packet, but they simply cannot know ahead of time to be the decoding packet before the ExtendedRequest string has been read, and then it’s kind of too late to switch to a different unmarshaling receiver.
| if len(p.Data) < len(data) { | ||
| p.Data = make([]byte, len(data)) | ||
| } | ||
|
|
||
| n := copy(p.Data, data) | ||
| p.Data = p.Data[:n] |
There was a problem hiding this comment.
This is actually one of the biggest memory improvements right here. readChunkAt can now give the buffer to read into directly into the packet handling code, and it will be copied from here directly into that buffer, with even less middleman. This also allows everyone to be confident about ownership and when those buffers can be returned to a bufPool which also means more reusable memory, and less allocations.
| r := <-ch | ||
| if r.err != nil { | ||
| t.Fatal("unexpected error:", r.err) | ||
| } |
There was a problem hiding this comment.
Since I changed entirely how this response handling code was written, this test had to be pretty much rewritten from the ground up.
I think I’ve made the test case much more strict, while still being properly behaved. Hopefully that will make it more reliable? (There is of course still the caveat on needing to add a time delay on the Open so…)
| case sshfx.ModeSocket: | ||
| ret |= os.ModeSocket | ||
| default: | ||
| ret |= os.ModeIrregular |
There was a problem hiding this comment.
Hm, this might not be around in the minimum version we’re supporting?
As of go1.16, the good news is that they do actually define the fs.ModeSetuid fs.ModeSetguid and fs.ModeSticky flags for all architectures, regardless of their support. That should fortunately simplify a lot of this code.
There was a problem hiding this comment.
This was introduced in 1.11: https://golang.org/doc/go1.11#os
| const ( | ||
| s_ISUID = 04000 | ||
| s_ISGID = 02000 | ||
| S_ISVTX = 01000 |
There was a problem hiding this comment.
This value was accidentally exported. Hopefully, the Plan9 community was not using it… 😬
There was a problem hiding this comment.
They couldn't be, because this actually breaks the build on Plan 9 (the s_ version isn't defined). My bad, I did build with GOOS=plan9 at some point but apparently I forgot to do that with the final version.
There was a problem hiding this comment.
I noticed a few cases where GOOS=plan9 go build passed without issue just because cgo wasn’t enabled, and so it “worked around” the issue. 🤦♀️
| const ( | ||
| s_ISUID = syscall.S_ISUID | ||
| s_ISGID = syscall.S_ISGID | ||
| s_ISVTX = syscall.S_ISVTX | ||
| ) |
There was a problem hiding this comment.
We’re switching to use the sshfx defined values now, so it doesn’t matter what the local syscall values are. That they just happen to line up is now more of a happy coincidence than any kind of future-forward requirement. What with fs.ModeXYZ being genericized and applied to all archs and oses, things should get easier for us in general.
| import sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer" | ||
|
|
||
| const S_IFMT = syscall.S_IFMT | ||
| const S_IFMT = sshfx.ModeType |
There was a problem hiding this comment.
There’s now no need to have two copies of this an only pull it in based on GOOS/GOARCH pairs that have a bad value.
We do export the value though, and it was intended to be used, so we’re keeping it exported. At any possible v2, we can remove it in light of using the ssh/filexfer.ModeType value along with its brethren directly.
|
This looks like a great work, thank you |
|
I was just looking at the memory profile from the new version: 😆 8.66% of the allocations are from a packet that is sent only once per connection! |
…ke semantic sense
3097d2b to
f71e354
Compare
|
Converting to draft, as there’s a lot of merge conflicts that have to be resolved… |
|
I’m going to go ahead and close this. There’s so many conflicts now that it’s probably best to just restart. (The PR will be kept around as a reference anyways, so, no point in keeping it in an “open” state.) |
So, I’m checking in and pushing a PR very early here, so that others can get a sense of how the client refactor will look like after integrating the filexfer.
The early attention here should be on the
Client.close()function, which felt would be the quickest way to show off how things would change. The changes toConnandclientConnare to support the changes in theclose()function, and allow for a parallel old-way/new-way hand-off from one set of wheels to the other while the car is still running. Before the PR is merged, it will eventually only have the new-way.