Update protocol package

This commit is contained in:
Audrius Butkevicius 2015-07-30 18:47:23 +01:00
parent 5a2db802d9
commit b63351074c
6 changed files with 72 additions and 38 deletions

2
Godeps/Godeps.json generated
View File

@ -35,7 +35,7 @@
}, },
{ {
"ImportPath": "github.com/syncthing/protocol", "ImportPath": "github.com/syncthing/protocol",
"Rev": "22e24fc3879b1665077389f96862e222b2cdd8d3" "Rev": "ebcdea63c07327a342f65415bbadc497462b8f1f"
}, },
{ {
"ImportPath": "github.com/syndtr/goleveldb/leveldb", "ImportPath": "github.com/syndtr/goleveldb/leveldb",

View File

@ -31,7 +31,7 @@ func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo, fl
func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
} }
func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int, hash []byte, flags uint32, options []Option, buf []byte) error {
t.folder = folder t.folder = folder
t.name = name t.name = name
t.offset = offset t.offset = offset
@ -39,7 +39,8 @@ func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64
t.hash = hash t.hash = hash
t.flags = flags t.flags = flags
t.options = options t.options = options
return t.data, nil copy(buf, t.data)
return nil
} }
func (t *TestModel) Close(deviceID DeviceID, err error) { func (t *TestModel) Close(deviceID DeviceID, err error) {

View File

@ -26,9 +26,9 @@ func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileI
m.next.IndexUpdate(deviceID, folder, files, flags, options) m.next.IndexUpdate(deviceID, folder, files, flags, options)
} }
func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error {
name = norm.NFD.String(name) name = norm.NFD.String(name)
return m.next.Request(deviceID, folder, name, offset, size, hash, flags, options) return m.next.Request(deviceID, folder, name, offset, hash, flags, options, buf)
} }
func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) { func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {

View File

@ -18,8 +18,8 @@ func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileI
m.next.IndexUpdate(deviceID, folder, files, flags, options) m.next.IndexUpdate(deviceID, folder, files, flags, options)
} }
func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error {
return m.next.Request(deviceID, folder, name, offset, size, hash, flags, options) return m.next.Request(deviceID, folder, name, offset, hash, flags, options, buf)
} }
func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) { func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {

View File

@ -34,9 +34,9 @@ func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileI
m.next.IndexUpdate(deviceID, folder, files, flags, options) m.next.IndexUpdate(deviceID, folder, files, flags, options)
} }
func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error {
name = filepath.FromSlash(name) name = filepath.FromSlash(name)
return m.next.Request(deviceID, folder, name, offset, size, hash, flags, options) return m.next.Request(deviceID, folder, name, offset, hash, flags, options, buf)
} }
func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) { func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {

View File

@ -81,7 +81,7 @@ type Model interface {
// An index update was received from the peer device // An index update was received from the peer device
IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option)
// A request was made by the peer device // A request was made by the peer device
Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) Request(deviceID DeviceID, folder string, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error
// A cluster configuration message was received // A cluster configuration message was received
ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage)
// The peer device closed the connection // The peer device closed the connection
@ -112,11 +112,11 @@ type rawConnection struct {
idxMut sync.Mutex // ensures serialization of Index calls idxMut sync.Mutex // ensures serialization of Index calls
nextID chan int nextID chan int
outbox chan hdrMsg outbox chan hdrMsg
closed chan struct{} closed chan struct{}
once sync.Once once sync.Once
pool sync.Pool
compression Compression compression Compression
rdbuf0 []byte // used & reused by readMessage rdbuf0 []byte // used & reused by readMessage
@ -129,8 +129,9 @@ type asyncResult struct {
} }
type hdrMsg struct { type hdrMsg struct {
hdr header hdr header
msg encodable msg encodable
done chan struct{}
} }
type encodable interface { type encodable interface {
@ -151,14 +152,19 @@ func NewConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, receiv
cw := &countingWriter{Writer: writer} cw := &countingWriter{Writer: writer}
c := rawConnection{ c := rawConnection{
id: deviceID, id: deviceID,
name: name, name: name,
receiver: nativeModel{receiver}, receiver: nativeModel{receiver},
cr: cr, cr: cr,
cw: cw, cw: cw,
outbox: make(chan hdrMsg), outbox: make(chan hdrMsg),
nextID: make(chan int), nextID: make(chan int),
closed: make(chan struct{}), closed: make(chan struct{}),
pool: sync.Pool{
New: func() interface{} {
return make([]byte, BlockSize)
},
},
compression: compress, compression: compress,
} }
@ -195,7 +201,7 @@ func (c *rawConnection) Index(folder string, idx []FileInfo, flags uint32, optio
Files: idx, Files: idx,
Flags: flags, Flags: flags,
Options: options, Options: options,
}) }, nil)
c.idxMut.Unlock() c.idxMut.Unlock()
return nil return nil
} }
@ -213,7 +219,7 @@ func (c *rawConnection) IndexUpdate(folder string, idx []FileInfo, flags uint32,
Files: idx, Files: idx,
Flags: flags, Flags: flags,
Options: options, Options: options,
}) }, nil)
c.idxMut.Unlock() c.idxMut.Unlock()
return nil return nil
} }
@ -243,7 +249,7 @@ func (c *rawConnection) Request(folder string, name string, offset int64, size i
Hash: hash, Hash: hash,
Flags: flags, Flags: flags,
Options: options, Options: options,
}) }, nil)
if !ok { if !ok {
return nil, ErrClosed return nil, ErrClosed
} }
@ -257,7 +263,7 @@ func (c *rawConnection) Request(folder string, name string, offset int64, size i
// ClusterConfig send the cluster configuration message to the peer and returns any error // ClusterConfig send the cluster configuration message to the peer and returns any error
func (c *rawConnection) ClusterConfig(config ClusterConfigMessage) { func (c *rawConnection) ClusterConfig(config ClusterConfigMessage) {
c.send(-1, messageTypeClusterConfig, config) c.send(-1, messageTypeClusterConfig, config, nil)
} }
func (c *rawConnection) ping() bool { func (c *rawConnection) ping() bool {
@ -273,7 +279,7 @@ func (c *rawConnection) ping() bool {
c.awaiting[id] = rc c.awaiting[id] = rc
c.awaitingMut.Unlock() c.awaitingMut.Unlock()
ok := c.send(id, messageTypePing, nil) ok := c.send(id, messageTypePing, nil, nil)
if !ok { if !ok {
return false return false
} }
@ -342,7 +348,7 @@ func (c *rawConnection) readerLoop() (err error) {
if state != stateReady { if state != stateReady {
return fmt.Errorf("protocol error: ping message in state %d", state) return fmt.Errorf("protocol error: ping message in state %d", state)
} }
c.send(hdr.msgID, messageTypePong, pongMessage{}) c.send(hdr.msgID, messageTypePong, pongMessage{}, nil)
case pongMessage: case pongMessage:
if state != stateReady { if state != stateReady {
@ -519,12 +525,36 @@ func filterIndexMessageFiles(fs []FileInfo) []FileInfo {
} }
func (c *rawConnection) handleRequest(msgID int, req RequestMessage) { func (c *rawConnection) handleRequest(msgID int, req RequestMessage) {
data, err := c.receiver.Request(c.id, req.Folder, req.Name, int64(req.Offset), int(req.Size), req.Hash, req.Flags, req.Options) size := int(req.Size)
usePool := size <= BlockSize
c.send(msgID, messageTypeResponse, ResponseMessage{ var buf []byte
Data: data, var done chan struct{}
Code: errorToCode(err),
}) if usePool {
buf = c.pool.Get().([]byte)[:size]
done = make(chan struct{})
} else {
buf = make([]byte, size)
}
err := c.receiver.Request(c.id, req.Folder, req.Name, int64(req.Offset), req.Hash, req.Flags, req.Options, buf)
if err != nil {
c.send(msgID, messageTypeResponse, ResponseMessage{
Data: nil,
Code: errorToCode(err),
}, done)
} else {
c.send(msgID, messageTypeResponse, ResponseMessage{
Data: buf,
Code: errorToCode(err),
}, done)
}
if usePool {
<-done
c.pool.Put(buf)
}
} }
func (c *rawConnection) handleResponse(msgID int, resp ResponseMessage) { func (c *rawConnection) handleResponse(msgID int, resp ResponseMessage) {
@ -547,7 +577,7 @@ func (c *rawConnection) handlePong(msgID int) {
c.awaitingMut.Unlock() c.awaitingMut.Unlock()
} }
func (c *rawConnection) send(msgID int, msgType int, msg encodable) bool { func (c *rawConnection) send(msgID int, msgType int, msg encodable, done chan struct{}) bool {
if msgID < 0 { if msgID < 0 {
select { select {
case id := <-c.nextID: case id := <-c.nextID:
@ -564,7 +594,7 @@ func (c *rawConnection) send(msgID int, msgType int, msg encodable) bool {
} }
select { select {
case c.outbox <- hdrMsg{hdr, msg}: case c.outbox <- hdrMsg{hdr, msg, done}:
return true return true
case <-c.closed: case <-c.closed:
return false return false
@ -583,6 +613,9 @@ func (c *rawConnection) writerLoop() {
if hm.msg != nil { if hm.msg != nil {
// Uncompressed message in uncBuf // Uncompressed message in uncBuf
uncBuf, err = hm.msg.AppendXDR(uncBuf[:0]) uncBuf, err = hm.msg.AppendXDR(uncBuf[:0])
if hm.done != nil {
close(hm.done)
}
if err != nil { if err != nil {
c.close(err) c.close(err)
return return