init
This commit is contained in:
72
logic/dao/dao.go
Normal file
72
logic/dao/dao.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"gitlab.33.cn/chat/im/logic/conf"
|
||||
kafka "gopkg.in/Shopify/sarama.v1"
|
||||
)
|
||||
|
||||
// Dao dao.
|
||||
type Dao struct {
|
||||
c *conf.Config
|
||||
kafkaPub kafka.SyncProducer
|
||||
redis *redis.Pool
|
||||
redisExpire int32
|
||||
}
|
||||
|
||||
// New new a dao and return.
|
||||
func New(c *conf.Config) *Dao {
|
||||
d := &Dao{
|
||||
c: c,
|
||||
kafkaPub: newKafkaPub(c.Kafka),
|
||||
redis: newRedis(c.Redis),
|
||||
redisExpire: int32(time.Duration(c.Redis.Expire) / time.Second),
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func newKafkaPub(c *conf.Kafka) kafka.SyncProducer {
|
||||
kc := kafka.NewConfig()
|
||||
kc.Producer.RequiredAcks = kafka.WaitForAll // Wait for all in-sync replicas to ack the message
|
||||
kc.Producer.Retry.Max = 10 // Retry up to 10 times to produce the message
|
||||
kc.Producer.Return.Successes = true
|
||||
kc.Version = kafka.V0_11_0_2
|
||||
pub, err := kafka.NewSyncProducer(c.Brokers, kc)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return pub
|
||||
}
|
||||
|
||||
func newRedis(c *conf.Redis) *redis.Pool {
|
||||
return &redis.Pool{
|
||||
MaxIdle: c.Idle,
|
||||
MaxActive: c.Active,
|
||||
IdleTimeout: time.Duration(c.IdleTimeout),
|
||||
Dial: func() (redis.Conn, error) {
|
||||
conn, err := redis.Dial(c.Network, c.Addr,
|
||||
redis.DialConnectTimeout(time.Duration(c.DialTimeout)),
|
||||
redis.DialReadTimeout(time.Duration(c.ReadTimeout)),
|
||||
redis.DialWriteTimeout(time.Duration(c.WriteTimeout)),
|
||||
redis.DialPassword(c.Auth),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Close close the resource.
|
||||
func (d *Dao) Close() error {
|
||||
return d.redis.Close()
|
||||
}
|
||||
|
||||
// Ping dao ping.
|
||||
func (d *Dao) Ping(c context.Context) error {
|
||||
return d.pingRedis(c)
|
||||
}
|
||||
Reference in New Issue
Block a user