Skip to content

Commit

Permalink
Cleanup router when session is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Wilhelmsson authored and 2hdddg committed Jun 3, 2020
1 parent 60d5d32 commit 215c479
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions neo4j/directrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ func (r *directRouter) Writers(database string) ([]string, error) {

func (r *directRouter) Invalidate(database string) {
}

func (r *directRouter) CleanUp() {
}
1 change: 1 addition & 0 deletions neo4j/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type sessionRouter interface {
Readers(database string) ([]string, error)
Writers(database string) ([]string, error)
Invalidate(database string)
CleanUp()
}

type driver struct {
Expand Down
13 changes: 13 additions & 0 deletions neo4j/internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,16 @@ func (r *Router) Invalidate(database string) {
dbRouter.dueUnix = 0
}
}

func (r *Router) CleanUp() {
r.log.Debugf(r.logId, "Cleaning up")
now := r.now().Unix()
r.dbRoutersMut.Lock()
defer r.dbRoutersMut.Unlock()

for dbName, dbRouter := range r.dbRouters {
if now > dbRouter.dueUnix {
delete(r.dbRouters, dbName)
}
}
}
34 changes: 33 additions & 1 deletion neo4j/internal/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestWritersFailAfterNRetries(t *testing.T) {
t.Error("Should have failed")
}
if writers != nil {
t.Error("Should'nt have any writers")
t.Error("Should not have any writers")
}
if numsleep <= 0 {
t.Error("Should have slept plenty")
Expand Down Expand Up @@ -231,3 +231,35 @@ func TestWritersRetriesWhenNoWriters(t *testing.T) {
t.Error("Should have slept once")
}
}

func TestCleanUp(t *testing.T) {
table := &db.RoutingTable{TimeToLive: 1}
pool := &poolFake{
borrow: func(names []string, cancel context.CancelFunc) (poolpackage.Connection, error) {
return &connFake{table: table}, nil
},
}
now := time.Now()
router := New("router", func() []string { return []string{} }, nil, pool, logger)
router.now = func() time.Time { return now }

router.Readers("db1")
router.Readers("db2")

// Should be a router for each requested database
if len(router.dbRouters) != 2 {
t.Fatal("Should be two routing tables, one for each database")
}

// Should not remove these since they still have time to live
router.CleanUp()
if len(router.dbRouters) != 2 {
t.Fatal("Should not have removed routing tables")
}

router.now = func() time.Time { return now.Add(1 * time.Minute) }
router.CleanUp()
if len(router.dbRouters) != 0 {
t.Fatal("Should have cleaned up")
}
}
1 change: 1 addition & 0 deletions neo4j/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func (s *session) Close() error {
// Schedule cleanups
go func() {
s.pool.CleanUp()
s.router.CleanUp()
}()
return nil
}

0 comments on commit 215c479

Please sign in to comment.