File: utils.go

package info (click to toggle)
mirrorbits 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 984 kB
  • sloc: sh: 675; makefile: 93
file content (81 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2014-2019 Ludovic Fauvet
// Licensed under the MIT license

package database

import (
	"errors"
	"strconv"

	"github.com/gomodule/redigo/redis"
)

func (r *Redis) GetListOfMirrors() (map[int]string, error) {
	conn, err := r.Connect()
	if err != nil {
		return nil, err
	}
	defer conn.Close()

	values, err := redis.Values(conn.Do("HGETALL", "MIRRORS"))
	if err != nil {
		return nil, err
	}

	mirrors := make(map[int]string, len(values)/2)

	// Convert the mirror id to int
	for i := 0; i < len(values); i += 2 {
		key, okKey := values[i].([]byte)
		value, okValue := values[i+1].([]byte)
		if !okKey || !okValue {
			return nil, errors.New("invalid type for mirrors key")
		}
		id, err := strconv.Atoi(string(key))
		if err != nil {
			return nil, errors.New("invalid type for mirrors ID")
		}
		mirrors[id] = string(value)
	}

	return mirrors, nil
}

type NetReadyError struct {
	error
}

func (n *NetReadyError) Timeout() bool   { return false }
func (n *NetReadyError) Temporary() bool { return true }

func NewNetTemporaryError() NetReadyError {
	return NetReadyError{
		error: errors.New("database not ready"),
	}
}

type NotReadyError struct{}

func (e *NotReadyError) Close() error {
	return NewNetTemporaryError()
}

func (e *NotReadyError) Err() error {
	return NewNetTemporaryError()
}

func (e *NotReadyError) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
	return nil, NewNetTemporaryError()
}

func (e *NotReadyError) Send(commandName string, args ...interface{}) error {
	return NewNetTemporaryError()
}

func (e *NotReadyError) Flush() error {
	return NewNetTemporaryError()
}

func (e *NotReadyError) Receive() (reply interface{}, err error) {
	return nil, NewNetTemporaryError()
}