forked from tonicpow/go-minercraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastest_quote.go
59 lines (47 loc) · 1.36 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
package minercraft
import (
"context"
"sync"
)
// 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) {
// Get the fastest quote
result := c.fetchFastestQuote()
// Check for error?
if result.Response.Error != nil {
return nil, result.Response.Error
}
// Parse the response
quote, err := result.parseQuote()
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() *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())
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) {
defer wg.Done()
resultsChannel <- getQuote(ctx, client, miner)
}(ctx, c, miner)
}
// Waiting for all requests to finish
go func() {
wg.Wait()
close(resultsChannel)
}()
return <-resultsChannel
}