| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package database |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"strconv" |
| 9 |
|
| 10 |
"github.com/gomodule/redigo/redis" |
| 11 |
) |
| 12 |
|
| 13 |
func (r *Redis) GetListOfMirrors() (map[int]string, error) { |
| 14 |
conn, err := r.Connect() |
| 15 |
if err != nil { |
| 16 |
return nil, err |
| 17 |
} |
| 18 |
defer conn.Close() |
| 19 |
|
| 20 |
values, err := redis.Values(conn.Do("HGETALL", "MIRRORS")) |
| 21 |
if err != nil { |
| 22 |
return nil, err |
| 23 |
} |
| 24 |
|
| 25 |
mirrors := make(map[int]string, len(values)/2) |
| 26 |
|
| 27 |
// Convert the mirror id to int |
| 28 |
for i := 0; i < len(values); i += 2 { |
| 29 |
key, okKey := values[i].([]byte) |
| 30 |
value, okValue := values[i+1].([]byte) |
| 31 |
if !okKey || !okValue { |
| 32 |
return nil, errors.New("invalid type for mirrors key") |
| 33 |
} |
| 34 |
id, err := strconv.Atoi(string(key)) |
| 35 |
if err != nil { |
| 36 |
return nil, errors.New("invalid type for mirrors ID") |
| 37 |
} |
| 38 |
mirrors[id] = string(value) |
| 39 |
} |
| 40 |
|
| 41 |
return mirrors, nil |
| 42 |
} |
| 43 |
|
| 44 |
type NetReadyError struct { |
| 45 |
error |
| 46 |
} |
| 47 |
|
| 48 |
func (n *NetReadyError) Timeout() bool { return false } |
| 49 |
func (n *NetReadyError) Temporary() bool { return true } |
| 50 |
|
| 51 |
func NewNetTemporaryError() NetReadyError { |
| 52 |
return NetReadyError{ |
| 53 |
error: errors.New("database not ready"), |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
type NotReadyError struct{} |
| 58 |
|
| 59 |
func (e *NotReadyError) Close() error { |
| 60 |
return NewNetTemporaryError() |
| 61 |
} |
| 62 |
|
| 63 |
func (e *NotReadyError) Err() error { |
| 64 |
return NewNetTemporaryError() |
| 65 |
} |
| 66 |
|
| 67 |
func (e *NotReadyError) Do(commandName string, args ...any) (reply any, err error) { |
| 68 |
return nil, NewNetTemporaryError() |
| 69 |
} |
| 70 |
|
| 71 |
func (e *NotReadyError) Send(commandName string, args ...any) error { |
| 72 |
return NewNetTemporaryError() |
| 73 |
} |
| 74 |
|
| 75 |
func (e *NotReadyError) Flush() error { |
| 76 |
return NewNetTemporaryError() |
| 77 |
} |
| 78 |
|
| 79 |
func (e *NotReadyError) Receive() (reply any, err error) { |
| 80 |
return nil, NewNetTemporaryError() |
| 81 |
} |