|
|
|
|
@ -1,34 +1,29 @@
|
|
|
|
|
package exchange |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"sync" |
|
|
|
|
"encoding/json" |
|
|
|
|
"time" |
|
|
|
|
"net/http" |
|
|
|
|
"io/ioutil" |
|
|
|
|
"log" |
|
|
|
|
"net/http" |
|
|
|
|
"sync" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/yuriy0803/open-etc-pool-friends/util" |
|
|
|
|
"github.com/yuriy0803/open-etc-pool-friends/storage" |
|
|
|
|
"io/ioutil" |
|
|
|
|
|
|
|
|
|
"github.com/yuriy0803/open-etc-pool-friends/util" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ExchangeProcessor struct { |
|
|
|
|
ExchangeConfig *ExchangeConfig |
|
|
|
|
backend *storage.RedisClient |
|
|
|
|
rpc *RestClient |
|
|
|
|
halt bool |
|
|
|
|
|
|
|
|
|
backend *storage.RedisClient |
|
|
|
|
rpc *RestClient |
|
|
|
|
halt bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type ExchangeConfig struct { |
|
|
|
|
Enabled bool `json:"enabled"` |
|
|
|
|
Name string `json:"name"` |
|
|
|
|
Url string `json:"url"` |
|
|
|
|
Timeout string `json:"timeout"` |
|
|
|
|
Enabled bool `json:"enabled"` |
|
|
|
|
Name string `json:"name"` |
|
|
|
|
Url string `json:"url"` |
|
|
|
|
Timeout string `json:"timeout"` |
|
|
|
|
RefreshInterval string `json:"refreshInterval"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -54,25 +49,23 @@ func NewRestClient(name, url, timeout string) *RestClient {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *RestClient) GetData() (ExchangeReply, error) { |
|
|
|
|
Resp, err := r.doPost(r.Url, "ticker") |
|
|
|
|
resp, err := r.doPost(r.Url, "ticker") |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//var reply map[string][]interface{}
|
|
|
|
|
var data ExchangeReply |
|
|
|
|
err = json.Unmarshal(Resp, &data) |
|
|
|
|
err = json.Unmarshal(resp, &data) |
|
|
|
|
return data, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func StartExchangeProcessor(cfg *ExchangeConfig, backend *storage.RedisClient)*ExchangeProcessor{ |
|
|
|
|
func StartExchangeProcessor(cfg *ExchangeConfig, backend *storage.RedisClient) *ExchangeProcessor { |
|
|
|
|
u := &ExchangeProcessor{ExchangeConfig: cfg, backend: backend} |
|
|
|
|
u.rpc = NewRestClient("ExchangeProcessor", cfg.Url, cfg.Timeout) |
|
|
|
|
return u |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (u *ExchangeProcessor) Start(){ |
|
|
|
|
|
|
|
|
|
func (u *ExchangeProcessor) Start() { |
|
|
|
|
refreshIntv := util.MustParseDuration(u.ExchangeConfig.RefreshInterval) |
|
|
|
|
refreshTimer := time.NewTimer(refreshIntv) |
|
|
|
|
log.Printf("Set Exchange data refresh every %v", refreshIntv) |
|
|
|
|
@ -80,7 +73,6 @@ func (u *ExchangeProcessor) Start(){
|
|
|
|
|
u.fetchData() |
|
|
|
|
refreshTimer.Reset(refreshIntv) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
for { |
|
|
|
|
select { |
|
|
|
|
@ -90,11 +82,9 @@ func (u *ExchangeProcessor) Start(){
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (u *ExchangeProcessor) fetchData() { |
|
|
|
|
|
|
|
|
|
reply, err := u.rpc.GetData() |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
@ -102,13 +92,10 @@ func (u *ExchangeProcessor) fetchData() {
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//log.Printf("Reply %v", reply)
|
|
|
|
|
|
|
|
|
|
//Store the data into the Redis Store
|
|
|
|
|
u.backend.StoreExchangeData(reply) |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Printf("Failed to Store the data to echange %v", err) |
|
|
|
|
log.Printf("Failed to store the data to exchange %v", err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -116,25 +103,23 @@ func (u *ExchangeProcessor) fetchData() {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *RestClient) doPost(url string, method string) ([]byte, error) { |
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
|
req.Header.Set("Accept", "application/json") |
|
|
|
|
|
|
|
|
|
log.Println(req) |
|
|
|
|
|
|
|
|
|
resp, err := r.client.Get(url) |
|
|
|
|
resp, err := r.client.Do(req) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode == 200 { // OK
|
|
|
|
|
bodyBytes, err2 := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
|
|
|
|
return bodyBytes, err2 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
} |