mirror of
https://github.com/Alexey71/opera-proxy.git
synced 2026-05-14 06:30:59 +00:00
do relogin periodically to refresh account on API side
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user