github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/client/redis/dlock/example_test.go (about)

     1  package dlock
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/weedge/lib/strings"
     7  	"math/rand"
     8  	"runtime/debug"
     9  	"sync"
    10  	"time"
    11  
    12  	"github.com/go-redis/redis/v8"
    13  )
    14  
    15  var rdb = redis.NewClient(&redis.Options{
    16  	Addr:         "localhost:6379",
    17  	Password:     "", // no password set
    18  	DB:           0,  // use default DB
    19  	DialTimeout:  10 * time.Second,
    20  	ReadTimeout:  30 * time.Second,
    21  	WriteTimeout: 30 * time.Second,
    22  	PoolSize:     10,
    23  	PoolTimeout:  30 * time.Second,
    24  })
    25  
    26  func MockTestTryLock(tag string) {
    27  	var retryTimes = 5
    28  	var retryInterval = time.Millisecond * 50
    29  	lockK := "EXAMPLE_TRY_LOCK"
    30  	expiration := time.Millisecond * 200
    31  
    32  	locker := New(rdb, lockK, tag, &UUIDVal{}, retryTimes, retryInterval, expiration, true)
    33  	err, isGainLock := locker.TryLock(context.Background())
    34  	if err != nil || isGainLock == false {
    35  		return
    36  	}
    37  
    38  	defer locker.UnLock(context.Background())
    39  
    40  	println(tag + " run...")
    41  	time.Sleep(getRandDuration())
    42  	println(tag + " over")
    43  }
    44  
    45  func MockTestLock(tag string) {
    46  	var retryTimes = -1
    47  	var retryInterval = time.Millisecond * 50
    48  	lockK := "EXAMPLE_LOCK"
    49  	expiration := time.Millisecond * 200
    50  
    51  	locker := New(rdb, lockK, tag, &UUIDVal{}, retryTimes, retryInterval, expiration, true)
    52  	err, isGainLock := locker.Lock(context.Background())
    53  	if err != nil || isGainLock == false {
    54  		return
    55  	}
    56  
    57  	defer locker.UnLock(context.Background())
    58  
    59  	println(tag + " run...")
    60  	mTime := getRandDuration()
    61  	time.Sleep(mTime)
    62  	println(tag + " run time " + fmt.Sprintf("%d", mTime) + " ns")
    63  	println(tag + " over")
    64  }
    65  
    66  func getRandDuration() time.Duration {
    67  	rand.Seed(time.Now().UnixNano())
    68  	min := 50
    69  	max := 100
    70  	return time.Duration(rand.Intn(max-min)+min) * time.Millisecond
    71  }
    72  
    73  func ExampleRedisLocker_TryLock() {
    74  	wg := &sync.WaitGroup{}
    75  	tags := []string{"A", "B", "C", "D", "E"}
    76  	wg.Add(len(tags))
    77  	for _, tag := range tags {
    78  		go func(tag string) {
    79  			MockTestTryLock(tag)
    80  			wg.Done()
    81  		}(tag)
    82  	}
    83  	wg.Wait()
    84  
    85  	//output:
    86  	//
    87  }
    88  
    89  func ExampleRedisLocker_Lock() {
    90  	wg := &sync.WaitGroup{}
    91  	tags := []string{"A", "B", "C", "D", "E"}
    92  	wg.Add(len(tags))
    93  	for _, tag := range tags {
    94  		go func(tag string) {
    95  			MockTestLock(tag)
    96  			wg.Done()
    97  		}(tag)
    98  	}
    99  	wg.Wait()
   100  
   101  	//output:
   102  	//
   103  }
   104  
   105  func Recover() {
   106  	if e := recover(); e != nil {
   107  		fmt.Printf("panic: %v, stack: %v\n", e, strings.BytesToString(debug.Stack()))
   108  	}
   109  }
   110  
   111  func ExampleRedisLocker_LockLongPanic() {
   112  	wg := &sync.WaitGroup{}
   113  	wg.Add(1)
   114  	go func() {
   115  		defer wg.Done()
   116  		defer Recover()
   117  		var retryTimes = 5
   118  		var retryInterval = time.Millisecond * 50
   119  		lockK := "EXAMPLE_LOCK_WATCH"
   120  		expiration := time.Millisecond * 200
   121  		tag := "panic"
   122  
   123  		locker := New(rdb, lockK, tag, &UUIDVal{}, retryTimes, retryInterval, expiration, true)
   124  		defer locker.UnLock(context.Background())
   125  		err, isGainLock := locker.TryLock(context.Background())
   126  		if err != nil || isGainLock == false {
   127  			return
   128  		}
   129  
   130  		time.Sleep(20 * time.Second)
   131  		panic("test panic for lock watch")
   132  	}()
   133  	wg.Wait()
   134  	println("over")
   135  
   136  	//output:
   137  	//
   138  }