├── go.mod ├── go.sum ├── .gitignore ├── prime.go ├── bench_test.go ├── shardedsf.go ├── options.go ├── shardedsf_test.go ├── README.md └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tarndt/shardedsingleflight 2 | 3 | go 1.17 4 | 5 | require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= 2 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | -------------------------------------------------------------------------------- /prime.go: -------------------------------------------------------------------------------- 1 | package shardedsingleflight 2 | 3 | import ( 4 | "math/big" 5 | ) 6 | 7 | func nextPrime(n uint64) uint64 { 8 | switch n { 9 | case 0, 1: 10 | return 2 11 | case 2, 3: 12 | return n 13 | } 14 | 15 | if n%2 == 0 { 16 | n++ 17 | } 18 | 19 | //There are very small gaps between consecutive small primes, brute force it 20 | i, one := big.NewInt(int64(n)), big.NewInt(1) 21 | for { 22 | if i.ProbablyPrime(0) { //ProbablyPrime is 100% accurate for inputs less than 2^64 23 | return i.Uint64() 24 | } 25 | i.Add(i, one) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /bench_test.go: -------------------------------------------------------------------------------- 1 | package shardedsingleflight 2 | 3 | import ( 4 | "crypto/rand" 5 | "fmt" 6 | "io" 7 | "testing" 8 | 9 | "golang.org/x/sync/singleflight" 10 | ) 11 | 12 | type doer interface { 13 | Do(string, func() (interface{}, error)) (interface{}, error, bool) 14 | } 15 | 16 | func BenchmarkDo(b *testing.B) { 17 | keys := randKeys(b, 1024, 10) 18 | 19 | b.Run("noshard", func(b *testing.B) { 20 | benchDo(b, new(singleflight.Group), keys) 21 | }) 22 | 23 | hashes := []struct { 24 | constr NewHash 25 | name string 26 | }{ 27 | {FNV64, "fnv64"}, {FNV64a, "fnv64a"}, 28 | {CRCISO, "crc-iso"}, {CRCECMA, "crc-ecma"}, 29 | {MapHash, "maphash"}, 30 | } 31 | 32 | for _, hash := range hashes { 33 | b.Run(fmt.Sprintf("shard-hash-"+hash.name), func(b *testing.B) { 34 | benchDo(b, NewShardedGroup(WithHashFunc(hash.constr)), keys) 35 | }) 36 | } 37 | } 38 | 39 | func benchDo(b *testing.B, g doer, keys []string) { 40 | keyc := len(keys) 41 | b.ReportAllocs() 42 | b.ResetTimer() 43 | 44 | b.RunParallel(func(pb *testing.PB) { 45 | for i := 0; pb.Next(); i++ { 46 | g.Do(keys[i%keyc], func() (interface{}, error) { 47 | return nil, nil 48 | }) 49 | } 50 | }) 51 | } 52 | 53 | func randKeys(b *testing.B, count, length uint) []string { 54 | keys := make([]string, 0, count) 55 | key := make([]byte, length) 56 | 57 | for i := uint(0); i < count; i++ { 58 | if _, err := io.ReadFull(rand.Reader, key); err != nil { 59 | b.Fatalf("Failed to generate random key %d of %d of length %d: %s", i+1, count, length, err) 60 | } 61 | keys = append(keys, string(key)) 62 | } 63 | return keys 64 | } 65 | -------------------------------------------------------------------------------- /shardedsf.go: -------------------------------------------------------------------------------- 1 | package shardedsingleflight 2 | 3 | import ( 4 | "golang.org/x/sync/singleflight" 5 | ) 6 | 7 | //ShardedGroup is a sharded singleflight Group. See singleflight.Group 8 | type ShardedGroup struct { 9 | newHash NewHash 10 | shardc uint64 11 | shards []singleflight.Group 12 | } 13 | 14 | //NewShardedGroup is the sharded singleflight Group contstructor. Any provided 15 | //options, which may be absent, will be applied. 16 | func NewShardedGroup(opts ...GroupOptions) *ShardedGroup { 17 | sg := &ShardedGroup{ 18 | newHash: DefHashFunc, 19 | shardc: DefShardCount, 20 | } 21 | 22 | for _, opt := range opts { 23 | opt.apply(sg) 24 | } 25 | sg.shards = make([]singleflight.Group, sg.shardc) 26 | 27 | return sg 28 | } 29 | 30 | //Do maps the key to a shard and runs the singleflight shard's Do. 31 | // See singleflight.Do 32 | func (sg *ShardedGroup) Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool) { 33 | return sg.shards[sg.shardIdx(key)].Do(key, fn) 34 | } 35 | 36 | //DoChan maps the key to a shard and runs the singleflight shard's DoChan. 37 | // See singleflight.DoChan 38 | func (sg *ShardedGroup) DoChan(key string, fn func() (interface{}, error)) <-chan singleflight.Result { 39 | return sg.shards[sg.shardIdx(key)].DoChan(key, fn) 40 | } 41 | 42 | //Forget maps the key to a shard and runs the singleflight shard's Forget. 43 | // See singleflight.Forget 44 | func (sg *ShardedGroup) Forget(key string) { 45 | sg.shards[sg.shardIdx(key)].Forget(key) 46 | } 47 | 48 | func (sg *ShardedGroup) shardIdx(key string) uint64 { 49 | hasher := sg.newHash() 50 | hasher.Write([]byte(key)) 51 | return hasher.Sum64() % sg.shardc 52 | } 53 | -------------------------------------------------------------------------------- /options.go: -------------------------------------------------------------------------------- 1 | package shardedsingleflight 2 | 3 | import ( 4 | "hash" 5 | "hash/crc64" 6 | "hash/fnv" 7 | "hash/maphash" 8 | "runtime" 9 | ) 10 | 11 | //GroupOptions represents an option that can be provided to the ShardGroup constructor 12 | type GroupOptions interface { 13 | apply(g *ShardedGroup) 14 | } 15 | 16 | var ( 17 | _ GroupOptions = WithShardCount(0) 18 | _ GroupOptions = WithHashFunc(DefHashFunc) 19 | ) 20 | 21 | //WithShardCount is an option to override the default shard count with an explicit count 22 | type WithShardCount uint64 23 | 24 | func (shardCount WithShardCount) apply(g *ShardedGroup) { 25 | g.shardc = uint64(shardCount) 26 | } 27 | 28 | //DefShardCount is the heuristically determined default shard count. It will vary 29 | // by machine taking into consideration factors such as the number of cores present 30 | var DefShardCount = nextPrime(uint64(runtime.NumCPU() * 7)) 31 | 32 | //NewHash is the type for constructors of hash.Hash64 instances 33 | type NewHash func() hash.Hash64 34 | 35 | //WithHashFunc is an option to override the default hash.Hash64 constructor used 36 | // by a ShardGroup 37 | type WithHashFunc NewHash 38 | 39 | func (hashFunc WithHashFunc) apply(g *ShardedGroup) { 40 | g.newHash = NewHash(hashFunc) 41 | } 42 | 43 | //The following are the hash.Hash64 constructors provided by various Go stdlib packages 44 | var ( 45 | DefHashFunc = FNV64a 46 | FNV64a = fnv.New64a 47 | FNV64 = fnv.New64 48 | CRCISO = func() hash.Hash64 { return crc64.New(crc64.MakeTable(crc64.ISO)) } 49 | CRCECMA = func() hash.Hash64 { return crc64.New(crc64.MakeTable(crc64.ECMA)) } 50 | MapHash = func() hash.Hash64 { return new(maphash.Hash) } 51 | ) 52 | -------------------------------------------------------------------------------- /shardedsf_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package shardedsingleflight 6 | 7 | import ( 8 | "errors" 9 | "fmt" 10 | "sync" 11 | "sync/atomic" 12 | "testing" 13 | "time" 14 | ) 15 | 16 | func TestDo(t *testing.T) { 17 | g := NewShardedGroup() 18 | v, err, _ := g.Do("key", func() (interface{}, error) { 19 | return "bar", nil 20 | }) 21 | if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want { 22 | t.Errorf("Do = %v; want %v", got, want) 23 | } 24 | if err != nil { 25 | t.Errorf("Do error = %v", err) 26 | } 27 | } 28 | 29 | func TestDoErr(t *testing.T) { 30 | g := NewShardedGroup() 31 | someErr := errors.New("Some error") 32 | v, err, _ := g.Do("key", func() (interface{}, error) { 33 | return nil, someErr 34 | }) 35 | if err != someErr { 36 | t.Errorf("Do error = %v; want someErr %v", err, someErr) 37 | } 38 | if v != nil { 39 | t.Errorf("unexpected non-nil value %#v", v) 40 | } 41 | } 42 | 43 | func TestDoDupSuppress(t *testing.T) { 44 | g := NewShardedGroup() 45 | var wg1, wg2 sync.WaitGroup 46 | c := make(chan string, 1) 47 | var calls int32 48 | fn := func() (interface{}, error) { 49 | if atomic.AddInt32(&calls, 1) == 1 { 50 | // First invocation. 51 | wg1.Done() 52 | } 53 | v := <-c 54 | c <- v // pump; make available for any future calls 55 | 56 | time.Sleep(10 * time.Millisecond) // let more goroutines enter Do 57 | 58 | return v, nil 59 | } 60 | 61 | const n = 10 62 | wg1.Add(1) 63 | for i := 0; i < n; i++ { 64 | wg1.Add(1) 65 | wg2.Add(1) 66 | go func() { 67 | defer wg2.Done() 68 | wg1.Done() 69 | v, err, _ := g.Do("key", fn) 70 | if err != nil { 71 | t.Errorf("Do error: %v", err) 72 | return 73 | } 74 | if s, _ := v.(string); s != "bar" { 75 | t.Errorf("Do = %T %v; want %q", v, v, "bar") 76 | } 77 | }() 78 | } 79 | wg1.Wait() 80 | // At least one goroutine is in fn now and all of them have at 81 | // least reached the line before the Do. 82 | c <- "bar" 83 | wg2.Wait() 84 | if got := atomic.LoadInt32(&calls); got <= 0 || got >= n { 85 | t.Errorf("number of calls = %d; want over 0 and less than %d", got, n) 86 | } 87 | } 88 | 89 | // Test that singleflight behaves correctly after Forget called. 90 | // See https://github.com/golang/go/issues/31420 91 | func TestForget(t *testing.T) { 92 | g := NewShardedGroup() 93 | 94 | var ( 95 | firstStarted = make(chan struct{}) 96 | unblockFirst = make(chan struct{}) 97 | firstFinished = make(chan struct{}) 98 | ) 99 | 100 | go func() { 101 | g.Do("key", func() (i interface{}, e error) { 102 | close(firstStarted) 103 | <-unblockFirst 104 | close(firstFinished) 105 | return 106 | }) 107 | }() 108 | <-firstStarted 109 | g.Forget("key") 110 | 111 | unblockSecond := make(chan struct{}) 112 | secondResult := g.DoChan("key", func() (i interface{}, e error) { 113 | <-unblockSecond 114 | return 2, nil 115 | }) 116 | 117 | close(unblockFirst) 118 | <-firstFinished 119 | 120 | thirdResult := g.DoChan("key", func() (i interface{}, e error) { 121 | return 3, nil 122 | }) 123 | 124 | close(unblockSecond) 125 | <-secondResult 126 | r := <-thirdResult 127 | if r.Val != 2 { 128 | t.Errorf("We should receive result produced by second call, expected: 2, got %d", r.Val) 129 | } 130 | } 131 | 132 | func TestDoChan(t *testing.T) { 133 | g := NewShardedGroup() 134 | ch := g.DoChan("key", func() (interface{}, error) { 135 | return "bar", nil 136 | }) 137 | 138 | res := <-ch 139 | v := res.Val 140 | err := res.Err 141 | if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want { 142 | t.Errorf("Do = %v; want %v", got, want) 143 | } 144 | if err != nil { 145 | t.Errorf("Do error = %v", err) 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # shardedsingleflight 2 | [![License: MPL 2.0](https://img.shields.io/badge/License-MPL_2.0-brightgreen.svg)](https://opensource.org/licenses/MPL-2.0)[![Go Reference](https://pkg.go.dev/badge/github.com/tarndt/shardedsingleflight.svg)](https://pkg.go.dev/github.com/tarndt/shardedsingleflight) [![Go Report Card](https://goreportcard.com/badge/github.com/tarndt/shardedsingleflight)](https://goreportcard.com/report/github.com/tarndt/shardedsingleflight) 3 | 4 | A sharded wrapper for `golang.org/x/sync/singleflight` ([code](https://github.com/golang/sync/tree/master/singleflight), [docs](https://pkg.go.dev/golang.org/x/sync/singleflight)) for high contention/concurrency environments. 5 | 6 | ## What is singleflight? 7 | If you are not familiar, *singleflight* is a package created by [Brad Fitzpatrick](https://en.wikipedia.org/wiki/Brad_Fitzpatrick) that addresses the [thundering herd problem](https://en.wikipedia.org/wiki/Thundering_herd_problem) by assigning every operation a key and de-duplicating concurrently invoked operations based on that key. So for example, if you have a function that reads a file from disk and you wrap that function with singleflight, if the function is invoked twice, the second caller will get the same result returned to the first caller and the file will only be read once. 8 | 9 | ```go 10 | //Not thundering herd safe! 11 | func readFile(filepath string) (string, error) { 12 | data, err := os.ReadFile(filepath) 13 | return string(data), err 14 | } 15 | 16 | var g singeflight.Group //normally a field in a struct 17 | 18 | //Safe from the herd! 19 | func ReadFile(filepath string) (string, error) { 20 | result, err, _ := g.Do(filepath, func() (interface{}, error) { 21 | return readFile(filepath) 22 | }) 23 | return result.(string), err 24 | } 25 | ``` 26 | *See [singleflight.Do](https://pkg.go.dev/golang.org/x/sync/singleflight#Group.Do) and [singleflight.DoChan](https://pkg.go.dev/golang.org/x/sync/singleflight#Group.DoChan) for more details.* 27 | 28 | When duplicate operations are expensive and results can be shared, *singeflight*'s reduction in duplicate computation and/or I/O almost always warrants the overhead; I have found that on systems running very concurrent workloads that the [mutex](https://pkg.go.dev/sync#Mutex) contention [internal to singleflight](https://github.com/golang/sync/blob/master/singleflight/singleflight.go#L69) can quickly become significant. 29 | 30 | This package creates shards that each have their own [singleflight.Group](https://pkg.go.dev/golang.org/x/sync/singleflight#Group) and use a [hash function](https://pkg.go.dev/hash#Hash64) to distribute the keys over them. The result is a drastic reduction of [mutex](https://pkg.go.dev/sync#Mutex) contention as demonstrated by the package's benchmarks that compare it to a vanilla *singleflight* Group. How drastic a reduction depends on factors including how well distributed your hash function is, the number of shards provisioned, how many cores your system has, and the level of concurrency. 31 | 32 | ## So- The short version: Why does this exist? 33 | 1. [Brad Fitzpatrick](https://en.wikipedia.org/wiki/Brad_Fitzpatrick)'s *singleflight* library is amazingly useful! It is a robust and simple way to counter the [thundering herd problem](https://en.wikipedia.org/wiki/Thundering_herd_problem) in many cases. 34 | 2. A number of times in [my career](https://www.linkedin.com/in/tylorarndt/) I have have encountered problems using *singleflight* on machines with many cores/goroutines due to contention for the internal mutexes used by *singleflight* Groups. 35 | 3. I have written less robust versions of the sharded solution in this repo too many times and would like to spend my time on more interesting problems in the future. 36 | 4. If you face a similar challenge, I hope you can benefit from this solution as well! 37 | 38 | ### Show me the money! 39 | *shardedsingleflight* allows configuring both the shard count and shard mapping ([hash](https://pkg.go.dev/hash#Hash64)) algorithm to be overridden. Below is a comparison of parallel vanilla *singleflight* (`noshard-24`) vs. *shardedsingleflight* on a 24 logical-core machine using various hash algorithms and the default shard count heuristic (`nextPrime(logical-cores * 7)`). On this machine, *shardedsingleflight* using [FNV-64](https://pkg.go.dev/hash/fnv) is about **9x faster** than vanilla *singleflight*. As always test on your own hardware and using your own software to validate this is worth using over vanilla *singleflight*. Software engineering is always about tradeoffs, we are trading a little extra memory and hash computation for less [mutex](https://pkg.go.dev/sync#Mutex) contention internal to singleflight, but that only pays off with enough concurrency (and thus contention). 40 | ``` 41 | go test -test.bench=.* 42 | goos: linux 43 | goarch: amd64 44 | pkg: github.com/tarndt/shardedsingleflight 45 | cpu: AMD Ryzen 9 3900X 12-Core Processor 46 | BenchmarkDo/noshard-24 2070744 583.30 ns/op 94 B/op 0 allocs/op 47 | BenchmarkDo/shard-hash-fnv64-24 18465124 65.65 ns/op 119 B/op 2 allocs/op 48 | BenchmarkDo/shard-hash-fnv64a-24 16838563 69.95 ns/op 119 B/op 2 allocs/op 49 | BenchmarkDo/shard-hash-crc-iso-24 15008778 77.21 ns/op 127 B/op 2 allocs/op 50 | BenchmarkDo/shard-hash-crc-ecma-24 14828358 79.10 ns/op 127 B/op 2 allocs/op 51 | BenchmarkDo/shard-hash-maphash-24 9129664 133.1 ns/op 272 B/op 3 allocs/op 52 | PASS 53 | ok github.com/tarndt/shardedsingleflight 3.162s 54 | ``` 55 | *Note: As seen above the extra complexity involved in sharding comes with a memory allocation tradeoff, but still is favorable in terms of execution time in a high-contention environment nonetheless.* 56 | 57 | ### Contributing 58 | Issues, PRs and feedback are welcome! -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | --------------------------------------------------------------------------------