remove packages
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"zero/core/stores/redis"
|
||||
"zero/dq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
consumer := dq.NewConsumer(dq.DqConf{
|
||||
Beanstalks: []dq.Beanstalk{
|
||||
{
|
||||
Endpoint: "localhost:11300",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11301",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11302",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11303",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11304",
|
||||
Tube: "tube",
|
||||
},
|
||||
},
|
||||
Redis: redis.RedisConf{
|
||||
Host: "localhost:6379",
|
||||
Type: redis.NodeType,
|
||||
},
|
||||
})
|
||||
consumer.Consume(func(body []byte) {
|
||||
fmt.Println(string(body))
|
||||
})
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"zero/dq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
producer := dq.NewProducer([]dq.Beanstalk{
|
||||
{
|
||||
Endpoint: "localhost:11300",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11301",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11302",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11303",
|
||||
Tube: "tube",
|
||||
},
|
||||
{
|
||||
Endpoint: "localhost:11304",
|
||||
Tube: "tube",
|
||||
},
|
||||
})
|
||||
for i := 0; i < 5; i++ {
|
||||
_, err := producer.At([]byte(strconv.Itoa(i)), time.Now().Add(time.Second*10))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package main
|
||||
|
||||
import "zero/core/threading"
|
||||
|
||||
func main() {
|
||||
q := threading.NewTaskRunner(5)
|
||||
q.Schedule(func() {
|
||||
panic("hello")
|
||||
})
|
||||
select {}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"Name": "kmq",
|
||||
"Brokers": [
|
||||
"172.16.56.64:19092",
|
||||
"172.16.56.65:19092",
|
||||
"172.16.56.66:19092"
|
||||
],
|
||||
"Group": "adhoc",
|
||||
"Topic": "kevin",
|
||||
"Offset": "first",
|
||||
"NumProducers": 1
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/kq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var c kq.KqConf
|
||||
conf.MustLoad("config.json", &c)
|
||||
|
||||
q := kq.MustNewQueue(c, kq.WithHandle(func(k, v string) error {
|
||||
fmt.Printf("=> %s\n", v)
|
||||
return nil
|
||||
}))
|
||||
defer q.Stop()
|
||||
q.Start()
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"zero/core/cmdline"
|
||||
"zero/kq"
|
||||
)
|
||||
|
||||
type message struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Payload string `json:"message"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
pusher := kq.NewPusher([]string{
|
||||
"172.16.56.64:19092",
|
||||
"172.16.56.65:19092",
|
||||
"172.16.56.66:19092",
|
||||
}, "kevin")
|
||||
|
||||
ticker := time.NewTicker(time.Millisecond)
|
||||
for round := 0; round < 3; round++ {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
count := rand.Intn(100)
|
||||
m := message{
|
||||
Key: strconv.FormatInt(time.Now().UnixNano(), 10),
|
||||
Value: fmt.Sprintf("%d,%d", round, count),
|
||||
Payload: fmt.Sprintf("%d,%d", round, count),
|
||||
}
|
||||
body, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(body))
|
||||
if err := pusher.Push(string(body)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmdline.EnterToContinue()
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"zero/core/discov"
|
||||
"zero/core/lang"
|
||||
"zero/core/logx"
|
||||
"zero/core/service"
|
||||
"zero/core/stores/redis"
|
||||
"zero/rq"
|
||||
)
|
||||
|
||||
var (
|
||||
redisHost = flag.String("redis", "localhost:6379", "")
|
||||
redisType = flag.String("type", "node", "")
|
||||
redisKey = flag.String("key", "queue", "")
|
||||
producers = flag.Int("producers", 1, "")
|
||||
dropBefore = flag.Int64("drop", 0, "messages before seconds to drop")
|
||||
)
|
||||
|
||||
type Consumer struct {
|
||||
lock sync.Mutex
|
||||
resources map[string]interface{}
|
||||
}
|
||||
|
||||
func NewConsumer() *Consumer {
|
||||
return &Consumer{
|
||||
resources: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Consumer) Consume(msg string) error {
|
||||
fmt.Println("=>", msg)
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
c.resources[msg] = lang.Placeholder
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Consumer) OnEvent(event interface{}) {
|
||||
fmt.Printf("event: %+v\n", event)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
consumer := NewConsumer()
|
||||
q, err := rq.NewMessageQueue(rq.RmqConf{
|
||||
ServiceConf: service.ServiceConf{
|
||||
Name: "queue",
|
||||
Log: logx.LogConf{
|
||||
Path: "logs",
|
||||
KeepDays: 3,
|
||||
Compress: true,
|
||||
},
|
||||
},
|
||||
Redis: redis.RedisKeyConf{
|
||||
RedisConf: redis.RedisConf{
|
||||
Host: *redisHost,
|
||||
Type: *redisType,
|
||||
},
|
||||
Key: *redisKey,
|
||||
},
|
||||
Etcd: discov.EtcdConf{
|
||||
Hosts: []string{
|
||||
"localhost:2379",
|
||||
},
|
||||
Key: "queue",
|
||||
},
|
||||
DropBefore: *dropBefore,
|
||||
NumProducers: *producers,
|
||||
}, rq.WithHandler(consumer), rq.WithRenewId(time.Now().UnixNano()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer q.Stop()
|
||||
|
||||
q.Start()
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"zero/core/discov"
|
||||
"zero/rq"
|
||||
|
||||
"github.com/google/gops/agent"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := agent.Listen(agent.Options{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pusher, err := rq.NewPusher([]string{"localhost:2379"}, "queue", rq.WithConsistentStrategy(
|
||||
func(msg string) (string, string, error) {
|
||||
return msg, msg, nil
|
||||
}, discov.BalanceWithId()), rq.WithServerSensitive())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for i := 0; ; i++ {
|
||||
pusher.Push(strconv.Itoa(i))
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
|
||||
"zero/core/logx"
|
||||
"zero/core/queue"
|
||||
"zero/core/service"
|
||||
"zero/core/stores/redis"
|
||||
"zero/rq"
|
||||
)
|
||||
|
||||
var (
|
||||
host = flag.String("s", "10.24.232.63:7002", "server address")
|
||||
mode = flag.String("m", "queue", "cluster test mode")
|
||||
)
|
||||
|
||||
type bridgeHandler struct {
|
||||
pusher queue.QueuePusher
|
||||
}
|
||||
|
||||
func newBridgeHandler() rq.ConsumeHandler {
|
||||
return bridgeHandler{}
|
||||
}
|
||||
|
||||
func (h bridgeHandler) Consume(str string) error {
|
||||
logx.Info("=>", str)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if *mode == "queue" {
|
||||
mq, err := rq.NewMessageQueue(rq.RmqConf{
|
||||
ServiceConf: service.ServiceConf{
|
||||
Log: logx.LogConf{
|
||||
Path: "logs",
|
||||
},
|
||||
},
|
||||
Redis: redis.RedisKeyConf{
|
||||
RedisConf: redis.RedisConf{
|
||||
Host: *host,
|
||||
Type: "cluster",
|
||||
},
|
||||
Key: "notexist",
|
||||
},
|
||||
NumProducers: 1,
|
||||
}, rq.WithHandler(newBridgeHandler()))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer mq.Stop()
|
||||
|
||||
mq.Start()
|
||||
} else {
|
||||
rds := redis.NewRedis(*host, "cluster")
|
||||
rds.Llen("notexist")
|
||||
select {}
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"zero/core/stores/cache"
|
||||
"zero/core/stores/sqlc"
|
||||
"zero/core/stores/sqlx"
|
||||
"zero/kq"
|
||||
)
|
||||
|
||||
var (
|
||||
userRows = "id, mobile, name, sex"
|
||||
|
||||
cacheUserMobilePrefix = "cache#user#mobile#"
|
||||
cacheUserIdPrefix = "cache#user#id#"
|
||||
|
||||
ErrNotFound = sqlc.ErrNotFound
|
||||
)
|
||||
|
||||
type (
|
||||
User struct {
|
||||
Id int64 `db:"id" json:"id,omitempty"`
|
||||
Mobile string `db:"mobile" json:"mobile,omitempty"`
|
||||
Name string `db:"name" json:"name,omitempty"`
|
||||
Sex int `db:"sex" json:"sex,omitempty"`
|
||||
}
|
||||
|
||||
UserModel struct {
|
||||
sqlc.CachedConn
|
||||
// sqlx.SqlConn
|
||||
table string
|
||||
|
||||
// kafka use kq not kmq
|
||||
push *kq.Pusher
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserModel(db sqlx.SqlConn, c cache.CacheConf, table string, pusher *kq.Pusher) *UserModel {
|
||||
return &UserModel{
|
||||
CachedConn: sqlc.NewConn(db, c),
|
||||
table: table,
|
||||
push: pusher,
|
||||
}
|
||||
}
|
||||
|
||||
func (um *UserModel) FindOne(id int64) (*User, error) {
|
||||
key := fmt.Sprintf("%s%d", cacheUserIdPrefix, id)
|
||||
var user User
|
||||
err := um.QueryRow(&user, key, func(conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("SELECT %s FROM user WHERE id=?", userRows)
|
||||
return conn.QueryRow(v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &user, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (um *UserModel) FindByMobile(mobile string) (*User, error) {
|
||||
var user User
|
||||
key := fmt.Sprintf("%s%s", cacheUserMobilePrefix, mobile)
|
||||
err := um.QueryRowIndex(&user, key, func(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%d", cacheUserIdPrefix, primary.(int64))
|
||||
}, func(conn sqlx.SqlConn, v interface{}) (interface{}, error) {
|
||||
query := fmt.Sprintf("SELECT %s FROM user WHERE mobile=?", userRows)
|
||||
if err := conn.QueryRow(&user, query, mobile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user.Id, nil
|
||||
}, func(conn sqlx.SqlConn, v interface{}, primary interface{}) error {
|
||||
return conn.QueryRow(v, "SELECT * FROM user WHERE id=?", primary)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &user, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Count for no cache
|
||||
func (um *UserModel) Count() (int64, error) {
|
||||
var count int64
|
||||
err := um.QueryRowNoCache(&count, "SELECT count(1) FROM user")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// Query rows
|
||||
func (um *UserModel) FindByName(name string) ([]*User, error) {
|
||||
var users []*User
|
||||
query := fmt.Sprintf("SELECT %s FROM user WHERE name=?", userRows)
|
||||
err := um.QueryRowsNoCache(&userRows, query, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (um *UserModel) UpdateSexById(sex int, id int64) error {
|
||||
key := fmt.Sprintf("%s%d", cacheUserIdPrefix, id)
|
||||
_, err := um.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("UPDATE user SET sex=? WHERE id=?")
|
||||
return conn.Exec(query, sex, id)
|
||||
}, key)
|
||||
return err
|
||||
}
|
||||
|
||||
func (um *UserModel) UpdateMobileById(mobile string, id int64) error {
|
||||
idKey := fmt.Sprintf("%s%d", cacheUserIdPrefix, id)
|
||||
mobileKey := fmt.Sprintf("%s%s", cacheUserMobilePrefix, mobile)
|
||||
_, err := um.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("UPDATE user SET mobile=? WHERE id=?")
|
||||
return conn.Exec(query, mobile, id)
|
||||
}, idKey, mobileKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (um *UserModel) Update(u *User) error {
|
||||
oldUser, err := um.FindOne(u.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idKey := fmt.Sprintf("%s%d", cacheUserIdPrefix, oldUser.Id)
|
||||
mobileKey := fmt.Sprintf("%s%s", cacheUserMobilePrefix, oldUser.Mobile)
|
||||
_, err = um.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("UPDATE user SET mobile=?, name=?, sex=? WHERE id=?")
|
||||
return conn.Exec(query, u.Mobile, u.Name, u.Sex, u.Id)
|
||||
}, idKey, mobileKey)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user