Skip to content

Commit

Permalink
extcache: Add experiment for extcache
Browse files Browse the repository at this point in the history
  • Loading branch information
robertabcd committed Dec 24, 2021
1 parent 2ac2665 commit af79802
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cached_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ func generateArticle(key cache.Key) (cache.Cacheable, error) {
r := key.(*ArticleRequest)
ctx := context.TODO()
ctx = context.WithValue(ctx, CtxKeyBoardname, r)
ctx = extcache.WithExtCache(ctx, extCache)
if config.Experiments.ExtCache.Enabled(fastStrHash64(r.Filename)) {
ctx = extcache.WithExtCache(ctx, extCache)
}

p, err := r.Select(pttbbs.SelectHead, 0, HeadSize)
if err != nil {
Expand Down Expand Up @@ -332,7 +334,9 @@ func generateArticlePart(key cache.Key) (cache.Cacheable, error) {
r := key.(*ArticlePartRequest)
ctx := context.TODO()
ctx = context.WithValue(ctx, CtxKeyBoardname, r)
ctx = extcache.WithExtCache(ctx, extCache)
if config.Experiments.ExtCache.Enabled(fastStrHash64(r.Filename)) {
ctx = extcache.WithExtCache(ctx, extCache)
}

p, err := ptt.GetArticleSelect(r.Brd.Ref(), pttbbs.SelectHead, r.Filename, r.CacheKey, r.Offset, -1)
if err == pttbbs.ErrNotFound {
Expand Down
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/ptt/pttweb/captcha"
"github.com/ptt/pttweb/experiment"
"github.com/ptt/pttweb/extcache"
)

Expand Down Expand Up @@ -42,6 +43,12 @@ type PttwebConfig struct {
CaptchaRedisConfig *captcha.RedisConfig

ExtCacheConfig extcache.Config

Experiments Experiments
}

type Experiments struct {
ExtCache experiment.OptIn
}

const (
Expand Down
32 changes: 32 additions & 0 deletions experiment/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package experiment

import (
"encoding/json"
"fmt"
)

const percentBase = uint64(10000)

type Percent struct {
threshold uint64
}

func (p *Percent) UnmarshalJSON(b []byte) error {
var pct float64
if err := json.Unmarshal(b, &pct); err != nil {
return err
}
if pct < 0 {
return fmt.Errorf("percent is negative: %v", pct)
}
p.threshold = uint64(pct * float64(percentBase) / 100)
return nil
}

type OptIn struct {
OptIn Percent
}

func (o *OptIn) Enabled(val uint64) bool {
return val%percentBase < o.OptIn.threshold
}
7 changes: 7 additions & 0 deletions pttweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"flag"
"fmt"
"hash/fnv"
"html/template"
"log"
"net"
Expand Down Expand Up @@ -954,3 +955,9 @@ func manSelectType(m pttbbs.SelectMethod) manpb.ArticleRequest_SelectType {
panic("unknown select type")
}
}

func fastStrHash64(s string) uint64 {
h := fnv.New64()
_, _ = h.Write([]byte(s))
return h.Sum64()
}

0 comments on commit af79802

Please sign in to comment.