Skip to content

Commit

Permalink
Fix the new renew durations
Browse files Browse the repository at this point in the history
I didn't test it before I committed last time but I didn't push it so no
big deal.
  • Loading branch information
fsmv committed Jan 23, 2024
1 parent 6836032 commit f2dd8da
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions portal/gate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var (
NotRegisteredError = errors.New("pattern not registered")
)

const failRetryDelay = 2 * time.Minute

type Client struct {
// Call any of the service.proto functions here
RPC PortalClient
Expand All @@ -74,6 +76,11 @@ var (
Token *string
)

func renewDuration(deadline time.Time) time.Duration {
remainingTime := time.Until(deadline)
return remainingTime - remainingTime/100
}

// Sets up [Address] and [Token] based on optional
// [ask.systems/daemon/portal/flags] values and with fallback to using the
// PORTAL_ADDR and PORTAL_TOKEN environment variable values if the the variables
Expand Down Expand Up @@ -295,7 +302,7 @@ func (c Client) KeepLeaseRenewedTLS(quit <-chan struct{}, lease *Lease, newCert
lease.Pattern)
}()
// Wait until 1% of the time is remaining
timer := time.NewTimer(time.Until(lease.Timeout.AsTime()) / 100)
timer := time.NewTimer(renewDuration(lease.Timeout.AsTime()))
for {
select {
case <-quit:
Expand All @@ -306,7 +313,7 @@ func (c Client) KeepLeaseRenewedTLS(quit <-chan struct{}, lease *Lease, newCert
newLease, err := c.RPC.Renew(context.Background(), lease)
if err != nil {
log.Printf("Error from renew: %v", err)
timer.Reset(2 * time.Minute)
timer.Reset(failRetryDelay)
continue
}
lease = newLease
Expand All @@ -315,7 +322,7 @@ func (c Client) KeepLeaseRenewedTLS(quit <-chan struct{}, lease *Lease, newCert
}
timeout := lease.Timeout.AsTime()
log.Printf("Renewed lease, port: %v, ttl: %v", lease.Port, timeout)
timer.Reset(time.Until(timeout) / 100)
timer.Reset(renewDuration(timeout))
}
}

Expand All @@ -331,15 +338,14 @@ func (c Client) keepRegistrationAlive(quit <-chan struct{}, request *RegisterReq
// re-register. Then we don't have to restart the server.
request.FixedPort = lease.Port
// Wait until 1% of the time is remaining
timer := time.NewTimer(time.Until(lease.Timeout.AsTime()) / 100)
timer := time.NewTimer(renewDuration(lease.Timeout.AsTime()))
for {
select {
case <-quit:
timer.Stop()
return
case <-timer.C:
}
const failRetryDelay = 2 * time.Minute
newLease, err := c.RPC.Renew(context.Background(), lease)
action := "Renewed"
if err != nil && status.Code(err) == codes.NotFound {
Expand All @@ -362,6 +368,6 @@ func (c Client) keepRegistrationAlive(quit <-chan struct{}, request *RegisterReq
}
timeout := lease.Timeout.AsTime()
log.Printf("%v lease, port: %v, ttl: %v", action, lease.Port, timeout)
timer.Reset(time.Until(timeout) / 100)
timer.Reset(renewDuration(timeout))
}
}

0 comments on commit f2dd8da

Please sign in to comment.