Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer (GitLab) <jacob@gitlab.com>2018-04-06 11:04:25 +0300
committerZeger-Jan van de Weg <zegerjan@gitlab.com>2018-04-06 11:04:25 +0300
commit4dadabad50b7821ee625166b8824b69b25a3221f (patch)
tree537cd5cae60ee948a73178f47cc2ff39dd1e092e
parent5afd6a821d5530c4fcd244fec0c8b534f2387756 (diff)
Don't use sync/atomic
-rw-r--r--STYLE.md13
-rw-r--r--internal/middleware/limithandler/testhelper_test.go16
2 files changed, 23 insertions, 6 deletions
diff --git a/STYLE.md b/STYLE.md
index 3a6ae2bad..d72775073 100644
--- a/STYLE.md
+++ b/STYLE.md
@@ -107,4 +107,15 @@ instead of:
func (server) GetBlob(_ *pb.GetBlobRequest, _ pb.BlobService_GetBlobServer) error {
return helper.Unimplemented
}
-``` \ No newline at end of file
+```
+
+## Concurrency
+
+The [documentation of "sync/atomic"](https://golang.org/pkg/sync/atomic/) says:
+
+> These functions require great care to be used correctly. Except for
+special, low-level applications, synchronization is better done with
+channels or the facilities of the sync package.
+
+Gitaly is not a low-level application so we should avoid "sync/atomic".
+We use channels and [package "sync"](https://golang.org/pkg/sync/).
diff --git a/internal/middleware/limithandler/testhelper_test.go b/internal/middleware/limithandler/testhelper_test.go
index 40eff4f01..e197b18f4 100644
--- a/internal/middleware/limithandler/testhelper_test.go
+++ b/internal/middleware/limithandler/testhelper_test.go
@@ -1,23 +1,29 @@
package limithandler_test
import (
- "sync/atomic"
+ "sync"
pb "gitlab.com/gitlab-org/gitaly/internal/middleware/limithandler/testpb"
"golang.org/x/net/context"
)
type server struct {
- requestCount uint64
- blockCh chan (struct{})
+ requestCount int
+ sync.Mutex
+ blockCh chan (struct{})
}
func (s *server) registerRequest() {
- atomic.AddUint64(&s.requestCount, 1)
+ s.Lock()
+ s.requestCount++
+ s.Unlock()
}
func (s *server) getRequestCount() int {
- return int(atomic.LoadUint64(&s.requestCount))
+ s.Lock()
+ count := s.requestCount
+ s.Unlock()
+ return count
}
func (s *server) Unary(ctx context.Context, in *pb.UnaryRequest) (*pb.UnaryResponse, error) {