@@ -2,11 +2,13 @@ package main
22
33import (
44 "bytes"
5+ "context"
56 "crypto/md5"
67 "fmt"
78 "io"
89 "io/ioutil"
910 "net/http"
11+ "net/http/httptrace"
1012 "os"
1113 "os/exec"
1214 "strings"
@@ -16,8 +18,13 @@ import (
1618 "github.com/ethereum/go-ethereum/common/hexutil"
1719 "github.com/ethereum/go-ethereum/crypto"
1820 "github.com/ethereum/go-ethereum/log"
21+ "github.com/ethereum/go-ethereum/metrics"
22+ "github.com/ethereum/go-ethereum/swarm/api/client"
23+ "github.com/ethereum/go-ethereum/swarm/spancontext"
1924 "github.com/ethereum/go-ethereum/swarm/storage/feed"
25+ "github.com/ethereum/go-ethereum/swarm/testutil"
2026 colorable "github.com/mattn/go-colorable"
27+ opentracing "github.com/opentracing/opentracing-go"
2128 "github.com/pborman/uuid"
2229 cli "gopkg.in/urfave/cli.v1"
2330)
@@ -26,11 +33,29 @@ const (
2633 feedRandomDataLength = 8
2734)
2835
29- // TODO: retrieve with manifest + extract repeating code
3036func cliFeedUploadAndSync (c * cli.Context ) error {
31-
37+ metrics . GetOrRegisterCounter ( "feed-and-sync" , nil ). Inc ( 1 )
3238 log .Root ().SetHandler (log .CallerFileHandler (log .LvlFilterHandler (log .Lvl (verbosity ), log .StreamHandler (colorable .NewColorableStderr (), log .TerminalFormat (true )))))
3339
40+ errc := make (chan error )
41+ go func () {
42+ errc <- feedUploadAndSync (c )
43+ }()
44+
45+ select {
46+ case err := <- errc :
47+ if err != nil {
48+ metrics .GetOrRegisterCounter ("feed-and-sync.fail" , nil ).Inc (1 )
49+ }
50+ return err
51+ case <- time .After (time .Duration (timeout ) * time .Second ):
52+ metrics .GetOrRegisterCounter ("feed-and-sync.timeout" , nil ).Inc (1 )
53+ return fmt .Errorf ("timeout after %v sec" , timeout )
54+ }
55+ }
56+
57+ // TODO: retrieve with manifest + extract repeating code
58+ func feedUploadAndSync (c * cli.Context ) error {
3459 defer func (now time.Time ) { log .Info ("total time" , "time" , time .Since (now ), "size (kb)" , filesize ) }(time .Now ())
3560
3661 generateEndpoints (scheme , cluster , appName , from , to )
@@ -204,12 +229,12 @@ func cliFeedUploadAndSync(c *cli.Context) error {
204229 log .Info ("all endpoints synced random data successfully" )
205230
206231 // upload test file
207- log .Info ("uploading to " + endpoints [0 ] + " and syncing" )
232+ seed := int (time .Now ().UnixNano () / 1e6 )
233+ log .Info ("feed uploading to " + endpoints [0 ]+ " and syncing" , "seed" , seed )
208234
209- f , cleanup := generateRandomFile (filesize * 1000 )
210- defer cleanup ()
235+ randomBytes := testutil .RandomBytes (seed , filesize * 1000 )
211236
212- hash , err := upload (f , endpoints [0 ])
237+ hash , err := upload (& randomBytes , endpoints [0 ])
213238 if err != nil {
214239 return err
215240 }
@@ -218,7 +243,7 @@ func cliFeedUploadAndSync(c *cli.Context) error {
218243 return err
219244 }
220245 multihashHex := hexutil .Encode (hashBytes )
221- fileHash , err := digest (f )
246+ fileHash , err := digest (bytes . NewReader ( randomBytes ) )
222247 if err != nil {
223248 return err
224249 }
@@ -284,14 +309,37 @@ func cliFeedUploadAndSync(c *cli.Context) error {
284309}
285310
286311func fetchFeed (topic string , user string , endpoint string , original []byte , ruid string ) error {
312+ ctx , sp := spancontext .StartSpan (context .Background (), "feed-and-sync.fetch" )
313+ defer sp .Finish ()
314+
287315 log .Trace ("sleeping" , "ruid" , ruid )
288316 time .Sleep (3 * time .Second )
289317
290318 log .Trace ("http get request (feed)" , "ruid" , ruid , "api" , endpoint , "topic" , topic , "user" , user )
291- res , err := http .Get (endpoint + "/bzz-feed:/?topic=" + topic + "&user=" + user )
319+
320+ var tn time.Time
321+ reqUri := endpoint + "/bzz-feed:/?topic=" + topic + "&user=" + user
322+ req , _ := http .NewRequest ("GET" , reqUri , nil )
323+
324+ opentracing .GlobalTracer ().Inject (
325+ sp .Context (),
326+ opentracing .HTTPHeaders ,
327+ opentracing .HTTPHeadersCarrier (req .Header ))
328+
329+ trace := client .GetClientTrace ("feed-and-sync - http get" , "feed-and-sync" , ruid , & tn )
330+
331+ req = req .WithContext (httptrace .WithClientTrace (ctx , trace ))
332+ transport := http .DefaultTransport
333+
334+ //transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
335+
336+ tn = time .Now ()
337+ res , err := transport .RoundTrip (req )
292338 if err != nil {
339+ log .Error (err .Error (), "ruid" , ruid )
293340 return err
294341 }
342+
295343 log .Trace ("http get response (feed)" , "ruid" , ruid , "api" , endpoint , "topic" , topic , "user" , user , "code" , res .StatusCode , "len" , res .ContentLength )
296344
297345 if res .StatusCode != 200 {
0 commit comments