Skip to content

Commit 3a47b87

Browse files
committed
Put singleinflght impl. in its own file
1 parent 1b9fe10 commit 3a47b87

File tree

3 files changed

+94
-83
lines changed

3 files changed

+94
-83
lines changed

client.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import (
88
"log"
99
"net/url"
1010
"strings"
11-
"sync"
1211

1312
"github.com/coreos/go-etcd/etcd"
1413
)
1514

16-
var etcdInflight = new(etcdSingle)
17-
1815
func NewClient(machines []string) (client *etcd.Client) {
1916
// set default if not specified in env
2017
if len(machines) == 1 && machines[0] == "" {
@@ -60,44 +57,6 @@ func (s *server) UpdateClient(resp *etcd.Response) {
6057
s.client = c
6158
}
6259

63-
type etcdCall struct {
64-
wg sync.WaitGroup
65-
val *etcd.Response
66-
err error
67-
dups int
68-
}
69-
70-
type etcdSingle struct {
71-
sync.Mutex
72-
m map[string]*etcdCall
73-
}
74-
75-
func (g *etcdSingle) Do(key string, fn func() (*etcd.Response, error)) (*etcd.Response, error, bool) {
76-
g.Lock()
77-
if g.m == nil {
78-
g.m = make(map[string]*etcdCall)
79-
}
80-
if c, ok := g.m[key]; ok {
81-
c.dups++
82-
g.Unlock()
83-
c.wg.Wait()
84-
return c.val, c.err, true
85-
}
86-
c := new(etcdCall)
87-
c.wg.Add(1)
88-
g.m[key] = c
89-
g.Unlock()
90-
91-
c.val, c.err = fn()
92-
c.wg.Done()
93-
94-
g.Lock()
95-
delete(g.m, key)
96-
g.Unlock()
97-
98-
return c.val, c.err, c.dups > 0
99-
}
100-
10160
// get is a wrapper for client.Get that uses SingleInflight to suppress multiple
10261
// outstanding queries.
10362
func get(client *etcd.Client, path string, recursive bool) (*etcd.Response, error) {

dnssec.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ package main
66

77
import (
88
"os"
9-
"sync"
109
"time"
1110

1211
"github.com/miekg/dns"
1312
"github.com/skynetservices/skydns/cache"
1413
)
1514

16-
var inflight = new(single)
17-
1815
// ParseKeyFile read a DNSSEC keyfile as generated by dnssec-keygen or other
1916
// utilities. It add ".key" for the public key and ".private" for the private key.
2017
func ParseKeyFile(file string) (*dns.DNSKEY, dns.PrivateKey, error) {
@@ -161,42 +158,3 @@ func rrSets(rrs []dns.RR) map[rrset][]dns.RR {
161158
}
162159
return nil
163160
}
164-
165-
// Adapted from singleinflight.go from the original Go Code. Copyright 2013 The Go Authors.
166-
type call struct {
167-
wg sync.WaitGroup
168-
val *dns.RRSIG
169-
err error
170-
dups int
171-
}
172-
173-
type single struct {
174-
sync.Mutex
175-
m map[string]*call
176-
}
177-
178-
func (g *single) Do(key string, fn func() (*dns.RRSIG, error)) (*dns.RRSIG, error, bool) {
179-
g.Lock()
180-
if g.m == nil {
181-
g.m = make(map[string]*call)
182-
}
183-
if c, ok := g.m[key]; ok {
184-
c.dups++
185-
g.Unlock()
186-
c.wg.Wait()
187-
return c.val, c.err, true
188-
}
189-
c := new(call)
190-
c.wg.Add(1)
191-
g.m[key] = c
192-
g.Unlock()
193-
194-
c.val, c.err = fn()
195-
c.wg.Done()
196-
197-
g.Lock()
198-
delete(g.m, key)
199-
g.Unlock()
200-
201-
return c.val, c.err, c.dups > 0
202-
}

singleinflight.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2013 Erik St. Martin, Brian Ketelsen. All rights reserved.
2+
// Use of this source code is governed by The MIT License (MIT) that can be
3+
// found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"sync"
9+
10+
"github.com/coreos/go-etcd/etcd"
11+
"github.com/miekg/dns"
12+
)
13+
14+
var (
15+
inflight = new(single)
16+
etcdInflight = new(etcdSingle)
17+
)
18+
19+
// Adapted from singleinflight.go from the original Go Code. Copyright 2013 The Go Authors.
20+
type call struct {
21+
wg sync.WaitGroup
22+
val *dns.RRSIG
23+
err error
24+
dups int
25+
}
26+
27+
type single struct {
28+
sync.Mutex
29+
m map[string]*call
30+
}
31+
32+
func (g *single) Do(key string, fn func() (*dns.RRSIG, error)) (*dns.RRSIG, error, bool) {
33+
g.Lock()
34+
if g.m == nil {
35+
g.m = make(map[string]*call)
36+
}
37+
if c, ok := g.m[key]; ok {
38+
c.dups++
39+
g.Unlock()
40+
c.wg.Wait()
41+
return c.val, c.err, true
42+
}
43+
c := new(call)
44+
c.wg.Add(1)
45+
g.m[key] = c
46+
g.Unlock()
47+
48+
c.val, c.err = fn()
49+
c.wg.Done()
50+
51+
g.Lock()
52+
delete(g.m, key)
53+
g.Unlock()
54+
55+
return c.val, c.err, c.dups > 0
56+
}
57+
58+
type etcdCall struct {
59+
wg sync.WaitGroup
60+
val *etcd.Response
61+
err error
62+
dups int
63+
}
64+
65+
type etcdSingle struct {
66+
sync.Mutex
67+
m map[string]*etcdCall
68+
}
69+
70+
func (g *etcdSingle) Do(key string, fn func() (*etcd.Response, error)) (*etcd.Response, error, bool) {
71+
g.Lock()
72+
if g.m == nil {
73+
g.m = make(map[string]*etcdCall)
74+
}
75+
if c, ok := g.m[key]; ok {
76+
c.dups++
77+
g.Unlock()
78+
c.wg.Wait()
79+
return c.val, c.err, true
80+
}
81+
c := new(etcdCall)
82+
c.wg.Add(1)
83+
g.m[key] = c
84+
g.Unlock()
85+
86+
c.val, c.err = fn()
87+
c.wg.Done()
88+
89+
g.Lock()
90+
delete(g.m, key)
91+
g.Unlock()
92+
93+
return c.val, c.err, c.dups > 0
94+
}

0 commit comments

Comments
 (0)