-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.go
More file actions
58 lines (48 loc) · 1000 Bytes
/
utilities.go
File metadata and controls
58 lines (48 loc) · 1000 Bytes
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
package fkdevice
import (
"io"
"net"
"net/http"
"time"
)
type DeadlineReader struct {
Target net.Conn
Timeout time.Duration
}
func (dr *DeadlineReader) Read(p []byte) (n int, err error) {
dr.Target.SetReadDeadline(time.Now().Add(dr.Timeout))
n, err = dr.Target.Read(p)
return
}
type DebugReader struct {
Target io.Reader
}
func (dr *DebugReader) Read(p []byte) (n int, err error) {
return dr.Target.Read(p)
}
type LimitedReader struct {
Target io.Reader
MessagesExpected int
}
func (dr *LimitedReader) Read(p []byte) (n int, err error) {
if dr.MessagesExpected == 0 {
return 0, io.EOF
}
return dr.Target.Read(p)
}
type QueryResponse struct {
tcp net.Conn
http *http.Response
}
func (qr *QueryResponse) Reader(to int) io.Reader {
if qr.tcp != nil {
return &DebugReader{Target: &DeadlineReader{qr.tcp, time.Duration(to) * time.Second}}
}
return qr.http.Body
}
func (qr *QueryResponse) Close() error {
if qr.tcp != nil {
qr.tcp.Close()
}
return nil
}