Skip to content

Commit

Permalink
Merge pull request #157 from Michad/bugfix/concurrent-map-access
Browse files Browse the repository at this point in the history
fix: set blend preauth to return a new instance of providercontext
  • Loading branch information
Michad authored Aug 2, 2024
2 parents ce99cfc + d87656a commit 4b794e1
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions internal/providers/blend.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strconv"
"strings"
"sync"
"time"

"github.com/Michad/tilegroxy/pkg"
"github.com/Michad/tilegroxy/pkg/config"
Expand Down Expand Up @@ -129,9 +130,7 @@ func (s BlendRegistration) Initialize(cfgAny any, clientConfig config.ClientConf
}

func (t Blend) PreAuth(ctx *pkg.RequestContext, providerContext layer.ProviderContext) (layer.ProviderContext, error) {
if providerContext.Other == nil {
providerContext.Other = map[string]interface{}{}
}
newProviderContext := layer.ProviderContext{Other: map[string]interface{}{}}

wg := sync.WaitGroup{}
errs := make(chan error, len(t.providers))
Expand All @@ -141,6 +140,12 @@ func (t Blend) PreAuth(ctx *pkg.RequestContext, providerContext layer.ProviderCo
}, len(t.providers))

for i, p := range t.providers {
var thisPc interface{}

if providerContext.Other != nil {
thisPc = providerContext.Other[strconv.Itoa(i)]
}

wg.Add(1)
go func(acObj interface{}, index int, p layer.Provider) {
defer func() {
Expand All @@ -165,20 +170,36 @@ func (t Blend) PreAuth(ctx *pkg.RequestContext, providerContext layer.ProviderCo
}{index, ac}

errs <- err
}(providerContext.Other[strconv.Itoa(i)], i, p)
}(thisPc, i, p)
}

wg.Wait()

errSlice := make([]error, len(t.providers))
allBypass := true
nextExp := time.Now().Add(time.Hour)
for i := range t.providers {
errSlice[i] = <-errs

acStruct := <-acResults
providerContext.Other[strconv.Itoa(i)] = acStruct.ProviderContext
newProviderContext.Other[strconv.Itoa(i)] = acStruct.ProviderContext

if !acStruct.AuthBypass {
allBypass = false
}

if acStruct.AuthExpiration.Before(nextExp) {
nextExp = acStruct.AuthExpiration
}
}

newProviderContext.AuthExpiration = nextExp

if allBypass {
newProviderContext.AuthBypass = true
}

return providerContext, errors.Join(errSlice...)
return newProviderContext, errors.Join(errSlice...)
}

func (t Blend) GenerateTile(ctx *pkg.RequestContext, providerContext layer.ProviderContext, tileRequest pkg.TileRequest) (*pkg.Image, error) {
Expand Down

0 comments on commit 4b794e1

Please sign in to comment.