Skip to content

Commit a55d7ba

Browse files
Update bsvalias timeouts.
1 parent 3d97e05 commit a55d7ba

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed

bsvalias/http.go

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,56 @@ import (
2020
"github.com/pkg/errors"
2121
)
2222

23+
var (
24+
DefaultDialTimeout = time.Second * 10
25+
DefaultRequestTimeout = time.Second * 20
26+
)
27+
2328
// HTTPClient represents a client for a paymail/bsvalias service that uses HTTP for requests.
2429
type HTTPClient struct {
2530
Handle string
2631
Site Site
2732
Alias string
2833
Hostname string
34+
35+
DialTimeout, RequestTimeout time.Duration
2936
}
3037

3138
// HTTPFactory is a factory for creating HTTP clients.
32-
type HTTPFactory struct{}
39+
type HTTPFactory struct {
40+
DialTimeout, RequestTimeout time.Duration
41+
}
3342

3443
// NewHTTPFactory creates a new HTTP factory.
3544
func NewHTTPFactory() *HTTPFactory {
36-
return &HTTPFactory{}
45+
return &HTTPFactory{
46+
DialTimeout: DefaultDialTimeout,
47+
RequestTimeout: DefaultRequestTimeout,
48+
}
49+
}
50+
51+
func (f *HTTPFactory) SetTimeouts(dialTimeout, requestTimeout time.Duration) {
52+
f.DialTimeout = dialTimeout
53+
f.RequestTimeout = requestTimeout
3754
}
3855

3956
// NewClient creates a new client.
4057
func (f *HTTPFactory) NewClient(ctx context.Context, handle string) (Client, error) {
41-
return NewHTTPClient(ctx, handle)
58+
c, err := NewHTTPClient(ctx, handle)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
c.SetTimeouts(f.DialTimeout, f.RequestTimeout)
64+
return c, nil
4265
}
4366

4467
// NewHTTPClient creates a new HTTPClient.
4568
func NewHTTPClient(ctx context.Context, handle string) (*HTTPClient, error) {
4669
result := HTTPClient{
47-
Handle: handle,
70+
DialTimeout: DefaultDialTimeout,
71+
RequestTimeout: DefaultRequestTimeout,
72+
Handle: handle,
4873
}
4974

5075
fields := strings.Split(handle, "@")
@@ -64,6 +89,11 @@ func NewHTTPClient(ctx context.Context, handle string) (*HTTPClient, error) {
6489
return &result, nil
6590
}
6691

92+
func (c *HTTPClient) SetTimeouts(dialTimeout, requestTimeout time.Duration) {
93+
c.DialTimeout = dialTimeout
94+
c.RequestTimeout = requestTimeout
95+
}
96+
6797
func (c *HTTPClient) IsCapable(url string) (bool, error) {
6898
if _, err := c.Site.Capabilities.GetURL(url); err != nil {
6999
if errors.Cause(err) == ErrNotCapable {
@@ -87,7 +117,7 @@ func (c *HTTPClient) GetPublicKey(ctx context.Context) (*bitcoin.PublicKey, erro
87117
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
88118

89119
var response PublicKeyResponse
90-
if err := get(ctx, url, &response); err != nil {
120+
if err := get(ctx, c.DialTimeout, c.RequestTimeout, url, &response); err != nil {
91121
return nil, errors.Wrap(err, "http get")
92122
}
93123

@@ -137,7 +167,7 @@ func (c *HTTPClient) GetPaymentDestination(ctx context.Context, senderName, send
137167
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
138168

139169
var response PaymentDestinationResponse
140-
if err := post(ctx, url, request, &response); err != nil {
170+
if err := post(ctx, c.DialTimeout, c.RequestTimeout, url, request, &response); err != nil {
141171
return nil, errors.Wrap(err, "http post")
142172
}
143173

@@ -189,7 +219,7 @@ func (c *HTTPClient) GetPaymentRequest(ctx context.Context, senderName, senderHa
189219
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
190220

191221
var response PaymentRequestResponse
192-
if err := post(ctx, url, request, &response); err != nil {
222+
if err := post(ctx, c.DialTimeout, c.RequestTimeout, url, request, &response); err != nil {
193223
return nil, errors.Wrap(err, "http post")
194224
}
195225

@@ -243,7 +273,7 @@ func (c *HTTPClient) GetP2PPaymentDestination(ctx context.Context,
243273
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
244274

245275
var response P2PPaymentDestinationResponse
246-
if err := post(ctx, url, request, &response); err != nil {
276+
if err := post(ctx, c.DialTimeout, c.RequestTimeout, url, request, &response); err != nil {
247277
return nil, errors.Wrap(err, "http post")
248278
}
249279

@@ -299,7 +329,7 @@ func (c *HTTPClient) PostP2PTransaction(ctx context.Context, senderHandle, note,
299329
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
300330

301331
var response P2PTransactionResponse
302-
if err := post(ctx, url, request, &response); err != nil {
332+
if err := post(ctx, c.DialTimeout, c.RequestTimeout, url, request, &response); err != nil {
303333
return "", errors.Wrap(err, "http post")
304334
}
305335

@@ -321,7 +351,7 @@ func (c *HTTPClient) ListTokenizedInstruments(ctx context.Context) ([]Instrument
321351
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
322352

323353
var response InstrumentAliasListResponse
324-
if err := get(ctx, url, &response); err != nil {
354+
if err := get(ctx, c.DialTimeout, c.RequestTimeout, url, &response); err != nil {
325355
return nil, errors.Wrap(err, "http get")
326356
}
327357

@@ -339,7 +369,7 @@ func (c *HTTPClient) GetPublicProfile(ctx context.Context) (*PublicProfile, erro
339369
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
340370

341371
response := &PublicProfile{}
342-
if err := get(ctx, url, response); err != nil {
372+
if err := get(ctx, c.DialTimeout, c.RequestTimeout, url, response); err != nil {
343373
return nil, errors.Wrap(err, "http get")
344374
}
345375

@@ -358,7 +388,7 @@ func (c *HTTPClient) PostNegotiationTx(ctx context.Context,
358388
url = strings.ReplaceAll(url, "{alias}", c.Alias)
359389
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
360390

361-
status, body, err := postRaw(ctx, url, tx)
391+
status, body, err := postRaw(ctx, c.DialTimeout, c.RequestTimeout, url, tx)
362392
if err != nil {
363393
return nil, errors.Wrap(err, "http post")
364394
}
@@ -402,23 +432,25 @@ func (c *HTTPClient) PostMerkleProofs(ctx context.Context, merkleProofs MerklePr
402432
url = strings.ReplaceAll(url, "{alias}", c.Alias)
403433
url = strings.ReplaceAll(url, "{domain.tld}", c.Hostname)
404434

405-
if err := post(ctx, url, merkleProofs, nil); err != nil {
435+
if err := post(ctx, c.DialTimeout, c.RequestTimeout, url, merkleProofs, nil); err != nil {
406436
return errors.Wrap(err, "http post")
407437
}
408438

409439
return nil
410440
}
411441

412-
func postRaw(ctx context.Context, url string, request interface{}) (int, io.ReadCloser, error) {
442+
func postRaw(ctx context.Context, dialTimeout, requestTimeout time.Duration, url string,
443+
request interface{}) (int, io.ReadCloser, error) {
444+
413445
var transport = &http.Transport{
414446
Dial: (&net.Dialer{
415-
Timeout: 5 * time.Second,
447+
Timeout: dialTimeout,
416448
}).Dial,
417-
TLSHandshakeTimeout: 5 * time.Second,
449+
TLSHandshakeTimeout: dialTimeout,
418450
}
419451

420452
var client = &http.Client{
421-
Timeout: time.Second * 10,
453+
Timeout: requestTimeout,
422454
Transport: transport,
423455
}
424456

@@ -436,16 +468,18 @@ func postRaw(ctx context.Context, url string, request interface{}) (int, io.Read
436468
}
437469

438470
// post sends a request to the HTTP server using the POST method.
439-
func post(ctx context.Context, url string, request, response interface{}) error {
471+
func post(ctx context.Context, dialTimeout, requestTimeout time.Duration, url string,
472+
request, response interface{}) error {
473+
440474
var transport = &http.Transport{
441475
Dial: (&net.Dialer{
442-
Timeout: 5 * time.Second,
476+
Timeout: dialTimeout,
443477
}).Dial,
444-
TLSHandshakeTimeout: 5 * time.Second,
478+
TLSHandshakeTimeout: dialTimeout,
445479
}
446480

447481
var client = &http.Client{
448-
Timeout: time.Second * 10,
482+
Timeout: requestTimeout,
449483
Transport: transport,
450484
}
451485

@@ -486,16 +520,18 @@ func post(ctx context.Context, url string, request, response interface{}) error
486520
}
487521

488522
// get sends a request to the HTTP server using the GET method.
489-
func get(ctx context.Context, url string, response interface{}) error {
523+
func get(ctx context.Context, dialTimeout, requestTimeout time.Duration, url string,
524+
response interface{}) error {
525+
490526
var transport = &http.Transport{
491527
Dial: (&net.Dialer{
492-
Timeout: 5 * time.Second,
528+
Timeout: dialTimeout,
493529
}).Dial,
494-
TLSHandshakeTimeout: 5 * time.Second,
530+
TLSHandshakeTimeout: dialTimeout,
495531
}
496532

497533
var client = &http.Client{
498-
Timeout: time.Second * 10,
534+
Timeout: requestTimeout,
499535
Transport: transport,
500536
}
501537

bsvalias/site.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func GetSite(ctx context.Context, domain string) (Site, error) {
3838

3939
url := fmt.Sprintf("%s/.well-known/bsvalias", host)
4040

41-
if err := get(ctx, url, &site.Capabilities); err == nil {
41+
if err := get(ctx, DefaultDialTimeout, DefaultRequestTimeout, url, &site.Capabilities); err == nil {
4242
site.URL = host
4343
return site, nil
4444
}
@@ -50,7 +50,8 @@ func GetSite(ctx context.Context, domain string) (Site, error) {
5050
// use the default well known url, per the spec.
5151
url := fmt.Sprintf("https://%s/.well-known/bsvalias", domain)
5252

53-
if err := get(ctx, url, &site.Capabilities); err != nil {
53+
if err := get(ctx, DefaultDialTimeout, DefaultRequestTimeout, url,
54+
&site.Capabilities); err != nil {
5455
return site, errors.Wrap(ErrNotCapable, err.Error())
5556
}
5657

0 commit comments

Comments
 (0)