File: hide.go

package info (click to toggle)
golang-github-getlantern-errors 0.0~git20190325.abdb3e3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 84 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,128 bytes parent folder | download
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
package errors

import (
	"encoding/binary"
	"sync"

	"github.com/getlantern/hidden"
)

var (
	hiddenErrors = make([]*structured, 100)
	nextID       = uint64(0)
	hiddenMutex  sync.RWMutex
)

// This trick saves the error to a ring buffer and embeds a non-printing
// hiddenID in the error's description, so that if the errors is later wrapped
// by a standard error using something like
// fmt.Errorf("An error occurred: %v", thisError), we can subsequently extract
// the error simply using the hiddenID in the string.
func (e *structured) save() {
	hiddenMutex.Lock()
	b := make([]byte, 8)
	binary.BigEndian.PutUint64(b, nextID)
	e.id = nextID
	e.hiddenID = hidden.ToString(b)
	hiddenErrors[idxForID(nextID)] = e
	nextID++
	hiddenMutex.Unlock()
}

func get(hiddenID []byte) Error {
	if len(hiddenID) != 8 {
		return nil
	}
	id := binary.BigEndian.Uint64(hiddenID)
	hiddenMutex.RLock()
	err := hiddenErrors[idxForID(id)]
	hiddenMutex.RUnlock()
	if err != nil && err.id == id {
		// Found it!
		return err
	}
	// buffer has rolled over
	return nil
}

func idxForID(id uint64) int {
	return int(id % uint64(len(hiddenErrors)))
}