You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
2.8 KiB
134 lines
2.8 KiB
8 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
8 years ago
|
"io/ioutil"
|
||
8 years ago
|
"time"
|
||
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
|
"github.com/minio/redigo/redis"
|
||
|
)
|
||
|
|
||
8 years ago
|
// redisNotify to send logs to Redis server
|
||
|
type redisNotify struct {
|
||
8 years ago
|
Enable bool `json:"enable"`
|
||
|
Addr string `json:"address"`
|
||
|
Password string `json:"password"`
|
||
|
Key string `json:"key"`
|
||
|
}
|
||
|
|
||
|
type redisConn struct {
|
||
|
*redis.Pool
|
||
8 years ago
|
params redisNotify
|
||
8 years ago
|
}
|
||
|
|
||
|
// Dial a new connection to redis instance at addr, optionally with a password if any.
|
||
8 years ago
|
func dialRedis(rNotify redisNotify) (*redis.Pool, error) {
|
||
8 years ago
|
// Return error if redis not enabled.
|
||
8 years ago
|
if !rNotify.Enable {
|
||
|
return nil, errNotifyNotEnabled
|
||
8 years ago
|
}
|
||
8 years ago
|
addr := rNotify.Addr
|
||
|
password := rNotify.Password
|
||
8 years ago
|
rPool := &redis.Pool{
|
||
|
MaxIdle: 3,
|
||
|
IdleTimeout: 240 * time.Second,
|
||
|
Dial: func() (redis.Conn, error) {
|
||
|
c, err := redis.Dial("tcp", addr)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if password != "" {
|
||
8 years ago
|
if _, derr := c.Do("AUTH", password); derr != nil {
|
||
8 years ago
|
c.Close()
|
||
8 years ago
|
return nil, derr
|
||
8 years ago
|
}
|
||
|
}
|
||
|
return c, err
|
||
|
},
|
||
|
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
||
|
_, err := c.Do("PING")
|
||
|
return err
|
||
|
},
|
||
|
}
|
||
|
|
||
|
// Test if connection with REDIS can be established.
|
||
|
rConn := rPool.Get()
|
||
|
defer rConn.Close()
|
||
|
|
||
|
// Check connection.
|
||
|
_, err := rConn.Do("PING")
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Return pool.
|
||
|
return rPool, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
func newRedisNotify(accountID string) (*logrus.Logger, error) {
|
||
|
rNotify := serverConfig.GetRedisNotifyByID(accountID)
|
||
8 years ago
|
|
||
|
// Dial redis.
|
||
8 years ago
|
rPool, err := dialRedis(rNotify)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
return nil, err
|
||
8 years ago
|
}
|
||
|
|
||
|
rrConn := redisConn{
|
||
|
Pool: rPool,
|
||
8 years ago
|
params: rNotify,
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
redisLog := logrus.New()
|
||
8 years ago
|
|
||
8 years ago
|
redisLog.Out = ioutil.Discard
|
||
8 years ago
|
|
||
|
// Set default JSON formatter.
|
||
8 years ago
|
redisLog.Formatter = new(logrus.JSONFormatter)
|
||
8 years ago
|
|
||
8 years ago
|
redisLog.Hooks.Add(rrConn)
|
||
8 years ago
|
|
||
8 years ago
|
// Success, redis enabled.
|
||
|
return redisLog, nil
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Fire is called when an event should be sent to the message broker.
|
||
8 years ago
|
func (r redisConn) Fire(entry *logrus.Entry) error {
|
||
|
rConn := r.Pool.Get()
|
||
|
defer rConn.Close()
|
||
|
|
||
|
data, err := entry.String()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
_, err = rConn.Do("RPUSH", r.params.Key, data)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Required for logrus hook implementation
|
||
|
func (r redisConn) Levels() []logrus.Level {
|
||
|
return []logrus.Level{
|
||
|
logrus.InfoLevel,
|
||
|
}
|
||
|
}
|