This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.go
82 lines (71 loc) · 1.6 KB
/
raw.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// in raw.go are the functions to send and receive messages on an underlying connection
package fmtp
import (
"context"
"io"
"net"
"github.com/pkg/errors"
)
// send is the function that sends a message over a io.Writer
//
// WARNING: unsafe for concurrent use!
// DEPRECATED: switching to pipeline infrastructure
func send(ctx context.Context, w io.Writer, msg *Message) (int, error) {
// Create the local context
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Send
var (
ret = make(chan error)
n int64
)
defer close(ret)
go func() {
var err error
for i := 0; i < 3; i++ {
n, err = msg.WriteTo(w)
// Check if this is a temporary error, in such case, retry
if netErr, ok := err.(net.Error); ok && netErr.Temporary() {
continue
} else {
ret <- err
}
// Return
return
}
ret <- errors.Wrap(err, "send: cannot send after 3 retry")
}()
select {
case err := <-ret:
return int(n), err
case <-ctx.Done():
return int(n), ctx.Err()
}
}
// receive is the function that receives a message from a io.Reader
//
// WARNING: unsafe for concurrent use!
func receive(ctx context.Context, r io.Reader) (*Message, error) {
// Create the message to unmarshal to
resp := &Message{}
// Launch a listener goroutine
ret := make(chan error)
defer close(ret)
go func(ctx context.Context) {
// Unmarshal from the connection
_, err := resp.ReadFrom(r)
select {
case <-ctx.Done():
return
default:
ret <- err
}
}(ctx)
// Select on the results
select {
case err := <-ret:
return resp, err
case <-ctx.Done():
return nil, context.DeadlineExceeded
}
}