redishare

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

README ΒΆ

redishare πŸ‡

redishare syncs K/V pairs across processes connected to shared Redis instances.

Values are stored in a local map, and can be uploaded to a primary Redis connection.

Other nodes' uploaded values can be downloaded from a secondary Redis connection.

Default clients have randomly-generated NodeIDs, but you may want to set this to a stable identifier for each server.

Minimal example:

package main

import (
  "fmt"

  "git.sr.ht/~dms/redishare"
  "github.com/go-redis/redis/v8"
)

func main() {
  c := NewDefaultClient("localhost:6378", "localhost:6379", func(err error, values redishare.ScrapedValuesMap) {
    fmt.Printf("Scraped values: %v\n", values)
  })
  
  c.Set("πŸ”‘", "✨")
  c.Share()
  c.Scrape()
  // Scraped values: map[πŸ”‘:map[3de4dc9b:✨]]
}

Non-default client example:

package main

import (
  "fmt"
  "os"
  "time"
  
  "git.sr.ht/~dms/redishare"
  "github.com/go-redis/redis/v8"
)

func main() {
  c := &Client{
    ClientState: redishare.NewClientState(),
    NodeID:      os.Getenv("REDISHARE_NODE_ID"),
    Primary: redis.NewClient(&redis.Options{
      Addr: os.Getenv("PRIMARY_REDIS_ADDR"),
    }),
    Secondary: redis.NewClient(&redis.Options{
      Addr: os.Getenv("SECONDARY_REDIS_ADDR"),
    }),
    ShareTicker:  time.NewTicker(250 * time.Millisecond),
    ScrapeTicker: time.NewTicker(500 * time.Millisecond),
    OnScrape: func(err error, values redishare.ScrapedValuesMap) {
      fmt.Printf("Scraped values: %v\n", values)
    },
    OnShare: func(err error) {
      if err != nil {
        fmt.Printf("Error sharing values: %v\n", err)
      }
    },
  }

  c.Set("πŸ”‘", "✨")
  c.Share()
  c.Scrape()
  // Scraped values: map[πŸ”‘:map[3de4dc9b:✨]]
}

Licensed under BSD 3-clause License (see LICENSE.txt for details).

Copyright Duncan Smith 2022

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Client ΒΆ

type Client struct {
	*ClientState

	// NodeID identifies this node's data
	NodeID string

	// Primary is a client connected to the primary instance
	Primary *redis.Client

	// Secondary is a client connected to the secondary instance
	Secondary *redis.Client

	// ShareTicker defines how frequently values are uploaded
	ShareTicker *time.Ticker

	// ScrapeTicker defines how frequently values are downloaded
	ScrapeTicker *time.Ticker

	// OnScrape is called when an attempt to scrape values is complete.
	OnScrape OnScrapeCallback

	// OnShare is called when an attempt to share values is complete.
	OnShare OnShareCallback
}

Client is an interface to a Redis cluster that allows for easy sharing of values

func NewDefaultClient ΒΆ

func NewDefaultClient(primaryAddr, secondaryAddr string, onScrape OnScrapeCallback) *Client

NewDefaultClient builds a Client with a configuration that should work for most cases

func (*Client) Scrape ΒΆ

func (c *Client) Scrape()

Scrape downloads values for all local keys from the secondary

func (*Client) Set ΒΆ

func (c *Client) Set(key string, value string)

Set sets a value internally which will be uploaded via the connection to the primary

func (*Client) Share ΒΆ

func (c *Client) Share()

Share uploads all local values to the primary

func (*Client) StartScraping ΒΆ

func (c *Client) StartScraping()

StartScraping periodically downloads shared values from the secondary. It should be started as a goroutine. There is no support for stopping.

func (*Client) StartSharing ΒΆ

func (c *Client) StartSharing()

StartSharing starts a goroutine that periodically uploads shared values to the primary

type ClientState ΒΆ

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

ClientState is the internal state of the client

func NewClientState ΒΆ

func NewClientState() *ClientState

NewClientState initializes a new client

type LocalValueMap ΒΆ

type LocalValueMap map[string]string

LocalValueMap is a map containing this node's local representation of each value to be shared and scraped

type OnScrapeCallback ΒΆ

type OnScrapeCallback = func(error, ScrapedValuesMap)

OnScrapeCallback is called when values are scraped

type OnShareCallback ΒΆ

type OnShareCallback = func(error)

OnShareCallback is called when values are shared

type ScrapedValuesMap ΒΆ

type ScrapedValuesMap map[string]SharedValueMap

ScrapedValuesMap is a map from each shared key to the SharedValueMap containing each node's value for that key

type SharedValueMap ΒΆ

type SharedValueMap map[string]string

SharedValueMap is a map containing each node's representation of a value, indexed by the node's ID

Jump to

Keyboard shortcuts

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