do relogin periodically to refresh account on API side

This commit is contained in:
Vladislav Yarmak
2021-03-30 16:29:54 +03:00
parent 397bf28ac6
commit dfea7e62bc
2 changed files with 55 additions and 1 deletions
+41 -1
View File
@@ -12,7 +12,10 @@ import (
"time"
)
const COPY_BUF = 128 * 1024
const (
COPY_BUF = 128 * 1024
WALLCLOCK_PRECISION = 1 * time.Second
)
func basic_auth_header(login, password string) string {
return "Basic " + base64.StdEncoding.EncodeToString(
@@ -143,3 +146,40 @@ func copyBody(wr io.Writer, body io.Reader) {
}
}
}
func AfterWallClock(d time.Duration) <-chan time.Time {
ch := make(chan time.Time, 1)
deadline := time.Now().Add(d).Truncate(0)
after_ch := time.After(d)
ticker := time.NewTicker(WALLCLOCK_PRECISION)
go func() {
var t time.Time
defer ticker.Stop()
for {
select {
case t = <-after_ch:
ch <-t
return
case t = <-ticker.C:
if t.After(deadline) {
ch <-t
return
}
}
}
}()
return ch
}
func runTicker(ctx context.Context, interval time.Duration, cb func (context.Context)) {
go func() {
for {
select {
case <-ctx.Done():
return
case <-AfterWallClock(interval):
cb(ctx)
}
}
}()
}