7
7
"net"
8
8
"net/http"
9
9
"os"
10
- "time"
11
10
"sync"
11
+ "time"
12
12
)
13
13
14
14
type FetchConfig struct {
@@ -22,9 +22,9 @@ type FetchConfig struct {
22
22
23
23
func NewFetchConfig () * FetchConfig {
24
24
return & FetchConfig {
25
- etags : make (map [string ]string ),
25
+ etags : make (map [string ]string ),
26
26
etagsLock : & sync.RWMutex {},
27
- Mime : "application/json" ,
27
+ Mime : "application/json" ,
28
28
}
29
29
}
30
30
@@ -45,11 +45,13 @@ func (e IdenticalEtag) Error() string {
45
45
return fmt .Sprintf ("File %s is identical according to Etag: %s" , e .File , e .Etag )
46
46
}
47
47
48
- func (c * FetchConfig ) FetchFile (file string ) ([]byte , int , bool , error ) {
48
+ func (c * FetchConfig ) FetchFile (file string ) ([]byte , int , error ) {
49
49
var f io.Reader
50
50
var err error
51
- if len (file ) > 8 && (file [0 :7 ] == "http://" || file [0 :8 ] == "https://" ) {
52
51
52
+ status_code := - 1
53
+
54
+ if len (file ) > 8 && (file [0 :7 ] == "http://" || file [0 :8 ] == "https://" ) {
53
55
// Copying base of DefaultTransport from https://golang.org/src/net/http/transport.go
54
56
// There is a proposal for a Clone of
55
57
tr := & http.Transport {
@@ -65,6 +67,7 @@ func (c *FetchConfig) FetchFile(file string) ([]byte, int, bool, error) {
65
67
ExpectContinueTimeout : 1 * time .Second ,
66
68
ProxyConnectHeader : map [string ][]string {},
67
69
}
70
+
68
71
// Keep User-Agent in proxy request
69
72
tr .ProxyConnectHeader .Set ("User-Agent" , c .UserAgent )
70
73
@@ -84,61 +87,57 @@ func (c *FetchConfig) FetchFile(file string) ([]byte, int, bool, error) {
84
87
85
88
proxyurl , err := http .ProxyFromEnvironment (req )
86
89
if err != nil {
87
- return nil , - 1 , false , err
90
+ return nil , - 1 , err
88
91
}
89
92
proxyreq := http .ProxyURL (proxyurl )
90
93
tr .Proxy = proxyreq
91
94
92
- if err != nil {
93
- return nil , - 1 , false , err
94
- }
95
-
96
95
fhttp , err := client .Do (req )
97
96
if err != nil {
98
- return nil , - 1 , false , err
97
+ return nil , - 1 , err
99
98
}
100
99
if fhttp .Body != nil {
101
100
defer fhttp .Body .Close ()
102
101
}
103
102
defer client .CloseIdleConnections ()
104
- //RefreshStatusCode.WithLabelValues(file, fmt.Sprintf("%d", fhttp.StatusCode)).Inc()
105
103
106
104
if fhttp .StatusCode == 304 {
107
- //LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
108
- return nil , fhttp .StatusCode , true , HttpNotModified {
105
+ return nil , fhttp .StatusCode , HttpNotModified {
109
106
File : file ,
110
107
}
111
- } else if fhttp .StatusCode != 200 {
108
+ }
109
+
110
+ if fhttp .StatusCode != 200 {
112
111
c .etagsLock .Lock ()
113
112
delete (c .etags , file )
114
113
c .etagsLock .Unlock ()
115
- return nil , fhttp .StatusCode , true , fmt .Errorf ("HTTP %s" , fhttp .Status )
116
114
}
117
- //LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
118
-
119
- f = fhttp .Body
120
115
121
116
newEtag := fhttp .Header .Get ("ETag" )
122
-
123
- if ! c .EnableEtags || newEtag == "" || newEtag != c .etags [file ] { // check lock here
124
- c .etagsLock .Lock ()
125
- c .etags [file ] = newEtag
126
- c .etagsLock .Unlock ()
127
- } else {
128
- return nil , fhttp .StatusCode , true , IdenticalEtag {
117
+ if c .EnableEtags && newEtag != "" && newEtag == c .etags [file ] {
118
+ return nil , fhttp .StatusCode , IdenticalEtag {
129
119
File : file ,
130
120
Etag : newEtag ,
131
121
}
132
122
}
123
+
124
+ c .etagsLock .Lock ()
125
+ c .etags [file ] = newEtag
126
+ c .etagsLock .Unlock ()
127
+
128
+ f = fhttp .Body
129
+ status_code = fhttp .StatusCode
133
130
} else {
134
131
f , err = os .Open (file )
135
132
if err != nil {
136
- return nil , - 1 , false , err
133
+ return nil , - 1 , err
137
134
}
138
135
}
136
+
139
137
data , err := ioutil .ReadAll (f )
140
138
if err != nil {
141
- return nil , - 1 , false , err
139
+ return nil , - 1 , err
142
140
}
143
- return data , - 1 , false , nil
141
+
142
+ return data , status_code , nil
144
143
}
0 commit comments