mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
fix: throttle wakatime api requests (attempt to fix #152)
This commit is contained in:
parent
9d7afde6a9
commit
56247b4e1e
@ -450,7 +450,7 @@ func (h *SettingsHandler) actionImportWaktime(w http.ResponseWriter, r *http.Req
|
|||||||
Value: time.Now().Format(time.RFC822),
|
Value: time.Now().Format(time.RFC822),
|
||||||
})
|
})
|
||||||
|
|
||||||
return http.StatusAccepted, "ImportAll started. This may take a few minutes.", ""
|
return http.StatusAccepted, "Import started. This will take several minutes. Please check back later.", ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SettingsHandler) actionRegenerateSummaries(w http.ResponseWriter, r *http.Request) (int, string, string) {
|
func (h *SettingsHandler) actionRegenerateSummaries(w http.ResponseWriter, r *http.Request) (int, string, string) {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/emvi/logbuch"
|
"github.com/emvi/logbuch"
|
||||||
"github.com/muety/wakapi/config"
|
"github.com/muety/wakapi/config"
|
||||||
@ -17,7 +18,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const OriginWakatime = "wakatime"
|
const OriginWakatime = "wakatime"
|
||||||
const maxWorkers = 6
|
const (
|
||||||
|
// wakatime api permits a max. rate of 10 req / sec
|
||||||
|
// https://github.com/wakatime/wakatime/issues/261
|
||||||
|
// with 5 workers, each sleeping slightly over 1/2 sec after every req., we should stay well below that limit
|
||||||
|
maxWorkers = 5
|
||||||
|
throttleDelay = 550 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
type WakatimeHeartbeatImporter struct {
|
type WakatimeHeartbeatImporter struct {
|
||||||
ApiKey string
|
ApiKey string
|
||||||
@ -72,6 +79,7 @@ func (w *WakatimeHeartbeatImporter) Import(user *models.User, minFrom time.Time,
|
|||||||
|
|
||||||
go func(day time.Time) {
|
go func(day time.Time) {
|
||||||
defer sem.Release(1)
|
defer sem.Release(1)
|
||||||
|
defer time.Sleep(throttleDelay)
|
||||||
|
|
||||||
d := day.Format(config.SimpleDateFormat)
|
d := day.Format(config.SimpleDateFormat)
|
||||||
heartbeats, err := w.fetchHeartbeats(d)
|
heartbeats, err := w.fetchHeartbeats(d)
|
||||||
@ -113,6 +121,8 @@ func (w *WakatimeHeartbeatImporter) fetchHeartbeats(day string) ([]*wakatime.Hea
|
|||||||
res, err := httpClient.Do(w.withHeaders(req))
|
res, err := httpClient.Do(w.withHeaders(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
} else if res.StatusCode >= 400 {
|
||||||
|
return nil, errors.New(fmt.Sprintf("got status %d from wakatime api", res.StatusCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
var heartbeatsData wakatime.HeartbeatsViewModel
|
var heartbeatsData wakatime.HeartbeatsViewModel
|
||||||
|
@ -1 +1 @@
|
|||||||
1.26.0
|
1.26.1
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
<div class="w-full flex justify-center">
|
<div class="w-full flex justify-center">
|
||||||
<div class="flex items-center justify-between max-w-2xl flex-grow">
|
<div class="flex items-center justify-between max-w-2xl flex-grow">
|
||||||
<div><a onclick="window.history.back()" class="text-gray-500 text-sm cursor-pointer">← Go back</a></div>
|
<div><a href="/" class="text-gray-500 text-sm cursor-pointer">← Go back</a></div>
|
||||||
<div><h1 class="font-semibold text-2xl text-white m-0 border-b-4 border-green-700">Settings</h1></div>
|
<div><h1 class="font-semibold text-2xl text-white m-0 border-b-4 border-green-700">Settings</h1></div>
|
||||||
<div> </div>
|
<div> </div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user