Skip to content

Commit

Permalink
project-flotta#118 ECOPROJECT-717 Implement registration-retry with …
Browse files Browse the repository at this point in the history
…exponential backoff
  • Loading branch information
gabriel-farache committed Apr 13, 2022
1 parent 1d2e58c commit cc2e90f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
51 changes: 39 additions & 12 deletions internal/registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

const (
retryAfter = 10
retryAfter = 10
maxInterval = 60
)

//go:generate mockgen -package=registration -destination=mock_deregistrable.go . Deregistrable
Expand All @@ -43,6 +44,7 @@ type Registration struct {
lock sync.RWMutex
deregistrables []Deregistrable
clientCert *ClientCert
nbRetry int
}

func NewRegistration(deviceID string, hardware *hardware2.Hardware, dispatcherClient DispatcherClient,
Expand All @@ -55,6 +57,7 @@ func NewRegistration(deviceID string, hardware *hardware2.Hardware, dispatcherCl
RetryAfter: retryAfter,
workloads: workloadsManager,
lock: sync.RWMutex{},
nbRetry: 0,
}
err := reg.CreateClientCerts()
if err != nil {
Expand Down Expand Up @@ -111,18 +114,36 @@ func (r *Registration) RegisterDevice() {
}

func (r *Registration) registerDeviceWithRetries(interval int64) {
ticker := time.NewTicker(time.Second * time.Duration(interval))
for range ticker.C {
if !r.config.IsInitialConfig() {
ticker.Stop()
break
}
log.Infof("configuration has not been initialized yet. Sending registration request. DeviceID: %s;", r.deviceID)
err := r.registerDeviceOnce()
if err != nil {
log.Errorf("cannot register device. DeviceID: %s; err: %v", r.deviceID, err)
currentInterval := interval
ticker := time.NewTicker(time.Second * time.Duration(currentInterval))

for {
select {
case <-ticker.C:
log.Infof("Current interval: %d", currentInterval)
if !r.config.IsInitialConfig() {
ticker.Stop()
break
}
log.Infof("configuration has not been initialized yet. Sending registration request. DeviceID: %s;", r.deviceID)
err := r.registerDeviceOnce()
if err != nil {
log.Errorf("cannot register device. DeviceID: %s; err: %v", r.deviceID, err)
}
if currentInterval < maxInterval {
currentInterval = currentInterval * 2
if currentInterval > maxInterval {
currentInterval = maxInterval
}
ticker.Stop()
ticker = time.NewTicker(time.Duration(currentInterval) * time.Second)
}
r.nbRetry++

}

}

}

func (r *Registration) registerDeviceOnce() error {
Expand Down Expand Up @@ -172,7 +193,7 @@ func (r *Registration) registerDeviceOnce() error {
var message models.MessageResponse
err = json.Unmarshal(parsedResponse.Body, &message)
if err != nil {
return fmt.Errorf("Cannot unmarshal registration response content: %v", err)
return fmt.Errorf("Cannot unmarshal registration response content: %v", err)
}

parsedContent, ok := message.Content.(map[string]interface{})
Expand Down Expand Up @@ -222,6 +243,12 @@ func (r *Registration) Deregister() error {
return errors
}

func (r *Registration) NbRetry() int {
r.lock.RLock()
defer r.lock.RUnlock()
return r.nbRetry
}

type YGGDResponse struct {
// StatusCode response
StatusCode int
Expand Down
26 changes: 26 additions & 0 deletions internal/registration/registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,32 @@ var _ = Describe("Registration", func() {
Eventually(reg.IsRegistered, "5s").Should(BeTrue())
})

It("Try to re-register exponetial", func() {
// given
reg, err := registration.NewRegistration(deviceID, hw, dispatcherMock, configManager, wkManager)
Expect(err).NotTo(HaveOccurred())

msgResponse := getYggdrasilResponse(models.RegistrationResponse{
Certificate: string(clientCertPem),
})

reg.RetryAfter = 1

// then
dispatcherMock.EXPECT().Send(gomock.Any(), gomock.Any()).Return(
nil, fmt.Errorf("failed")).Times(3)
dispatcherMock.EXPECT().
Send(gomock.Any(), RegistrationMatcher()).
Return(&pb.Response{Response: msgResponse}, nil).
Times(1)

// when
reg.RegisterDevice()

// then
Eventually(reg.IsRegistered, "8s").Should(BeTrue())
Eventually(reg.NbRetry, "8s").Should(Equal(3))
})
})

Context("Deregister", func() {
Expand Down

0 comments on commit cc2e90f

Please sign in to comment.