redchan

package module
v0.0.0-...-da8f86d Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2017 License: MIT Imports: 8 Imported by: 0

README

redchan

GO Chan over Redis.

We use like Go chan.

install:

go get github.com/bit4bit/redchan

example unbuffered:

...

redisChannel := RedisChannel{[]byte{}, "channeluuid", 0}
defer redchan.Close(redisChannel)
sendCh, sendErr := redchan.Send(redisChannel)
if err := sendErr(); err != nil {
	t.Fatal(sendErr)
}

recvCh, recvErr := redchan.Recv(redisChannel)
if err := recvErr(); err != nil {
		t.Fatal(recvErr)
}

go func(){
	sendCh() <- []byte("mydata")
	sendCh() <- []byte("mydata..")
	close(sendCh())
}()

for recv := <-recvCh {
	data := recv.([]byte)
	...
}


....

example buffered:

...

redisChannel := RedisChannel{[]byte{}, "channeluuid", 2}
defer redchan.Close(redisChannel)
sendCh, sendErr := redchan.Send(redisChannel)
if err := sendErr(); err != nil {
	t.Fatal(sendErr)
}

recvCh, recvErr := redchan.Recv(redisChannel)
if err := recvErr(); err != nil {
		t.Fatal(recvErr)
}

sendCh() <- []byte("mydata")
sendCh() <- []byte("mydata..")
close(sendCh())

<-recvCh
<-recvCh

....

example encoding decoding:

...

type msg struct {
	Message []byte
	Kind int
}

redisChannel := RedisChannel{msg{}, "channeluuid", 0}
defer redchan.Close(redisChannel)
sendCh, sendErr := redchan.Send(redisChannel)
if err := sendErr(); err != nil {
	t.Fatal(sendErr)
}

recvCh, recvErr := redchan.Recv(redisChannel)
if err := recvErr(); err != nil {
		t.Fatal(recvErr)
}

go func(){
	sendCh() <- msg{"hola", 1}
	sendCh() <- msg{"hello", 2}
	close(sendCh())
}()

for recv := <-recvCh {
	mymsg := recv.(msg{})
	...
}


....

Documentation

Overview

Package redchan go lang chan over redis

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(channel redChan, params ...string)

Close Channel

func Send

func Send(channel redChan, params ...string) (SendFunc, ErrorFunc)

Send create chan for send to recv

func SetRedis

func SetRedis(addr string, auth ...string)

SetRedis assign default redis addres and auth

Types

type ErrorFunc

type ErrorFunc func() error

ErrorFunc return last error If this return a error means you need call again Send/Recv because all connections are closed.

func Recv

func Recv(channel redChan, params ...string) (<-chan interface{}, ErrorFunc)

Recv create chan for recv from send

type RedisChannel

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

RedisChannel identify a unique channel on redis for send and receive messages

func (RedisChannel) ChannelID

func (rd RedisChannel) ChannelID() string

ChannelID unique id for channel

func (RedisChannel) ChannelKind

func (rd RedisChannel) ChannelKind() reflect.Type

ChannelKind kind of channel for decoding and encoding any struct

func (RedisChannel) ChannelSize

func (rd RedisChannel) ChannelSize() int

ChannelSize size of channel

type SendFunc

type SendFunc func() chan<- interface{}

SendFunc Wrap chan

Jump to

Keyboard shortcuts

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