Archived
1
0

poll varnish connections

This commit is contained in:
Jeff Becker 2018-03-04 07:03:10 -05:00
parent 3eb2c0df0d
commit 702ab469cd
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
2 changed files with 41 additions and 11 deletions

View File

@ -3,6 +3,7 @@ package srnd
import ( import (
"log" "log"
"net/http" "net/http"
"strconv"
) )
type CacheHandler interface { type CacheHandler interface {
@ -45,7 +46,11 @@ func NewCache(cache_type, host, port, user, password string, cache_config, confi
if cache_type == "varnish" { if cache_type == "varnish" {
url := cache_config["url"] url := cache_config["url"]
bind_addr := cache_config["bind"] bind_addr := cache_config["bind"]
return NewVarnishCache(url, bind_addr, prefix, webroot, name, translations, attachments, db, store) workers, _ := strconv.Atoi(cache_config["workers"])
if workers <= 0 {
workers = 4
}
return NewVarnishCache(url, bind_addr, prefix, webroot, name, translations, workers, attachments, db, store)
} }
log.Fatalf("invalid cache type: %s", cache_type) log.Fatalf("invalid cache type: %s", cache_type)

View File

@ -13,7 +13,9 @@ type VarnishCache struct {
prefix string prefix string
handler *nullHandler handler *nullHandler
client *http.Client client *http.Client
workers int
threadsRegenChan chan ArticleEntry threadsRegenChan chan ArticleEntry
invalidateChan chan *url.URL
} }
func (self *VarnishCache) invalidate(r string) { func (self *VarnishCache) invalidate(r string) {
@ -29,6 +31,14 @@ func (self *VarnishCache) invalidate(r string) {
q.Add("lang", lang) q.Add("lang", lang)
u.RawQuery = q.Encode() u.RawQuery = q.Encode()
} }
self.invalidateChan <- u
}
}
func (self *VarnishCache) doRequest(u *url.URL) {
if u == nil {
return
}
resp, err := self.client.Do(&http.Request{ resp, err := self.client.Do(&http.Request{
Method: "PURGE", Method: "PURGE",
URL: u, URL: u,
@ -39,7 +49,6 @@ func (self *VarnishCache) invalidate(r string) {
log.Println("varnish cache error", err) log.Println("varnish cache error", err)
} }
} }
}
func (self *VarnishCache) DeleteBoardMarkup(group string) { func (self *VarnishCache) DeleteBoardMarkup(group string) {
n, _ := self.handler.database.GetPagesPerBoard(group) n, _ := self.handler.database.GetPagesPerBoard(group)
@ -119,6 +128,20 @@ func (self *VarnishCache) poll() {
func (self *VarnishCache) Start() { func (self *VarnishCache) Start() {
go self.poll() go self.poll()
workers := self.workers
if workers <= 0 {
workers = 1
}
for workers > 0 {
go self.doWorker()
workers--
}
}
func (self *VarnishCache) doWorker() {
for {
self.doRequest(<-self.invalidateChan)
}
} }
func (self *VarnishCache) Regen(msg ArticleEntry) { func (self *VarnishCache) Regen(msg ArticleEntry) {
@ -137,9 +160,11 @@ func (self *VarnishCache) SetRequireCaptcha(required bool) {
self.handler.requireCaptcha = required self.handler.requireCaptcha = required
} }
func NewVarnishCache(varnish_url, bind_addr, prefix, webroot, name, translations string, attachments bool, db Database, store ArticleStore) CacheInterface { func NewVarnishCache(varnish_url, bind_addr, prefix, webroot, name, translations string, workers int, attachments bool, db Database, store ArticleStore) CacheInterface {
cache := new(VarnishCache) cache := new(VarnishCache)
cache.invalidateChan = make(chan *url.URL)
cache.threadsRegenChan = make(chan ArticleEntry) cache.threadsRegenChan = make(chan ArticleEntry)
cache.workers = workers
local_addr, err := net.ResolveTCPAddr("tcp", bind_addr) local_addr, err := net.ResolveTCPAddr("tcp", bind_addr)
if err != nil { if err != nil {
log.Fatalf("failed to resolve %s for varnish cache: %s", bind_addr, err) log.Fatalf("failed to resolve %s for varnish cache: %s", bind_addr, err)