Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snapshot engine in go #326

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flipt-engine-go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
96 changes: 96 additions & 0 deletions flipt-engine-go/engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package flipt_engine_go

import (
"context"
"errors"
"time"

"go.uber.org/zap"
)

type Config struct {
Enabled bool
Host string
Version string
Timeout time.Duration
Interval time.Duration
Namespaces []string
}

type Engine struct {
cfg *Config
http *HTTPParser
snapshot *Snapshot
logger *zap.Logger
}

func NewEngine(
cfg *Config,
http *HTTPParser,
snapshot *Snapshot,
logger *zap.Logger,

) *Engine {
return &Engine{
cfg: cfg,
http: http,
snapshot: snapshot,
logger: logger,
}
}

func (r *Engine) Run(ctx context.Context) {
defer func() {
if p := recover(); p != nil {
r.logger.Error("panic occurred in flipt client-side engine", zap.Any("panic", p))
}
}()

// initial snapshot
err := r.replaceSnapshot(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
r.logger.Error("replace snapshot", zap.Error(err))
}

ticker := time.NewTicker(r.cfg.Interval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := r.replaceSnapshot(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
r.logger.Error("replace snapshot", zap.Error(err))
}
}
}
}

func (r *Engine) replaceSnapshot(ctx context.Context) error {
r.snapshot.cleanStore() // clear from memory before making a backup

for _, namespace := range r.cfg.Namespaces {
response, err := r.http.Do(ctx, namespace)
if err != nil {
return err
}

doc, err := r.http.Parse(response)
if err != nil {
return err
}

evalNamespace := r.snapshot.makeSnapshot(doc)
r.snapshot.replaceStore(evalNamespace)
}

return nil
}
10 changes: 10 additions & 0 deletions flipt-engine-go/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package flipt_engine_go

import (
"errors"
)

var (
ErrNotFound = errors.New("not found")
ErrInvalid = errors.New("invalid")
)
Loading