|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | +) |
| 12 | + |
| 13 | +var ctx = context.Background() |
| 14 | +var consStopped = false |
| 15 | + |
| 16 | +func main() { |
| 17 | + wg := &sync.WaitGroup{} |
| 18 | + rdb := redis.NewClient(&redis.Options{ |
| 19 | + Addr: ":6379", |
| 20 | + }) |
| 21 | + _ = rdb.FlushDB(ctx).Err() |
| 22 | + |
| 23 | + go func() { |
| 24 | + for { |
| 25 | + time.Sleep(2 * time.Second) |
| 26 | + fmt.Printf("pool stats: %+v\n", rdb.PoolStats()) |
| 27 | + } |
| 28 | + }() |
| 29 | + err := rdb.Ping(ctx).Err() |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + if err := rdb.Set(ctx, "prods", "0", 0).Err(); err != nil { |
| 34 | + panic(err) |
| 35 | + } |
| 36 | + if err := rdb.Set(ctx, "cons", "0", 0).Err(); err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + if err := rdb.Set(ctx, "cntr", "0", 0).Err(); err != nil { |
| 40 | + panic(err) |
| 41 | + } |
| 42 | + if err := rdb.Set(ctx, "recs", "0", 0).Err(); err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + fmt.Println("cntr", rdb.Get(ctx, "cntr").Val()) |
| 46 | + fmt.Println("recs", rdb.Get(ctx, "recs").Val()) |
| 47 | + subCtx, cancelSubCtx := context.WithCancel(ctx) |
| 48 | + for i := 0; i < 10; i++ { |
| 49 | + wg.Add(1) |
| 50 | + go subscribe(subCtx, rdb, "test", i, wg) |
| 51 | + } |
| 52 | + time.Sleep(time.Second) |
| 53 | + cancelSubCtx() |
| 54 | + time.Sleep(time.Second) |
| 55 | + subCtx, cancelSubCtx = context.WithCancel(ctx) |
| 56 | + for i := 0; i < 10; i++ { |
| 57 | + if err := rdb.Incr(ctx, "prods").Err(); err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + wg.Add(1) |
| 61 | + go floodThePool(subCtx, rdb, wg) |
| 62 | + } |
| 63 | + |
| 64 | + for i := 0; i < 100; i++ { |
| 65 | + if err := rdb.Incr(ctx, "cons").Err(); err != nil { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | + wg.Add(1) |
| 69 | + go subscribe(subCtx, rdb, "test2", i, wg) |
| 70 | + } |
| 71 | + time.Sleep(10 * time.Second) |
| 72 | + fmt.Println("canceling") |
| 73 | + cancelSubCtx() |
| 74 | + wg.Wait() |
| 75 | + cntr, err := rdb.Get(ctx, "cntr").Result() |
| 76 | + recs, err := rdb.Get(ctx, "recs").Result() |
| 77 | + prods, err := rdb.Get(ctx, "prods").Result() |
| 78 | + cons, err := rdb.Get(ctx, "cons").Result() |
| 79 | + fmt.Printf("cntr: %s\n", cntr) |
| 80 | + fmt.Printf("recs: %s\n", recs) |
| 81 | + fmt.Printf("prods: %s\n", prods) |
| 82 | + fmt.Printf("cons: %s\n", cons) |
| 83 | + time.Sleep(2 * time.Second) |
| 84 | +} |
| 85 | + |
| 86 | +func floodThePool(ctx context.Context, rdb *redis.Client, wg *sync.WaitGroup) { |
| 87 | + defer wg.Done() |
| 88 | + for { |
| 89 | + select { |
| 90 | + case <-ctx.Done(): |
| 91 | + fmt.Println("floodThePool stopping") |
| 92 | + consStopped = true |
| 93 | + return |
| 94 | + default: |
| 95 | + } |
| 96 | + err := rdb.Publish(ctx, "test2", "hello").Err() |
| 97 | + if err != nil { |
| 98 | + log.Println("publish error:", err) |
| 99 | + } |
| 100 | + |
| 101 | + err = rdb.Incr(ctx, "cntr").Err() |
| 102 | + if err != nil { |
| 103 | + log.Println("incr error:", err) |
| 104 | + } |
| 105 | + time.Sleep(10 * time.Nanosecond) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func subscribe(ctx context.Context, rdb *redis.Client, topic string, subscriberId int, wg *sync.WaitGroup) { |
| 110 | + defer wg.Done() |
| 111 | + defer fmt.Printf("subscriber %d stopping\n", subscriberId) |
| 112 | + rec := rdb.Subscribe(ctx, topic) |
| 113 | + recChan := rec.Channel() |
| 114 | + for { |
| 115 | + select { |
| 116 | + case <-ctx.Done(): |
| 117 | + rec.Close() |
| 118 | + if subscriberId == 199 { |
| 119 | + fmt.Printf("subscriber %d done\n", subscriberId) |
| 120 | + } |
| 121 | + return |
| 122 | + default: |
| 123 | + select { |
| 124 | + case <-ctx.Done(): |
| 125 | + rec.Close() |
| 126 | + if subscriberId == 199 { |
| 127 | + fmt.Printf("subscriber %d done\n", subscriberId) |
| 128 | + } |
| 129 | + return |
| 130 | + case msg := <-recChan: |
| 131 | + err := rdb.Incr(ctx, "recs").Err() |
| 132 | + if err != nil { |
| 133 | + log.Println("incr error:", err) |
| 134 | + } |
| 135 | + if consStopped { |
| 136 | + fmt.Printf("subscriber %d received %s\n", subscriberId, msg.Payload) |
| 137 | + } |
| 138 | + if subscriberId == 199 { |
| 139 | + fmt.Printf("subscriber %d received %s\n", subscriberId, msg.Payload) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments