about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cache.go88
1 files changed, 57 insertions, 31 deletions
diff --git a/cache.go b/cache.go
index e0fedb1..6e58956 100644
--- a/cache.go
+++ b/cache.go
@@ -46,13 +46,19 @@ func cacheAndPush() {
 
 func refreshCache() {
 
+	// This clusterfuck of mutex read locks is
+	// necessary to avoid deadlock. This mess
+	// also avoids a panic that would occur
+	// should twtxtCache be written to during
+	// this loop.
 	twtxtCache.Mu.RLock()
 	for k := range twtxtCache.Users {
+		twtxtCache.Mu.RUnlock()
 		err := twtxtCache.UpdateUser(k)
 		if err != nil {
 			log.Printf("%v\n", err)
-			continue
 		}
+		twtxtCache.Mu.RLock()
 	}
 	twtxtCache.Mu.RUnlock()
 
@@ -120,42 +126,47 @@ func pullDatabase() {
 		urls := split[0]
 		field := split[1]
 
-		if urls != "remote" {
-			data := registry.NewUser()
-			twtxtCache.Mu.RLock()
-			if _, ok := twtxtCache.Users[urls]; ok {
-				data = twtxtCache.Users[urls]
-			}
-			twtxtCache.Mu.RUnlock()
-
-			switch field {
-			case "IP":
-				data.IP = net.ParseIP(val)
-			case "Nick":
-				data.Nick = val
-			case "URL":
-				data.URL = val
-			case "Date":
-				data.Date = val
-			case "Status":
-				thetime, err := time.Parse(time.RFC3339, split[2])
-				if err != nil {
-					log.Printf("%v\n", err)
-				}
-				data.Status[thetime] = val
-			}
-
-			twtxtCache.Mu.Lock()
-			twtxtCache.Users[urls] = data
-			twtxtCache.Mu.Unlock()
-
-		} else {
+		if urls == "remote" {
 			remoteRegistries.Mu.Lock()
 			remoteRegistries.List = append(remoteRegistries.List, val)
 			remoteRegistries.Mu.Unlock()
+			continue
+		}
+
+		data := registry.NewUser()
+		twtxtCache.Mu.RLock()
+		if _, ok := twtxtCache.Users[urls]; ok {
+			data = twtxtCache.Users[urls]
+		}
+		twtxtCache.Mu.RUnlock()
+
+		switch field {
+		case "IP":
+			data.IP = net.ParseIP(val)
+		case "Nick":
+			data.Nick = val
+		case "URL":
+			data.URL = val
+		case "Date":
+			data.Date = val
+		case "Status":
+			thetime, err := time.Parse(time.RFC3339, split[2])
+			if err != nil {
+				log.Printf("%v\n", err)
+			}
+			data.Status[thetime] = val
 		}
+
+		twtxtCache.Mu.Lock()
+		twtxtCache.Users[urls] = data
+		twtxtCache.Mu.Unlock()
+
 	}
 
+	remoteRegistries.Mu.Lock()
+	remoteRegistries.List = dedupe(remoteRegistries.List)
+	remoteRegistries.Mu.Unlock()
+
 	iter.Release()
 	err := iter.Error()
 	if err != nil {
@@ -209,3 +220,18 @@ func pingAssets() {
 		staticCache.cssMod = cssStat.ModTime()
 	}
 }
+
+// Simple function to deduplicate entries in a []string
+func dedupe(list []string) []string {
+	var out []string
+	var seen = map[string]bool{}
+
+	for _, e := range list {
+		if !seen[e] {
+			out = append(out, e)
+			seen[e] = true
+		}
+	}
+
+	return out
+}
d=9867dff44cd0834edc7a26e931cc0e75757aef1f'>9867dff4 ^
f40137f1 ^
263e6b2a ^
f40137f1 ^
263e6b2a ^
f40137f1 ^
263e6b2a ^
8f1db754 ^
e4fc73b1 ^


8f1db754 ^



b24eb476 ^


263e6b2a ^
8f1db754 ^

b24eb476 ^
d0e29245 ^
82ac0b7e ^
b24eb476 ^
82ac0b7e ^
8f1db754 ^

b24eb476 ^
6c96a437 ^
0487a30e ^
ae256ea1 ^
9dcbec39 ^
ae256ea1 ^
0487a30e ^
8f1db754 ^


047296d8 ^

1ead3562 ^
192d59d3 ^
047296d8 ^
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