Skip to content

Commit

Permalink
feat: rename to flagsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
stillmatic committed Jul 3, 2023
1 parent 4b83d3b commit 37c41c1
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 373 deletions.
8 changes: 4 additions & 4 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"net/http"

"github.com/bufbuild/connect-go"
featuresheetv1 "github.com/stillmatic/featuresheet/gen/featuresheet/v1"
"github.com/stillmatic/featuresheet/gen/featuresheet/v1/featuresheetv1connect"
flagsheetv1 "github.com/stillmatic/flagsheet/gen/flagsheet/v1"
"github.com/stillmatic/flagsheet/gen/flagsheet/v1/flagsheetv1connect"
)

func main() {
client := featuresheetv1connect.NewFeatureSheetServiceClient(
client := flagsheetv1connect.NewFlagSheetServiceClient(
http.DefaultClient,
"http://localhost:8080",
)
res, err := client.Evaluate(
context.Background(),
connect.NewRequest(&featuresheetv1.EvaluateRequest{
connect.NewRequest(&flagsheetv1.EvaluateRequest{
Feature: "my_key",
EntityId: "my_id",
}),
Expand Down
6 changes: 3 additions & 3 deletions cmd/loadtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/bufbuild/connect-go"

fsv1 "github.com/stillmatic/featuresheet/gen/featuresheet/v1"
"github.com/stillmatic/featuresheet/gen/featuresheet/v1/featuresheetv1connect"
fsv1 "github.com/stillmatic/flagsheet/gen/flagsheet/v1"
"github.com/stillmatic/flagsheet/gen/flagsheet/v1/flagsheetv1connect"
)

var (
Expand All @@ -20,7 +20,7 @@ var (
)

func main() {
client := featuresheetv1connect.NewFeatureSheetServiceClient(
client := flagsheetv1connect.NewFlagSheetServiceClient(
http.DefaultClient,
"http://localhost:8080",
)
Expand Down
26 changes: 13 additions & 13 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
"gopkg.in/Iwark/spreadsheet.v2"

grpchealth "github.com/bufbuild/connect-grpchealth-go"
"github.com/stillmatic/featuresheet"
fsv1 "github.com/stillmatic/featuresheet/gen/featuresheet/v1"
"github.com/stillmatic/featuresheet/gen/featuresheet/v1/featuresheetv1connect"
"github.com/stillmatic/flagsheet"
fsv1 "github.com/stillmatic/flagsheet/gen/flagsheet/v1"
"github.com/stillmatic/flagsheet/gen/flagsheet/v1/flagsheetv1connect"
)

const (
featureSheetVersionKey = "FeatureSheet-Version"
featureSheetVersionValue = "v1"
flagSheetVersionKey = "FlagSheet-Version"
flagSheetVersionValue = "v1"
)

type FeatureSheetServer struct {
fs *featuresheet.FeatureSheet
type FlagSheetServer struct {
fs *flagsheet.FlagSheet
}

func (s *FeatureSheetServer) Evaluate(
func (s *FlagSheetServer) Evaluate(
ctx context.Context,
req *connect.Request[fsv1.EvaluateRequest],
) (*connect.Response[fsv1.EvaluateResponse], error) {
Expand All @@ -42,7 +42,7 @@ func (s *FeatureSheetServer) Evaluate(
res := connect.NewResponse(&fsv1.EvaluateResponse{
Variant: string(fv),
})
res.Header().Set(featureSheetVersionKey, featureSheetVersionValue)
res.Header().Set(flagSheetVersionKey, flagSheetVersionValue)
return res, nil
}

Expand Down Expand Up @@ -80,20 +80,20 @@ func main() {

client := conf.Client(context.Background())
service := spreadsheet.NewServiceWithClient(client)
fs, err := featuresheet.NewFeatureSheet(service, spreadsheetID, 10*time.Second)
fs, err := flagsheet.NewFlagSheet(service, spreadsheetID, 10*time.Second)
if err != nil {
panic(err)
}

// serving
s := &FeatureSheetServer{
s := &FlagSheetServer{
fs: fs,
}
mux := http.NewServeMux()
path, handler := featuresheetv1connect.NewFeatureSheetServiceHandler(s)
path, handler := flagsheetv1connect.NewFlagSheetServiceHandler(s)
mux.Handle(path, handler)
checker := grpchealth.NewStaticChecker(
"featuresheet.v1.FeatureSheetService",
"flagsheet.v1.FlagSheetService",
)
mux.Handle(grpchealth.NewHandler(checker))
mux.Handle("/health", http.HandlerFunc(ok))
Expand Down
26 changes: 13 additions & 13 deletions featuresheet.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package featuresheet
package flagsheet

import (
"bytes"
Expand Down Expand Up @@ -49,8 +49,8 @@ type Layer struct {
cnt int
}

// featureSheet is an internal representation for goroutine purposes.
type featureSheet struct {
// flagSheet is an internal representation for goroutine purposes.
type flagSheet struct {
sheetID string
service *spreadsheet.Service
expiration time.Duration
Expand All @@ -61,13 +61,13 @@ type featureSheet struct {
fmap map[string]Feature
}

type FeatureSheet struct {
*featureSheet
type FlagSheet struct {
*flagSheet
}

// Evaluate returns the feature variant for a given flagName and id
// if the feature does not exist, it returns an empty string and false
func (f *featureSheet) Evaluate(key string, id *string) (FeatureValue, error) {
func (f *flagSheet) Evaluate(key string, id *string) (FeatureValue, error) {
feature, ok := f.fmap[key]
if !ok {
return "", fmt.Errorf("feature %s not found", key)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (f *featureSheet) Evaluate(key string, id *string) (FeatureValue, error) {
return fv, nil
}

func (f *featureSheet) Refresh() error {
func (f *flagSheet) Refresh() error {
// get spreadsheet
spreadsheet, err := f.service.FetchSpreadsheet(f.sheetID)
if err != nil {
Expand Down Expand Up @@ -188,7 +188,7 @@ type janitor struct {
stop chan bool
}

func (j *janitor) Run(c *featureSheet) {
func (j *janitor) Run(c *flagSheet) {
ticker := time.NewTicker(j.Interval)
for {
select {
Expand All @@ -204,11 +204,11 @@ func (j *janitor) Run(c *featureSheet) {
}
}

func stopJanitor(c *FeatureSheet) {
func stopJanitor(c *FlagSheet) {
c.janitor.stop <- true
}

func runJanitor(c *featureSheet, ci time.Duration) {
func runJanitor(c *flagSheet, ci time.Duration) {
j := &janitor{
Interval: ci,
stop: make(chan bool),
Expand All @@ -217,16 +217,16 @@ func runJanitor(c *featureSheet, ci time.Duration) {
go j.Run(c)
}

func NewFeatureSheet(service *spreadsheet.Service, sheetID string, duration time.Duration) (*FeatureSheet, error) {
fs := &featureSheet{
func NewFlagSheet(service *spreadsheet.Service, sheetID string, duration time.Duration) (*FlagSheet, error) {
fs := &flagSheet{
sheetID: sheetID,
service: service,
expiration: duration,
}
if err := fs.Refresh(); err != nil {
return nil, err
}
FS := &FeatureSheet{fs}
FS := &FlagSheet{fs}
if duration > 0 {
runJanitor(fs, duration)
runtime.SetFinalizer(FS, stopJanitor)
Expand Down
8 changes: 4 additions & 4 deletions featuresheet_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package featuresheet_test
package flagsheet_test

import (
"context"
"os"
"testing"
"time"

"github.com/stillmatic/featuresheet"
"github.com/stillmatic/flagsheet"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2/google"
"gopkg.in/Iwark/spreadsheet.v2"
Expand All @@ -27,7 +27,7 @@ func TestSheet(t *testing.T) {

client := conf.Client(context.TODO())
service := spreadsheet.NewServiceWithClient(client)
spreadsheet, err := featuresheet.NewFeatureSheet(service, testSpreadsheetID, 1*time.Second)
spreadsheet, err := flagsheet.NewFlagSheet(service, testSpreadsheetID, 1*time.Second)
assert.NoError(t, err)
assert.NotNil(t, spreadsheet)
fv, err := spreadsheet.Evaluate("my_key", stringPtr("my_id"))
Expand All @@ -45,7 +45,7 @@ func BenchmarkEvaluate(b *testing.B) {

client := conf.Client(context.TODO())
service := spreadsheet.NewServiceWithClient(client)
spreadsheet, err := featuresheet.NewFeatureSheet(service, testSpreadsheetID, 1*time.Second)
spreadsheet, err := flagsheet.NewFlagSheet(service, testSpreadsheetID, 1*time.Second)
assert.NoError(b, err)
assert.NotNil(b, spreadsheet)
b.ResetTimer()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
syntax = "proto3";

package featuresheet.v1;
package flagsheet.v1;

option go_package = "github.com/stillmatic/featuresheet/gen/featuresheet/v1;featuresheetv1";
option go_package = "github.com/stillmatic/flagsheet/gen/flagsheet/v1;flagsheetv1";

message EvaluateRequest {
string feature = 1;
Expand All @@ -13,6 +13,6 @@ message EvaluateResponse {
string variant = 1;
}

service FeatureSheetService {
service FlagSheetService {
rpc Evaluate(EvaluateRequest) returns (EvaluateResponse);
}
Loading

0 comments on commit 37c41c1

Please sign in to comment.