Skip to content

Commit f625d4e

Browse files
Ensure resiliency for route cleanup (#68)
If there is temporary errors with database connection, we want to make sure that it's eventually cleaned up.
1 parent d8c349a commit f625d4e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

db/db_sql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func (s *SqlDB) CleanupRoutes(logger lager.Logger, pruningInterval time.Duration
152152
case <-pruningTicker.C:
153153
if atomic.CompareAndSwapInt32(&tcpInFlight, 0, 1) {
154154
go func() {
155+
defer atomic.StoreInt32(&tcpInFlight, 0)
155156
var tcpRoutes []models.TcpRouteMapping
156157
err := s.FindExpiredRoutes(&tcpRoutes, clock)
157158
if err != nil {
@@ -175,12 +176,12 @@ func (s *SqlDB) CleanupRoutes(logger lager.Logger, pruningInterval time.Duration
175176
}
176177

177178
logger.Info("successfully-finished-pruning-tcp-routes", lager.Data{"rowsAffected": rowsAffected})
178-
atomic.StoreInt32(&tcpInFlight, 0)
179179
}()
180180
}
181181

182182
if atomic.CompareAndSwapInt32(&httpInFlight, 0, 1) {
183183
go func() {
184+
defer atomic.StoreInt32(&httpInFlight, 0)
184185
var httpRoutes []models.Route
185186
err := s.FindExpiredRoutes(&httpRoutes, clock)
186187
if err != nil {
@@ -204,7 +205,6 @@ func (s *SqlDB) CleanupRoutes(logger lager.Logger, pruningInterval time.Duration
204205
}
205206

206207
logger.Info("successfully-finished-pruning-http-routes", lager.Data{"rowsAffected": rowsAffected})
207-
atomic.StoreInt32(&httpInFlight, 0)
208208
}()
209209
}
210210
case <-signals:

db/db_sql_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,39 @@ var _ = Describe("SqlDB", func() {
18931893
Eventually(logger, 2).Should(gbytes.Say(`failed-to-prune-.*-routes","log_level":2,"data":{"error":"sql: database is closed"}`))
18941894
})
18951895
})
1896+
1897+
Context("when db throws a temporary error", func() {
1898+
var (
1899+
fakeClient *fakes.FakeClient
1900+
count int32
1901+
)
1902+
1903+
BeforeEach(func() {
1904+
fakeClient = &fakes.FakeClient{}
1905+
sqlDB.Client = fakeClient
1906+
fakeClient.FindReturns(nil)
1907+
fakeClient.DeleteStub = func(value interface{}, where ...interface{}) (int64, error) {
1908+
time.Sleep(500 * time.Millisecond)
1909+
c := atomic.AddInt32(&count, 1)
1910+
if c > 5 && c < 10 {
1911+
return 0, errors.New("temp-error")
1912+
} else if c >= 10 {
1913+
return 111, nil
1914+
} else {
1915+
return 1, nil
1916+
}
1917+
}
1918+
})
1919+
1920+
It("eventually resolves the issue", func() {
1921+
Eventually(logger, 2).Should(gbytes.Say(`"prune.successfully-finished-pruning-tcp-routes","log_level":1,"data":{"rowsAffected":1}`))
1922+
Eventually(logger, 2).Should(gbytes.Say(`"prune.successfully-finished-pruning-http-routes","log_level":1,"data":{"rowsAffected":1}`))
1923+
Eventually(logger, 2).Should(gbytes.Say(`failed-to-prune-tcp-routes","log_level":2,"data":{"error":"temp-error"}`))
1924+
Eventually(logger, 2).Should(gbytes.Say(`failed-to-prune-http-routes","log_level":2,"data":{"error":"temp-error"}`))
1925+
Eventually(logger, 2).Should(gbytes.Say(`"prune.successfully-finished-pruning-tcp-routes","log_level":1,"data":{"rowsAffected":111}`))
1926+
Eventually(logger, 2).Should(gbytes.Say(`"prune.successfully-finished-pruning-http-routes","log_level":1,"data":{"rowsAffected":111}`))
1927+
})
1928+
})
18961929
})
18971930
}
18981931

0 commit comments

Comments
 (0)