Project has been created

This commit is contained in:
crusader 2017-08-30 11:06:34 +09:00
commit 435f308cda
6 changed files with 248 additions and 0 deletions

68
.gitignore vendored Normal file
View File

@ -0,0 +1,68 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
# Gradle:
.idea/**/gradle.xml
.idea/**/libraries
# Mongo Explorer plugin:
.idea/**/mongoSettings.xml
## File-based project format:
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Go template
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.idea/
*.iml
vendor/
glide.lock
.DS_Store
dist/
debug

32
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,32 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}/main.go",
"env": {},
"args": [],
"showLog": true
},
{
"name": "File Debug",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}
]
}

11
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,11 @@
// Place your settings in this file to overwrite default and user settings.
{
// Specifies Lint tool name.
"go.lintTool": "gometalinter",
// Flags to pass to Lint tool (e.g. ["-min_confidence=.8"])
"go.lintFlags": [
"--config=${workspaceRoot}/golint.json"
]
}

8
glide.yaml Normal file
View File

@ -0,0 +1,8 @@
package: git.loafle.net/overflow/overflow_subscriber
import:
- package: git.loafle.net/commons_go/util
subpackages:
- channel
- package: github.com/garyburd/redigo
subpackages:
- redis

119
redis/subscriber.go Normal file
View File

@ -0,0 +1,119 @@
package redis
import (
"context"
"log"
channelUtil "git.loafle.net/commons_go/util/channel"
ofSubscriber "git.loafle.net/overflow/overflow_subscriber"
"github.com/garyburd/redigo/redis"
)
type subscribeChannelAction struct {
channelUtil.Action
channel string
cb ofSubscriber.OnSubscribeFunc
}
type Subscriber interface {
ofSubscriber.Subscriber
}
type subscriber struct {
ctx context.Context
conn redis.PubSubConn
subListeners map[string]ofSubscriber.OnSubscribeFunc
isListenSubscriptions bool
subCh chan subscribeChannelAction
}
func New(ctx context.Context, conn redis.Conn) Subscriber {
n := &subscriber{
ctx: ctx,
subListeners: make(map[string]ofSubscriber.OnSubscribeFunc),
isListenSubscriptions: false,
subCh: make(chan subscribeChannelAction),
}
n.conn = redis.PubSubConn{Conn: conn}
go n.listen()
return n
}
func (n *subscriber) listen() {
for {
select {
case sa := <-n.subCh:
switch sa.Type {
case channelUtil.ActionTypeCreate:
_, ok := n.subListeners[sa.channel]
if ok {
log.Fatalf("Subscriber: Subscription of channel[%s] is already exist", sa.channel)
} else {
n.subListeners[sa.channel] = sa.cb
n.conn.Subscribe(sa.channel)
n.listenSubscriptions()
}
break
case channelUtil.ActionTypeDelete:
_, ok := n.subListeners[sa.channel]
if ok {
n.conn.Unsubscribe(sa.channel)
delete(n.subListeners, sa.channel)
} else {
log.Fatalf("Subscriber: Subscription of channel[%s] is not exist", sa.channel)
}
break
}
case <-n.ctx.Done():
log.Println("redis subscriber: Context Done")
n.conn.Close()
return
}
}
}
func (n *subscriber) listenSubscriptions() {
if n.isListenSubscriptions {
return
}
go func() {
for {
switch v := n.conn.Receive().(type) {
case redis.Message:
if cb, ok := n.subListeners[v.Channel]; ok {
cb(v.Channel, string(v.Data))
}
case redis.Subscription:
log.Printf("subscription message: %s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
log.Println("error pub/sub, delivery has stopped")
return
default:
}
}
}()
n.isListenSubscriptions = true
}
func (n *subscriber) Subscribe(channel string, cb ofSubscriber.OnSubscribeFunc) {
ca := subscribeChannelAction{
channel: channel,
cb: cb,
}
ca.Type = channelUtil.ActionTypeCreate
n.subCh <- ca
}
func (n *subscriber) Unsubscribe(channel string) {
ca := subscribeChannelAction{
channel: channel,
}
ca.Type = channelUtil.ActionTypeDelete
n.subCh <- ca
}

10
subscriber.go Normal file
View File

@ -0,0 +1,10 @@
package overflow_subscriber
type (
OnSubscribeFunc func(channel string, payload string)
)
type Subscriber interface {
Subscribe(channel string, cb OnSubscribeFunc)
Unsubscribe(channel string)
}