1

The code that triggers the HashMapSet is as follows:


    ctx = context.WithValue(ctx, "isCli", true)
    numberOfCreatedGroups := 0
    for _, ip := range ips {
        time.Sleep(time.Millisecond * time.Duration(delay))
        if err := ih.dc.add(ctx, ip, uint32(0)); err != nil {
            log.WithFields(log.Fields{
                GroupIp: ip,
            }).Info(err)
            continue
        } else {
            numberOfCreatedGroups += 1
        }
    }

The add function also calls the function below:

_, err := db.Repo.HashMapSet(ctx, key, groupIp, id)

where HashMapSet function calls the actual HSet function which can be seen as below.

func (r *repository) HashMapSet(ctx context.Context, key string, values ...interface{}) (int64, error) {
    intCmd := r.Client.HSet(ctx, key, values...)
    log.WithFields(log.Fields{"key": key, "values": values, "result": intCmd}).Debug("repo-hashmapset")
    return intCmd.Result()
}

Below is what I see as an output:

DEBU[2023-03-13T22:44:35.191449159+03:00] repo-hashmapset                               key=ig_groupids/55 result="hset ig_groupids/55 239.254.1.175 432: context deadline exceeded" values="[239.254.1.175 432]"
INFO[2023-03-13T22:44:35.191572577+03:00] context deadline exceeded                     group-ip=239.254.1.175
DEBU[2023-03-13T22:44:35.211896763+03:00] repo-hashmapset                               key=ig_groupids/55 result="hset ig_groupids/55 239.254.1.176 432: context deadline exceeded" values="[239.254.1.176 432]"
INFO[2023-03-13T22:44:35.212052712+03:00] context deadline exceeded                     group-ip=239.254.1.176
DEBU[2023-03-13T22:44:35.232327292+03:00] repo-hashmapset                               key=ig_groupids/55 result="hset ig_groupids/55 239.254.1.177 432: context deadline exceeded" values="[239.254.1.177 432]"
INFO[2023-03-13T22:44:35.232373678+03:00] context deadline exceeded                     group-ip=239.254.1.177
DEBU[2023-03-13T22:44:35.252736679+03:00] repo-hashmapset                               key=ig_groupids/55 result="hset ig_groupids/55 239.254.1.178 432: context deadline exceeded" values="[239.254.1.178 432]"
INFO[2023-03-13T22:44:35.252776536+03:00] context deadline exceeded                     group-ip=239.254.1.178
DEBU[2023-03-13T22:44:35.273120216+03:00] repo-hashmapset            

Is it related to context.WitValue? Because It seems to me that same context is used for all invocations here. After some times like 9 to 10 seconds, the application starts producing these context deadline exceeded outputs. I am using redis not redis-sentinel. Also can it be related to way of creating or initializng Redis connection? Which I didn't pass any timeout or something like that while initilization phase.

baris
  • 157
  • 1
  • 9
  • 4
    Did you set a deadline for the context passed into `context.WithValue`? There are no deadlines in the code you show. – Burak Serdar Mar 13 '23 at 20:12
  • 1
    You should at least check in your loop whether the context is canceled or has an exceeded deadline, as there is no point continuing after that (any IO or other operations that use the context will fail immediately): `if ctx.Err() != nil { break }` – Erwin Bolwidt Mar 14 '23 at 05:17
  • @BurakSerdar No, I didn't set any deadline for the context. I just noticed that the first function which is not listed here has timeout passed into `context.WithTimeout`. I guess that's the problem that I should focus on. – baris Mar 14 '23 at 11:10
  • @ErwinBolwidt Ok. Does this happen because it takes too much time to complete. How can I make sure that it does not fire a `context deadline exceeded` error and all operations are completed. – baris Mar 14 '23 at 11:12
  • 1
    @baris by not setting a deadline or a timeout on the context that you use. you could just use a new `context.Background()` – Erwin Bolwidt Mar 15 '23 at 00:12

0 Answers0