Browse Source

Don't iterate when context is done

Volodymyr Tkach 2 years ago
parent
commit
68c8eeb554
1 changed files with 7 additions and 2 deletions
  1. 7 2
      gosql/common/dbmethods.go

+ 7 - 2
gosql/common/dbmethods.go

@@ -52,8 +52,13 @@ func (d *DBMethods) Each(ctx context.Context, query string, callback func(ctx co
 	}
 	}
 	defer rows.Close()
 	defer rows.Close()
 	for rows.Next() {
 	for rows.Next() {
-		if err := callback(ctx, rows); err != nil {
-			return err
+		select {
+		case <-ctx.Done():
+			return ctx.Err()
+		default:
+			if err := callback(ctx, rows); err != nil {
+				return err
+			}
 		}
 		}
 	}
 	}
 	if err := rows.Err(); err != nil {
 	if err := rows.Err(); err != nil {