| 1 | diff -ruN a/packet-manager.go b/packet-manager.go |
| 2 | |
| 3 | |
| 4 | @@ -20,6 +20,8 @@ |
| 5 | packetCount uint32 |
| 6 | // it is not nil if the allocator is enabled |
| 7 | alloc *allocator |
| 8 | + // sequential processes every packet in a single worker, in the order received |
| 9 | + sequential bool |
| 10 | } |
| 11 | |
| 12 | type packetSender interface { |
| 13 | @@ -112,6 +114,21 @@ |
| 14 | // maximizing throughput of file transfers. |
| 15 | func (s *packetManager) workerChan(runWorker func(chan orderedRequest), |
| 16 | ) chan orderedRequest { |
| 17 | + if s.sequential { |
| 18 | + ch := make(chan orderedRequest) |
| 19 | + runWorker(ch) |
| 20 | + pktChan := make(chan orderedRequest, SftpServerWorkerCount) |
| 21 | + go func() { |
| 22 | + for pkt := range pktChan { |
| 23 | + s.incomingPacket(pkt) |
| 24 | + ch <- pkt |
| 25 | + } |
| 26 | + close(ch) |
| 27 | + s.close() |
| 28 | + }() |
| 29 | + return pktChan |
| 30 | + } |
| 31 | + |
| 32 | // multiple workers for faster read/writes |
| 33 | rwChan := make(chan orderedRequest, SftpServerWorkerCount) |
| 34 | for i := 0; i < SftpServerWorkerCount; i++ { |
| 35 | diff -ruN a/packet-manager_test.go b/packet-manager_test.go |
| 36 | |
| 37 | |
| 38 | @@ -3,7 +3,9 @@ |
| 39 | import ( |
| 40 | "encoding" |
| 41 | "fmt" |
| 42 | + "sync" |
| 43 | "testing" |
| 44 | + "time" |
| 45 | |
| 46 | "github.com/stretchr/testify/assert" |
| 47 | ) |
| 48 | @@ -125,3 +127,36 @@ |
| 49 | func (p sshFxpClosePacket) String() string { |
| 50 | return fmt.Sprintf("ClPkt:%d", p.ID) |
| 51 | } |
| 52 | + |
| 53 | +// A slow WRITE must still run before the WRITE and the FSETSTAT sent after it. |
| 54 | +func TestPacketManagerSequential(t *testing.T) { |
| 55 | + sender := newTestSender() |
| 56 | + s := newPktMgr(sender) |
| 57 | + s.sequential = true |
| 58 | + var mu sync.Mutex |
| 59 | + var done []uint32 |
| 60 | + runWorker := func(ch chan orderedRequest) { |
| 61 | + go func() { |
| 62 | + for pkt := range ch { |
| 63 | + if pkt.id() == 1 { |
| 64 | + time.Sleep(50 * time.Millisecond) |
| 65 | + } |
| 66 | + mu.Lock() |
| 67 | + done = append(done, pkt.id()) |
| 68 | + mu.Unlock() |
| 69 | + s.readyPacket(s.newOrderedResponse(&sshFxpStatusPacket{ID: pkt.id()}, pkt.orderID())) |
| 70 | + } |
| 71 | + }() |
| 72 | + } |
| 73 | + pktChan := s.workerChan(runWorker) |
| 74 | + for _, p := range []requestPacket{&sshFxpWritePacket{ID: 1}, &sshFxpWritePacket{ID: 2}, &sshFxpFsetstatPacket{ID: 3}} { |
| 75 | + pktChan <- s.newOrderedRequest(p) |
| 76 | + } |
| 77 | + for range 3 { |
| 78 | + <-sender.sent |
| 79 | + } |
| 80 | + close(pktChan) |
| 81 | + mu.Lock() |
| 82 | + defer mu.Unlock() |
| 83 | + assert.Equal(t, []uint32{1, 2, 3}, done) |
| 84 | +} |
| 85 | diff -ruN a/request-server.go b/request-server.go |
| 86 | |
| 87 | |
| 88 | @@ -74,6 +74,18 @@ |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | +// WithRSSequential processes requests one at a time, in the order they are received. |
| 93 | +// |
| 94 | +// By default, reads and writes run concurrently with each other and with the other |
| 95 | +// requests, so a request can see or undo the effect of a write sent before it: |
| 96 | +// two writes to the same range can land in either order, and a FSTAT or FSETSTAT |
| 97 | +// sent after a WRITE can run before it. OpenSSH's sftp-server is sequential. |
| 98 | +func WithRSSequential() RequestServerOption { |
| 99 | + return func(rs *RequestServer) { |
| 100 | + rs.pktMgr.sequential = true |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | // NewRequestServer creates/allocates/returns new RequestServer. |
| 105 | // Normally there will be one server per user-session. |
| 106 | func NewRequestServer(rwc io.ReadWriteCloser, h Handlers, options ...RequestServerOption) *RequestServer { |