Use resettable sync.Once instead of reassignment
This commit is contained in:
parent
51e04c9d0f
commit
909ffd1998
7 changed files with 133 additions and 4 deletions
24
Godeps/_workspace/src/github.com/matryer/resync/.gitignore
generated
vendored
Normal file
24
Godeps/_workspace/src/github.com/matryer/resync/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
27
Godeps/_workspace/src/github.com/matryer/resync/README.md
generated
vendored
Normal file
27
Godeps/_workspace/src/github.com/matryer/resync/README.md
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
# resync
|
||||
|
||||
`sync.Once` with `Reset()`
|
||||
|
||||
* See [sync.Once](http://golang.org/pkg/sync/#Once)
|
||||
|
||||
## Example
|
||||
|
||||
The following example examines how `resync.Once` could be used in a HTTP server situation.
|
||||
|
||||
```
|
||||
// use it just like sync.Once
|
||||
var once resync.Once
|
||||
|
||||
// handle a web request
|
||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
once.Do(func(){
|
||||
// load templates or something
|
||||
})
|
||||
// TODO: respond
|
||||
}
|
||||
|
||||
// handle some request that indicates things have changed
|
||||
func handleResetRequest(w http.ResponseWriter, r *http.Request) {
|
||||
once.Reset() // call Reset to cause initialisation to happen again above
|
||||
}
|
||||
```
|
38
Godeps/_workspace/src/github.com/matryer/resync/once.go
generated
vendored
Normal file
38
Godeps/_workspace/src/github.com/matryer/resync/once.go
generated
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
package resync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Once is an object that will perform exactly one action
|
||||
// until Reset is called.
|
||||
// See http://golang.org/pkg/sync/#Once
|
||||
type Once struct {
|
||||
m sync.Mutex
|
||||
done uint32
|
||||
}
|
||||
|
||||
// Do simulates sync.Once.Do by executing the specified function
|
||||
// only once, until Reset is called.
|
||||
// See http://golang.org/pkg/sync/#Once
|
||||
func (o *Once) Do(f func()) {
|
||||
if atomic.LoadUint32(&o.done) == 1 {
|
||||
return
|
||||
}
|
||||
// Slow-path.
|
||||
o.m.Lock()
|
||||
defer o.m.Unlock()
|
||||
if o.done == 0 {
|
||||
defer atomic.StoreUint32(&o.done, 1)
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
// Reset indicates that the next call to Do should actually be called
|
||||
// once again.
|
||||
func (o *Once) Reset() {
|
||||
o.m.Lock()
|
||||
defer o.m.Unlock()
|
||||
atomic.StoreUint32(&o.done, 0)
|
||||
}
|
35
Godeps/_workspace/src/github.com/matryer/resync/once_test.go
generated
vendored
Normal file
35
Godeps/_workspace/src/github.com/matryer/resync/once_test.go
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
package resync_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cheekybits/is"
|
||||
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/matryer/resync"
|
||||
)
|
||||
|
||||
func TestOnceReset(t *testing.T) {
|
||||
is := is.New(t)
|
||||
var calls int
|
||||
var c resync.Once
|
||||
c.Do(func() {
|
||||
calls++
|
||||
})
|
||||
c.Do(func() {
|
||||
calls++
|
||||
})
|
||||
c.Do(func() {
|
||||
calls++
|
||||
})
|
||||
is.Equal(calls, 1)
|
||||
c.Reset()
|
||||
c.Do(func() {
|
||||
calls++
|
||||
})
|
||||
c.Do(func() {
|
||||
calls++
|
||||
})
|
||||
c.Do(func() {
|
||||
calls++
|
||||
})
|
||||
is.Equal(calls, 2)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue