예제 #1
0
func NewRedisPool() *redis.Pool {
	return &redis.Pool{
		MaxIdle:   config.GetInt("redis_max_idle"),
		MaxActive: config.GetInt("redis_concurrent"), // max number of connections
		Dial: func() (redis.Conn, error) {
			var (
				c   redis.Conn
				err error
			)
			redis_host := fmt.Sprintf("%s:%s", config.GetMulti("redis_host", "redis_port")...)

			redis_passwd := config.Get("redis_passwd")
			if redis_passwd != "" {
				pwd := redis.DialPassword(redis_passwd)
				c, err = redis.Dial("tcp", redis_host, pwd)
			} else {
				c, err = redis.Dial("tcp", redis_host)
			}

			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}
예제 #2
0
func init() {

	go func() {
		sleep_time := time.Duration(config.GetInt("publish_timeout_second")+10) * time.Second
		for {
			select {
			case msg := <-OldMessagesQueue:
				MessagePool.Put(msg)
				//
			default:
				time.Sleep(sleep_time)

			}

		}
	}()

	go func() {
		for {
			select {
			case topic := <-OfflineTopicCleanProcessor:
				Log.Debugc(func() string {
					return fmt.Sprintf("clean offline topic queue: %s", topic)
				})

				OfflineTopicRWmux.RLock()
				q := OfflineTopicMap[topic]
				OfflineTopicRWmux.RUnlock()
				if q != nil {
					go q.Clean()
				}
			case msg := <-OfflineTopicQueueProcessor:
				//         _ = msg
				//         topic := string(msg.Topic())
				go func(topic_bytes, payload []byte) {
					//         func(topic string, payload []byte) {
					topic := string(topic_bytes)
					OfflineTopicRWmux.RLock()
					q := OfflineTopicMap[topic]
					OfflineTopicRWmux.RUnlock()
					if q == nil {
						q = NewOfflineTopicQueue(topic)

						OfflineTopicRWmux.Lock()
						OfflineTopicMap[topic] = q
						OfflineTopicRWmux.Unlock()
					}

					Log.Debugc(func() string {
						return fmt.Sprintf("add offline message to the topic: %s", topic)
						// return fmt.Sprintf("add offline message: topic: %s, payload: %s", msg.Topic(), msg.Payload())
					})

					q.Add(payload)
					_return_tmp_msg(msg)
				}(msg.Topic(), msg.Payload())

			}
		}
	}()

	go func() {
		for client := range ClientMapProcessor {
			client_id := client.Name
			client_conn := client.Conn

			if ClientMap[client_id] != nil {
				old_conn := *ClientMap[client_id]
				old_conn.Close()

				Log.Debugc(func() string {
					return fmt.Sprintf("client connected with same client_id: %s. close old connection.", client_id)
				})
			}
			ClientMap[client_id] = client_conn
		}
	}()

}