mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 08:40:28 +00:00
do session crypto work using the worker pool
This commit is contained in:
parent
befd1b43a0
commit
00e9c3dbd9
@ -442,6 +442,7 @@ func (sinfo *sessionInfo) recvWorker() {
|
|||||||
// Since there's no reason for anywhere else in the session code to need to *read* it...
|
// Since there's no reason for anywhere else in the session code to need to *read* it...
|
||||||
// Only needs to be updated from the outside if a ping resets it...
|
// Only needs to be updated from the outside if a ping resets it...
|
||||||
// That would get rid of the need to take a mutex for the sessionFunc
|
// That would get rid of the need to take a mutex for the sessionFunc
|
||||||
|
var callbacks []chan func()
|
||||||
doRecv := func(p *wire_trafficPacket) {
|
doRecv := func(p *wire_trafficPacket) {
|
||||||
var bs []byte
|
var bs []byte
|
||||||
var err error
|
var err error
|
||||||
@ -459,7 +460,10 @@ func (sinfo *sessionInfo) recvWorker() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var isOK bool
|
var isOK bool
|
||||||
|
ch := make(chan func(), 1)
|
||||||
|
poolFunc := func() {
|
||||||
bs, isOK = crypto.BoxOpen(&k, p.Payload, &p.Nonce)
|
bs, isOK = crypto.BoxOpen(&k, p.Payload, &p.Nonce)
|
||||||
|
callback := func() {
|
||||||
if !isOK {
|
if !isOK {
|
||||||
util.PutBytes(bs)
|
util.PutBytes(bs)
|
||||||
return
|
return
|
||||||
@ -483,7 +487,24 @@ func (sinfo *sessionInfo) recvWorker() {
|
|||||||
sinfo.recv <- bs
|
sinfo.recv <- bs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ch <- callback
|
||||||
|
}
|
||||||
|
// Send to the worker and wait for it to finish
|
||||||
|
util.WorkerGo(poolFunc)
|
||||||
|
callbacks = append(callbacks, ch)
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
|
for len(callbacks) > 0 {
|
||||||
|
select {
|
||||||
|
case f := <-callbacks[0]:
|
||||||
|
callbacks = callbacks[1:]
|
||||||
|
f()
|
||||||
|
case <-sinfo.cancel.Finished():
|
||||||
|
return
|
||||||
|
case p := <-sinfo.fromRouter:
|
||||||
|
doRecv(p)
|
||||||
|
}
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case <-sinfo.cancel.Finished():
|
case <-sinfo.cancel.Finished():
|
||||||
return
|
return
|
||||||
@ -496,6 +517,7 @@ func (sinfo *sessionInfo) recvWorker() {
|
|||||||
func (sinfo *sessionInfo) sendWorker() {
|
func (sinfo *sessionInfo) sendWorker() {
|
||||||
// TODO move info that this worker needs here, send updates via a channel
|
// TODO move info that this worker needs here, send updates via a channel
|
||||||
// Otherwise we need to take a mutex to avoid races with update()
|
// Otherwise we need to take a mutex to avoid races with update()
|
||||||
|
var callbacks []chan func()
|
||||||
doSend := func(bs []byte) {
|
doSend := func(bs []byte) {
|
||||||
var p wire_trafficPacket
|
var p wire_trafficPacket
|
||||||
var k crypto.BoxSharedKey
|
var k crypto.BoxSharedKey
|
||||||
@ -511,16 +533,34 @@ func (sinfo *sessionInfo) sendWorker() {
|
|||||||
}
|
}
|
||||||
// Get the mutex-protected info needed to encrypt the packet
|
// Get the mutex-protected info needed to encrypt the packet
|
||||||
sinfo.doFunc(sessionFunc)
|
sinfo.doFunc(sessionFunc)
|
||||||
|
ch := make(chan func(), 1)
|
||||||
|
poolFunc := func() {
|
||||||
// Encrypt the packet
|
// Encrypt the packet
|
||||||
p.Payload, _ = crypto.BoxSeal(&k, bs, &p.Nonce)
|
p.Payload, _ = crypto.BoxSeal(&k, bs, &p.Nonce)
|
||||||
packet := p.encode()
|
packet := p.encode()
|
||||||
// Cleanup
|
// Cleanup
|
||||||
util.PutBytes(bs)
|
util.PutBytes(bs)
|
||||||
util.PutBytes(p.Payload)
|
util.PutBytes(p.Payload)
|
||||||
// Send the packet
|
// The callback will send the packet
|
||||||
sinfo.core.router.out(packet)
|
callback := func() { sinfo.core.router.out(packet) }
|
||||||
|
ch <- callback
|
||||||
|
}
|
||||||
|
// Send to the worker and wait for it to finish
|
||||||
|
util.WorkerGo(poolFunc)
|
||||||
|
callbacks = append(callbacks, ch)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
for len(callbacks) > 0 {
|
||||||
|
select {
|
||||||
|
case f := <-callbacks[0]:
|
||||||
|
callbacks = callbacks[1:]
|
||||||
|
f()
|
||||||
|
case <-sinfo.cancel.Finished():
|
||||||
|
return
|
||||||
|
case bs := <-sinfo.send:
|
||||||
|
doSend(bs)
|
||||||
|
}
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case <-sinfo.cancel.Finished():
|
case <-sinfo.cancel.Finished():
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user