Skip to content

Commit

Permalink
Partially fixing #3.
Browse files Browse the repository at this point in the history
On close, drain the deletables channel (unblocking an waiting goroutines) and
close deletables. Like Gets and Sets against a now-closed promotables,
this means any subsequent to Deletes from deletables will panic.

I'm still not sure that this is ccache's responsibility. If a client closes a DB
connection, we'd expect subsequent operations against the now-closed connection
to fail. My main problems with defer'ing a recover are:

1 - the performance overhead on every single get / set / delete
2 - not communicating with the caller that the requested operatin is no longer
    valid.
  • Loading branch information
karlseguin committed Jul 26, 2015
1 parent bfa769c commit 74754c7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,35 @@ func (c *Cache) worker() {
select {
case item, ok := <-c.promotables:
if ok == false {
return
goto drain
}
if c.doPromote(item) && c.size > c.maxSize {
c.gc()
}
case item := <-c.deletables:
if item.element == nil {
item.promotions = -2
} else {
c.size -= item.size
c.list.Remove(item.element)
}
c.doDelete(item)
}
}

drain:
for {
select {
case item := <-c.deletables:
c.doDelete(item)
default:
close(c.deletables)
return
}
}
}

func (c *Cache) doDelete(item *Item) {
if item.element == nil {
item.promotions = -2
} else {
c.size -= item.size
c.list.Remove(item.element)
}
}

func (c *Cache) doPromote(item *Item) bool {
Expand Down

0 comments on commit 74754c7

Please sign in to comment.