Skip to content

Commit

Permalink
Added context and timeout to FastestQuote()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jul 30, 2021
1 parent a9c729f commit 609dc39
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package minercraft

import "time"

const (

// version is the current package version
version = "v0.2.9"
version = "v0.2.10"

// defaultUserAgent is the default user agent for all requests
defaultUserAgent string = "go-minercraft: " + version

// defaultFastQuoteTimeout is used for the FastestQuote timeout
defaultFastQuoteTimeout = 20 * time.Second
)

const (
Expand Down
6 changes: 4 additions & 2 deletions examples/fastest_quote/fastest_quote.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"context"
"log"
"time"

"github.com/tonicpow/go-minercraft"
)
Expand All @@ -16,9 +18,9 @@ func main() {

log.Printf("querying %d miners for the fastest response...", len(client.Miners))

// Fetch fastest quote from all miners
// Fetch the fastest quote from all miners
var response *minercraft.FeeQuoteResponse
response, err = client.FastestQuote()
response, err = client.FastestQuote(context.Background(), 10*time.Second)
if err != nil {
log.Fatalf("error occurred: %s", err.Error())
}
Expand Down
20 changes: 13 additions & 7 deletions fastest_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import (
"context"
"errors"
"sync"
"time"
)

// FastestQuote will check all known miners and return the fastest quote response
//
// Note: this might return different results each time if miners have the same rates as
// it's a race condition on which results come back first
func (c *Client) FastestQuote() (*FeeQuoteResponse, error) {
func (c *Client) FastestQuote(ctx context.Context, timeout time.Duration) (*FeeQuoteResponse, error) {

// No timeout (use the default)
if timeout.Seconds() == 0 {
timeout = defaultFastQuoteTimeout
}

// Get the fastest quote
result := c.fetchFastestQuote()
result := c.fetchFastestQuote(ctx, timeout)
if result == nil {
return nil, errors.New("no quotes found")
}
Expand All @@ -34,26 +40,26 @@ func (c *Client) FastestQuote() (*FeeQuoteResponse, error) {
}

// fetchFastestQuote will return a quote that is the quickest to resolve
func (c *Client) fetchFastestQuote() *internalResult {
func (c *Client) fetchFastestQuote(ctx context.Context, timeout time.Duration) *internalResult {

// The channel for the internal results
resultsChannel := make(chan *internalResult, len(c.Miners))

// Create a context (to cancel)
ctx, cancel := context.WithCancel(context.Background())
// Create a context (to cancel or timeout)
ctxWithCancel, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

// Loop each miner (break into a Go routine for each quote request)
var wg sync.WaitGroup
for _, miner := range c.Miners {
wg.Add(1)
go func(ctx context.Context, client *Client, miner *Miner) {
go func(ctx context.Context, wg *sync.WaitGroup, client *Client, miner *Miner) {
defer wg.Done()
res := getQuote(ctx, client, miner)
if res.Response.Error == nil {
resultsChannel <- res
}
}(ctx, c, miner)
}(ctxWithCancel, &wg, c, miner)
}

// Waiting for all requests to finish
Expand Down

0 comments on commit 609dc39

Please sign in to comment.