sessions

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(ctx context.Context, opt ClientOptions) (*Client, error)

NewClient creates a new session client

Example
package main

import (
	"context"
	"fmt"

	"github.com/escaletech/tog-go/sessions"
)

var ctx = context.Background()

func main() {
	sc, err := sessions.NewClient(ctx, sessions.ClientOptions{
		Addr:    "redis://localhost:6379",
		Cluster: false,
	})
	defer sc.Close()

	fmt.Println(err)
}
Output:

<nil>

func (*Client) Close

func (c *Client) Close() error

func (*Client) Session

func (c *Client) Session(ctx context.Context, ns, sid string, opt *SessionOptions) Session
Example (Empty)
package main

import (
	"context"
	"fmt"

	"github.com/escaletech/tog-go/sessions"
)

var ctx = context.Background()

var sc *sessions.Client

func main() {
	sess := sc.Session(ctx, "my_app_empty", "session_id", nil)

	fmt.Println(sess)
}
Output:

map[]
Example (One_flag)
package main

import (
	"context"
	"fmt"

	"github.com/escaletech/tog-go/flags"
	"github.com/escaletech/tog-go/sessions"
)

var ctx = context.Background()
var fc *flags.Client
var sc *sessions.Client

func main() {
	fc.SaveFlag(ctx, flags.Flag{
		Namespace: "my_app_simple",
		Name:      "blue-button",
		Rollout:   []flags.Rollout{{Value: true}},
	})

	sess := sc.Session(ctx, "my_app_simple", "session_id", nil)

	fmt.Println(sess)
}
Output:

map[blue-button:true]
Example (Override_value)
package main

import (
	"context"
	"fmt"

	"github.com/escaletech/tog-go/flags"
	"github.com/escaletech/tog-go/sessions"
)

var ctx = context.Background()
var fc *flags.Client
var sc *sessions.Client

func main() {
	fc.SaveFlag(ctx, flags.Flag{
		Namespace: "my_app_override",
		Name:      "blue-button",
		Rollout:   []flags.Rollout{{Value: false}},
	})

	sess := sc.Session(ctx, "my_app_override", "session_id", &sessions.SessionOptions{
		Force: sessions.Session{"blue-button": true},
	})

	fmt.Println(sess)
}
Output:

map[blue-button:true]
Example (Random)
package main

import (
	"context"
	"fmt"
	"math"

	"github.com/escaletech/tog-go/flags"
	"github.com/escaletech/tog-go/sessions"
)

var ctx = context.Background()
var fc *flags.Client
var sc *sessions.Client

func main() {
	const reps = 100000
	percentage := 50
	fc.SaveFlag(ctx, flags.Flag{
		Namespace: "my_app_random",
		Name:      "blue-button",
		Rollout:   []flags.Rollout{{Value: true, Percentage: &percentage}},
	})

	var trueCount, falseCount float64
	for i := 0; i < reps; i++ {
		sess := sc.Session(ctx, "my_app_random", fmt.Sprintf("session_id_%v", i), nil)
		if sess["blue-button"] {
			trueCount++
		} else {
			falseCount++
		}
	}

	trueRate := math.Round(trueCount / (reps / 100))
	falseRate := math.Round(falseCount / (reps / 100))
	fmt.Println(trueRate, "/", falseRate)
}
Output:

50 / 50
Example (Value_by_traits)
package main

import (
	"context"
	"fmt"

	"github.com/escaletech/tog-go/flags"
	"github.com/escaletech/tog-go/sessions"
)

var ctx = context.Background()
var fc *flags.Client
var sc *sessions.Client

func main() {
	fc.SaveFlag(ctx, flags.Flag{
		Namespace: "my_app_traits",
		Name:      "blue-button",
		Rollout: []flags.Rollout{
			{Value: true, Traits: []string{"early-adopter"}},
			{Value: false},
		},
	})

	earlyAdopterSession := sc.Session(ctx, "my_app_traits", "session_id", &sessions.SessionOptions{
		Traits: []string{"early-adopter"},
	})

	commonSession := sc.Session(ctx, "my_app_traits", "session_id", nil)

	fmt.Println(earlyAdopterSession)
	fmt.Println(commonSession)
}
Output:

map[blue-button:true]
map[blue-button:false]

type ClientOptions

type ClientOptions struct {
	Addr    string
	Cluster bool
	OnError ErrorHandler
}

type ErrorHandler

type ErrorHandler func(context.Context, error)

type Session

type Session map[string]bool

func (Session) IsSet added in v0.1.1

func (s Session) IsSet(name string) bool

type SessionOptions

type SessionOptions struct {
	Force  Session
	Traits []string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL