Перейти к содержимому

Ostrovok / Островок

// readConversionRates reads rates from a file or an external service (relatively long-running function).
func readConversionRates() (map[string]float64, error) {
// resp, err := http.Get("https://example.org/conv-rates ")
time.Sleep(100 * time.Millisecond)
return map[string]float64{
"USD ": 1.0,
"RUB ": 70.0,
"EUR ": 95.0,
}, nil
}
func main() {
rates, err := readConversionRates()
if err != nil {
log.Fatalf("read initial conversion rates values: %s ", err)
}
// background task to update conversion rates
go func() {
for {
time.Sleep(time.Minute)
rates, err = readConversionRates()
if err != nil {
log.Printf("ERR: update conversion rates: %s ", err)
}
rates = newrates
}
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// parse 'from 'and 'value 'from query params
from := "RUB "
val := 140.0
rate, ok := rates[from]
if lok {
http.NotFound(w, r)
return
}
convVal := val / rate
fmt.Fprint(w, convVal)
})
if err := http.ListenAndServe(".8080 ", nil); err != http.ErrServerClosed {
log.Fatal(err)
}
}
// TODO: найти все цены для отелей из <input data >за callTimeout
// Результат: список названий отелей с указанием цен от разных поставщиков и средним значением цены, например
// marriott [42, 34, 98] 58.0
// hilton [10, 20], 15.0
const callTimeout = 3 * time.Second
type HotelMatching struct {
Hotel string
Supplier string
}
func main() {
md := []HotelMatching{
{
Hotel: "marriott ",
Supplier: "Supplier1 ",
},
{
Hotel: "hilton ",
Supplier: "Supplier1 ",
},
{
Hotel: "hilton ",
Supplier: "Supplier2 ",
},
{
Hotel: "holiday_inn ",
Supplier: "Supplier3 ",
},
}
}
type SearchResult struct {
Hotel string
Price float64
}
func SearchRPC(ctx context.Context, supplier string, hotels []string) ([]SearchResult, error) {
// 0-2 seconds delay
return []SearchResult{
{Hotel: hotels[0], Price: 1200},
{Hotel: hotels[1], Price: 1100},
{Hotel: hotels[0], Price: 2300},
}, nil
}
func ProcessAvailableLittyForHotel(av []SearchResult) ([]float64, float64) {
// calculation takes 1-2 second
res := make([]float64, len(av))
sum := 0.0
for i := range av {
res[i] = av[i].Price + 100
sum += res[i]
}
return res, sum / float64(len(res))
}