Skip to content

Commit

Permalink
Fix a lock contention in Signaller controller (#69)
Browse files Browse the repository at this point in the history
The lock contention was observed under a relatively high amount of PURGE
requests, that Signaller propagates among instances in a Varnish
cluster, and changes in endpoints of a K8s service for Varnish;

The Signaller controller locks the "Signaller" structure when it tries
to get/update one of the fields (endpoints) in that structure.

This commit reduces the time when the "Signaller" structure is locked by
copying the current set of endpoints instead of locking the structure
while the controller sends PURGE requests.

Co-authored-by: mqmr <me@mqmr.com>
Co-authored-by: Martin Helmich <m.helmich@mittwald.de>
  • Loading branch information
3 people committed Apr 6, 2021
1 parent 2e455bf commit 6dbce14
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/signaller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/golang/glog"
"github.com/mittwald/kube-httpcache/pkg/watcher"
)

func (b *Signaller) Run() error {
Expand All @@ -35,9 +36,11 @@ func (b *Signaller) ServeHTTP(w http.ResponseWriter, r *http.Request) {
glog.V(5).Infof("received a signal request: %+v", r)

b.mutex.RLock()
defer b.mutex.RUnlock()
endpoints := make([]watcher.Endpoint, len(b.endpoints.Endpoints))
copy(endpoints, b.endpoints.Endpoints)
b.mutex.RUnlock()

for _, endpoint := range b.endpoints.Endpoints {
for _, endpoint := range endpoints {
url := fmt.Sprintf("%s://%s:%s%s", b.EndpointScheme, endpoint.Host, endpoint.Port, r.RequestURI)
request, err := http.NewRequest(r.Method, url, bytes.NewReader(body))
if err != nil {
Expand Down

0 comments on commit 6dbce14

Please sign in to comment.