1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-18 14:47:03 +00:00

queue: Use a PRNG to generate IDs

The queue IDs are internal to chasquid, and we don't need them to be
cryptographically secure, so this patch changes the ID generation to use
the PRNG.

This also helps avoid entropy issues.
This commit is contained in:
Alberto Bertogli
2016-10-25 10:43:55 +01:00
parent 1bc111f783
commit 8cbc4f9ca6

View File

@@ -7,11 +7,10 @@ package queue
import (
"context"
"crypto/rand"
"encoding/base64"
"expvar"
"fmt"
mathrand "math/rand"
"math/rand"
"os"
"os/exec"
"path/filepath"
@@ -66,20 +65,19 @@ var (
var newID chan string
func generateNewIDs() {
// The IDs are only used internally, we are ok with using a PRNG.
// We create our own to avoid relying on external sources initializing it
// properly.
prng := rand.New(rand.NewSource(time.Now().UnixNano()))
// IDs are base64(8 random bytes), but the code doesn't care.
var err error
buf := make([]byte, 8)
id := ""
for {
_, err = rand.Read(buf)
if err != nil {
panic(err)
}
prng.Read(buf)
id = base64.RawURLEncoding.EncodeToString(buf)
newID <- id
}
}
func init() {
@@ -469,7 +467,7 @@ func nextDelay(createdAt time.Time) time.Duration {
// Perturb the delay, to avoid all queued emails to be retried at the
// exact same time after a restart.
delay += time.Duration(mathrand.Intn(60)) * time.Second
delay += time.Duration(rand.Intn(60)) * time.Second
return delay
}