Documentation
¶
Overview ¶
Package redissource loads parser-kit data from a Redis key.
It lives in its own package so that importing the root package does not drag go-redis -- and with it cespare/xxhash and go.uber.org/atomic -- into binaries that never read from Redis. A service whose rules live on disk or behind HTTP pays nothing for Redis support existing; only importing this package links it in.
loader, err := parserkit.NewLoader[Rule](nil)
rules, err := loader.Load(ctx,
parserkit.At(0, redissource.New(rdb, "app:rules")),
parserkit.At(1, parserkit.File("/etc/app/rules.json")),
)
The source is a translation layer and nothing more: decoding, priority, merge semantics and the byte budget all live in the root package.
Example ¶
Redis is the primary source, the file on disk the fallback.
package main
import (
"context"
"fmt"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
parserkit "github.com/soulteary/parser-kit/v2"
"github.com/soulteary/parser-kit/v2/redissource"
)
type Rule struct {
ID string `json:"id"`
Action string `json:"action"`
}
func main() {
mr, err := miniredis.Run() // stand-in for a real server
if err != nil {
panic(err)
}
defer mr.Close()
_ = mr.Set("app:rules", `[{"id":"r1","action":"allow"}]`)
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
defer func() { _ = rdb.Close() }()
loader, err := parserkit.NewLoader[Rule](nil)
if err != nil {
panic(err)
}
rules, err := loader.Load(context.Background(),
parserkit.At(0, redissource.New(rdb, "app:rules")),
parserkit.At(1, parserkit.File("/etc/app/rules.json").AllowMissing()),
)
if err != nil {
panic(err)
}
fmt.Println(rules[0].ID, rules[0].Action)
}
Output: r1 allow
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClientNil = errors.New("redissource: redis client is nil")
ErrClientNil reports that there is no Redis client to read from.
Functions ¶
This section is empty.
Types ¶
type Fetcher ¶
type Fetcher struct {
// contains filtered or unexported fields
}
Fetcher reads a JSON payload from one Redis key. It implements parserkit.Fetcher.
func (*Fetcher) AllowMissing ¶
AllowMissing makes a key that does not exist report an absent source rather than an error, so the loader moves on to the next source without the missing key showing up as the reason everything failed.
A key that exists but holds an empty string is still empty data, not an absent source, and a connection failure is still an error.
type Getter ¶
type Getter interface {
Get(ctx context.Context, key string) *redis.StringCmd
StrLen(ctx context.Context, key string) *redis.IntCmd
}
Getter is the part of a go-redis client this source uses. *redis.Client, *redis.ClusterClient, *redis.Ring and redis.UniversalClient all satisfy it, so a source written against it works the same on a standalone server, a cluster and a Sentinel failover setup.
This replaces v1's `RedisClient interface{}`, which accepted anything, gave up at run time on a *redis.ClusterClient, and told the compiler nothing.
Example ¶
Getter is the part of a go-redis client the source uses -- two commands -- so one source works on a standalone server, a Ring and a Sentinel or cluster setup behind redis.UniversalClient, with nothing to switch on.
package main
import (
"context"
"fmt"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
parserkit "github.com/soulteary/parser-kit/v2"
"github.com/soulteary/parser-kit/v2/redissource"
)
func main() {
mr, err := miniredis.Run()
if err != nil {
panic(err)
}
defer mr.Close()
_ = mr.Set("app:rules", `[{"id":"r1","action":"allow"}]`)
clients := []struct {
shape string
client redissource.Getter
}{
{"Client", redis.NewClient(&redis.Options{Addr: mr.Addr()})},
{"Ring", redis.NewRing(&redis.RingOptions{Addrs: map[string]string{"one": mr.Addr()}})},
{"UniversalClient", redis.NewUniversalClient(&redis.UniversalOptions{Addrs: []string{mr.Addr()}})},
}
for _, c := range clients {
raw, err := redissource.New(c.client, "app:rules").Fetch(context.Background(), parserkit.DefaultMaxBytes)
if err != nil {
panic(err)
}
fmt.Printf("%s: %s\n", c.shape, raw)
}
}
Output: Client: [{"id":"r1","action":"allow"}] Ring: [{"id":"r1","action":"allow"}] UniversalClient: [{"id":"r1","action":"allow"}]