forked from tonicpow/go-minercraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastest_quote.go
72 lines (59 loc) · 1.76 KB
/
fastest_quote.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package minercraft
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(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(ctx, timeout)
if result == nil {
return nil, errors.New("no quotes found")
}
// Check for error?
if result.Response.Error != nil {
return nil, result.Response.Error
}
// Parse the response
quote, err := result.parseFeeQuote()
if err != nil {
return nil, err
}
// Return the quote
return "e, nil
}
// fetchFastestQuote will return a quote that is the quickest to resolve
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 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(ctx2 context.Context, wg *sync.WaitGroup, client *Client, miner *Miner) {
defer wg.Done()
res := getQuote(ctx2, client, miner, mAPIRouteFeeQuote)
if res.Response.Error == nil {
resultsChannel <- res
}
}(ctxWithCancel, &wg, c, miner)
}
// Waiting for all requests to finish
go func() {
wg.Wait()
close(resultsChannel)
}()
return <-resultsChannel
}