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
9 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 (
|
||
9 years ago
|
"io/ioutil"
|
||
9 years ago
|
"time"
|
||
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
|
"github.com/minio/redigo/redis"
|
||
|
)
|
||
|
|
||
9 years ago
|
// redisNotify to send logs to Redis server
|
||
|
type redisNotify struct {
|
||
9 years ago
|
Enable bool `json:"enable"`
|
||
|
Addr string `json:"address"`
|
||
|
Password string `json:"password"`
|
||
|
Key string `json:"key"`
|
||
|
}
|
||
|
|
||
|
type redisConn struct {
|
||
|
*redis.Pool
|
||
9 years ago
|
params redisNotify
|
||
9 years ago
|
}
|
||
|
|
||
|
// Dial a new connection to redis instance at addr, optionally with a password if any.
|
||
9 years ago
|
func dialRedis(rNotify redisNotify) (*redis.Pool, error) {
|
||
9 years ago
|
// Return error if redis not enabled.
|
||
9 years ago
|
if !rNotify.Enable {
|
||
|
return nil, errNotifyNotEnabled
|
||
9 years ago
|
}
|
||
9 years ago
|
addr := rNotify.Addr
|
||
|
password := rNotify.Password
|
||
9 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 != "" {
|
||
9 years ago
|
if _, derr := c.Do("AUTH", password); derr != nil {
|
||
9 years ago
|
c.Close()
|
||
9 years ago
|
return nil, derr
|
||
9 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
|
||
|
}
|
||
|
|
||
9 years ago
|
func newRedisNotify(accountID string) (*logrus.Logger, error) {
|
||
|
rNotify := serverConfig.GetRedisNotifyByID(accountID)
|
||
9 years ago
|
|
||
|
// Dial redis.
|
||
9 years ago
|
rPool, err := dialRedis(rNotify)
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, err
|
||
9 years ago
|
}
|
||
|
|
||
|
rrConn := redisConn{
|
||
|
Pool: rPool,
|
||
9 years ago
|
params: rNotify,
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
redisLog := logrus.New()
|
||
9 years ago
|
|
||
9 years ago
|
redisLog.Out = ioutil.Discard
|
||
9 years ago
|
|
||
|
// Set default JSON formatter.
|
||
9 years ago
|
redisLog.Formatter = new(logrus.JSONFormatter)
|
||
9 years ago
|
|
||
9 years ago
|
redisLog.Hooks.Add(rrConn)
|
||
9 years ago
|
|
||
9 years ago
|
// Success, redis enabled.
|
||
|
return redisLog, nil
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Fire is called when an event should be sent to the message broker.
|
||
9 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,
|
||
|
}
|
||
|
}
|