Skip to content
Permalink
Browse files

all: fix serverless app timeout

  • Loading branch information
scbizu committed Sep 22, 2019
1 parent 36522d1 commit f0b9844b57b6eab565d1f694703934dae8f84711
Showing with 15 additions and 43 deletions.
  1. +3 −3 .drone.yml
  2. +1 −2 service/feed.go
  3. +4 −38 service/internal/cache/cache.go
  4. +7 −0 service/internal/cache/tl.go
@@ -20,7 +20,7 @@ steps:
from_secret: AWS_SECRET_ACCESS_KEY
AWS_ACCESS_KEY_ID:
from_secret: AWS_ACCESS_KEY_ID
GO111MODULE: on
SERVERLESS: on
commands:
- curl -sf https://up.apex.sh/install | sh
- up deploy staging -v
@@ -38,7 +38,7 @@ steps:
from_secret: AWS_SECRET_ACCESS_KEY
AWS_ACCESS_KEY_ID:
from_secret: AWS_ACCESS_KEY_ID
GO111MODULE: on
SERVERLESS: on
commands:
- curl -sf https://up.apex.sh/install | sh
- up deploy production -v
@@ -52,7 +52,7 @@ steps:
image: plugins/docker
settings:
repo: scnace/feeds
autotag: true
auto_tag: true
username:
from_secret: DOCKERHUB_USER
password:
@@ -26,8 +26,7 @@ func NewFeedService(port int32) FeedService {
func (fs FeedService) Serve(ctx context.Context) error {

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
cache.GetReadySignal()
log.Info().Str("init", "cache ready")
log.Info().Str("init", "cache ready").Send()
w.Header().Set("Content-Type", "text/xml")
data := cache.GetAllMatches()
f := atom.Feeds{Items: data}
@@ -2,43 +2,27 @@ package cache

import (
"context"
"os"
"sync"
"time"

"github.com/rs/zerolog/log"
)

var rdyChan chan struct{}

var data *sync.Map

// Register inits the feeds cache
func Init(ctx context.Context) {
data = new(sync.Map)

go refreshCache(ctx)
}

func GetReadySignal() {
if getSize() > 0 {
return
}
for {
select {
case <-rdyChan:
return
}
// serverless app will not spawn up a goroutine
if os.Getenv("SERVERLESS") == "" {
go refreshCache(ctx)
}
}

func refreshCache(ctx context.Context) {

if getSize() == 0 {
refreshOnceIfEmpty(ctx)
rdyChan <- struct{}{}
return
}

ticker := time.NewTicker(5 * time.Minute)

for {
@@ -52,21 +36,3 @@ func refreshCache(ctx context.Context) {
}

}

func refreshOnceIfEmpty(ctx context.Context) {
if err := buildCache(ctx); err != nil {
log.Warn().Err(err).Send()
}
}

func getSize() int {

var len int

data.Range(func(k, v interface{}) bool {
len++
return true
})

return len
}
@@ -65,5 +65,12 @@ func GetAllMatches() []*tlp.Event {
}
return true
})
// TODO: stackoverflow warning
if len(matches) == 0 {
if err := buildCache(context.Background()); err != nil {
log.Warn().Msgf("get match: %q", err)
}
return GetAllMatches()
}
return matches
}

0 comments on commit f0b9844

Please sign in to comment.
You can’t perform that action at this time.