redisbackendhttpsessionstore

package module
v0.0.0-...-4f657ed Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2016 License: Apache-2.0 Imports: 10 Imported by: 0

README

http-session-redis-sentinel-backend

implements along with gorilla/sessions and go-redis/redis, and with boj/redistore nested

代码基于前2个项目实现, 并继承了后1个项目的结构类型

Getting source into Golang project 引入项目

go get github.com/stackdocker/http-session-redis-sentinel-backend

How to import 导入包名

import redisbackendhttpsessionstore "github.com/stackdocker/http-session-redis-sentinel-backend"

Redis Sentinel client 客户端工具

  • redis_cli

    sudo apt-get install redis-tools or ...

  • nc

    like echo -e 'ping\r\nquit\r\n' | nc 127.0.0.1 26379

  • curl

    echo -e "INFO\r\nQUIT\r\n" | curl telnet://127.0.0.1:6379

Golang redis and sentinel simple test client 内置客户端简单测试程序

cli subdir

redis client command example 运行示例

tangfx@clschool:~/workspace/goprojects/src/github.com/stackdocker/http-session-redis-sentinel-backend/cli (master) $ go run redis-client-simple.go --redis-master 191.168.0.1:6379

PONG <nil>

PONG <nil> ---- PING: PONG

key value

key2 does not exists

sentinel client command example 运行示例

tangfx@clschool:~/workspace/goprojects/src/github.com/stackdocker/http-session-redis-sentinel-backend/cli (master) $ go run sentinel-client-simple.go --address 172.31.33.2:26379,172.31.33.3:26379

Step 1, ping each Sentinel separately...

PONG <nil> ---- PING: PONG

PONG <nil> ---- PING: PONG

Step 2, ping Sentinel cluster...

Golang http server demo session with Redis and/or Sentinel backend 服务器程序示例

wui subdir

System environment variable 环境变量

PORT, default value is 80

Run from source code 运行示例

$ PORT=8080 go run demo-http-session-redis.go --sentinel-mode false --redis-addr 192.168.0.1:6379

Listening on port 8080

logger: demo-http-session-redis.go:182: GET /index.html index

logger: demo-http-session-redis.go:254: session=6B3AHP3RBJ6HBS2KWWFQPN5S6IM4TMT33

or

$ go run demo-http-session-redis.go --sentinel-mode true --master-name mymaster --sentinel-ips 172.31.33.2:26379,172.31.33.3:26379

Waiting Redis Sentiel connection

2015/10/19 17:04:34 redis-sentinel: discovered new "mymaster" sentinel: 10.120.1.3:26379

2015/10/19 17:04:34 redis-sentinel: discovered new "mymaster" sentinel: 10.120.1.4:26379

2015/10/19 17:04:34 redis-sentinel: "mymaster" addr is 10.120.1.5:6379

Sentinel currently unable to response, please try Ping laterly when to access

2015/10/19 17:06:42 redis-sentinel: "mymaster" addr is 10.120.1.5:6379

Failed to create connection! Please contact SysOps

  • Attention 注意

The unexpected thing showing previous command is underlying go-redis v3 client library will try connect internal Redis address. and spend a few time to exit.

The case shows program must communicate with Redis and Sentinel cluster, thus should configured with same subnet, or with DNAT to forward data packages to backend

How to build 编译生成

After go build or go install, copy secret, static, tmpl sub-dirs in where executable is located

  • secret dir, hold a authentication file 该子目录包含密码文件
  • static dir, simple content for index.html 该子目录为主页的静态内容
  • tmpl dir, Golang http/template format 该子目录是模板文件

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SentinelClientConfig

type SentinelClientConfig struct {
	MasterName string
	Addresses  []string
}

type SentinelFailoverStore

type SentinelFailoverStore struct {
	//Codecs             []securecookie.Codec
	//Options            *sessions.Options // default configuration
	//DefaultMaxAge      int     // default Redis TTL for a MaxAge == 0 session
	*redistore.RediStore

	FailoverClient *redis.Client
	// contains filtered or unexported fields
}

SentinelFailoverStore stores sessions in Redis Sentinel Failover.

It also serves as a referece for custom stores.

This store is still experimental and not well tested. Feedback is welcome.

func NewSentinelFailoverStore

func NewSentinelFailoverStore(clientConfig SentinelClientConfig,
	keyPairs ...[]byte) *SentinelFailoverStore

This function returns a new Redis Sentinel store.

Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.

The first key in a pair is used for authentication and the second for encryption. The encryption key can be set to nil or omitted in the last pair, but the authentication key is required in all pairs.

It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.

Use the convenience function securecookie.GenerateRandomKey() to create strong keys.

func (*SentinelFailoverStore) Get

Get returns a session for the given name after adding it to the registry.

It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.

It returns a new session and an error if the session exists but could not be decoded.

func (*SentinelFailoverStore) MaxAge

func (s *SentinelFailoverStore) MaxAge(age int)

MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.

func (*SentinelFailoverStore) MaxLength

func (s *SentinelFailoverStore) MaxLength(l int)

MaxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new SentinelFailoverStore is 4096.

func (*SentinelFailoverStore) New

New returns a session for the given name without adding it to the registry.

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call.

func (*SentinelFailoverStore) Save

Save adds a single session to the response.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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