-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathflow.go
48 lines (41 loc) · 782 Bytes
/
flow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package spice
import (
"net"
"sync"
"acln.ro/zerocopy"
)
// flow is a connection pipe to couple tenant to compute connections
type flow struct {
tenant net.Conn
compute net.Conn
}
// newFlow returns a new flow
func newFlow(tenant net.Conn, compute net.Conn) *flow {
flow := &flow{
tenant: tenant,
compute: compute,
}
return flow
}
// Pipe will start piping the connections together
func (f *flow) Pipe() error {
f.pipe(f.compute, f.tenant)
return nil
}
func (f *flow) pipe(src, dst net.Conn) (sent, received int64) {
if src == nil || dst == nil {
return
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
sent, _ = zerocopy.Transfer(src, dst)
wg.Done()
}()
go func() {
received, _ = zerocopy.Transfer(dst, src)
wg.Done()
}()
wg.Wait()
return
}