lock_racer.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/coreos/etcd/clientv3/concurrency"
  19. )
  20. func runRacer(getClient getClientFunc, round int) {
  21. rcs := make([]roundClient, 15)
  22. ctx := context.Background()
  23. cnt := 0
  24. for i := range rcs {
  25. rcs[i].c = getClient()
  26. var (
  27. s *concurrency.Session
  28. err error
  29. )
  30. for {
  31. s, err = concurrency.NewSession(rcs[i].c)
  32. if err == nil {
  33. break
  34. }
  35. }
  36. m := concurrency.NewMutex(s, "racers")
  37. rcs[i].acquire = func() error { return m.Lock(ctx) }
  38. rcs[i].validate = func() error {
  39. if cnt++; cnt != 1 {
  40. return fmt.Errorf("bad lock; count: %d", cnt)
  41. }
  42. return nil
  43. }
  44. rcs[i].release = func() error {
  45. if err := m.Unlock(ctx); err != nil {
  46. return err
  47. }
  48. cnt = 0
  49. return nil
  50. }
  51. }
  52. doRounds(rcs, round)
  53. }