Skip to content

Commit 215c479

Browse files
Peter Wilhelmsson2hdddg
authored andcommitted
Cleanup router when session is closed
1 parent 60d5d32 commit 215c479

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

neo4j/directrouter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ func (r *directRouter) Writers(database string) ([]string, error) {
3434

3535
func (r *directRouter) Invalidate(database string) {
3636
}
37+
38+
func (r *directRouter) CleanUp() {
39+
}

neo4j/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ type sessionRouter interface {
177177
Readers(database string) ([]string, error)
178178
Writers(database string) ([]string, error)
179179
Invalidate(database string)
180+
CleanUp()
180181
}
181182

182183
type driver struct {

neo4j/internal/router/router.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,16 @@ func (r *Router) Invalidate(database string) {
169169
dbRouter.dueUnix = 0
170170
}
171171
}
172+
173+
func (r *Router) CleanUp() {
174+
r.log.Debugf(r.logId, "Cleaning up")
175+
now := r.now().Unix()
176+
r.dbRoutersMut.Lock()
177+
defer r.dbRoutersMut.Unlock()
178+
179+
for dbName, dbRouter := range r.dbRouters {
180+
if now > dbRouter.dueUnix {
181+
delete(r.dbRouters, dbName)
182+
}
183+
}
184+
}

neo4j/internal/router/router_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestWritersFailAfterNRetries(t *testing.T) {
184184
t.Error("Should have failed")
185185
}
186186
if writers != nil {
187-
t.Error("Should'nt have any writers")
187+
t.Error("Should not have any writers")
188188
}
189189
if numsleep <= 0 {
190190
t.Error("Should have slept plenty")
@@ -231,3 +231,35 @@ func TestWritersRetriesWhenNoWriters(t *testing.T) {
231231
t.Error("Should have slept once")
232232
}
233233
}
234+
235+
func TestCleanUp(t *testing.T) {
236+
table := &db.RoutingTable{TimeToLive: 1}
237+
pool := &poolFake{
238+
borrow: func(names []string, cancel context.CancelFunc) (poolpackage.Connection, error) {
239+
return &connFake{table: table}, nil
240+
},
241+
}
242+
now := time.Now()
243+
router := New("router", func() []string { return []string{} }, nil, pool, logger)
244+
router.now = func() time.Time { return now }
245+
246+
router.Readers("db1")
247+
router.Readers("db2")
248+
249+
// Should be a router for each requested database
250+
if len(router.dbRouters) != 2 {
251+
t.Fatal("Should be two routing tables, one for each database")
252+
}
253+
254+
// Should not remove these since they still have time to live
255+
router.CleanUp()
256+
if len(router.dbRouters) != 2 {
257+
t.Fatal("Should not have removed routing tables")
258+
}
259+
260+
router.now = func() time.Time { return now.Add(1 * time.Minute) }
261+
router.CleanUp()
262+
if len(router.dbRouters) != 0 {
263+
t.Fatal("Should have cleaned up")
264+
}
265+
}

neo4j/session.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ func (s *session) Close() error {
471471
// Schedule cleanups
472472
go func() {
473473
s.pool.CleanUp()
474+
s.router.CleanUp()
474475
}()
475476
return nil
476477
}

0 commit comments

Comments
 (0)