Received: from mail.netlandish.com (mail.netlandish.com [174.136.98.166]) by code.netlandish.com (Postfix) with ESMTP id 4018D81242 for <~petersanchez/public-inbox@lists.code.netlandish.com>; Tue, 22 Dec 2020 01:16:24 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.210.173; helo=mail-pf1-f173.google.com; envelope-from=peter@netlandish.com; receiver= Authentication-Results: mail.netlandish.com; dkim=pass (2048-bit key; unprotected) header.d=netlandish-com.20150623.gappssmtp.com header.i=@netlandish-com.20150623.gappssmtp.com header.b=iELJ12jq Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by mail.netlandish.com (Postfix) with ESMTP id EF7BB152FB1 for <~petersanchez/public-inbox@lists.code.netlandish.com>; Tue, 22 Dec 2020 01:16:21 +0000 (UTC) Received: by mail-pf1-f173.google.com with SMTP id s21so7471098pfu.13 for <~petersanchez/public-inbox@lists.code.netlandish.com>; Mon, 21 Dec 2020 17:16:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netlandish-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=NT7vIB9FKj6hlVQEdQL/srfjq96m73Wd7SZ9pBMOe3g=; b=iELJ12jq64+mujTNcuZjPCoLKfof2S350MLDLS8xmddvIVKaO3H/lBVToL+IOLj5qg SOU+O9gB5zk3tTQOFEyW4Yl0xwt1llC7BudBtIP3pRodR+wHb0RTfKlAEE02JcgFRQ/L KRInENU8ULQlcBz3OJEpvxs2H7w1rZMJUah8oYLJyvEU6IdmhEOcNDKaVcP/uadqjMzO /ycnczKa1msVf87K8jFduPecLhBC4Cb77P8tfpRmzJG5OIqMe1lfYAuEBNz2nR+A697e JFOKxHOnG40aEzoJJdu3seR3JnuKlESNX0ZkVc0Y07yv+AlQdk4ZnWctuJMy+t8Cwxk3 e/cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=NT7vIB9FKj6hlVQEdQL/srfjq96m73Wd7SZ9pBMOe3g=; b=eMvSzyVMebWSwRD6N18dBIfT7zvVYKprOzwjjIHOFl7nn8uNvpDXGtySyBBblTW6qe eMnn+nomOn8k9MszWvJFwnp9yjBH1YSiZdj1X5MN8437AnkCDV8VVROdPn2BOqm+qLm5 DGlHeWZiQEnHqnVl4hGZv2gk8k+PBF2QQGEH1KVtlJ3cAxmUjyXVfNDqEIvg8xS+voxs 9oQvbCUhYtTVC6Y27/PHxcbDxgiAuyoCTI7E2DcWSATvHQAq6x733uUKtpQPrdt5aGNz xzmoVa5YHYhphdvTo+C05ZyvaoFawCw3NbWhEik2QoiB+IAFaAOCT7EdOllc9iFdGF8N KblQ== X-Gm-Message-State: AOAM530My7jhStMMzJIZ1Nz+vbmND4hh1rE0RDbzAqYN7mX19k2eYlvh cxlEG+2G7zmWLATd3BVbnwBzL+gO X-Google-Smtp-Source: ABdhPJx3xgEIvlt8EaggHNfP9isbaduwHwnBUcnEdkNMsTo6MDk4H9WrNQsoClyrKeDYQuoJIBOzyw== X-Received: by 2002:a62:84ca:0:b029:19e:6f95:11b1 with SMTP id k193-20020a6284ca0000b029019e6f9511b1mr17438387pfd.68.1608599780687; Mon, 21 Dec 2020 17:16:20 -0800 (PST) Received: from localhost ([47.145.116.220]) by smtp.gmail.com with ESMTPSA id a22sm18128234pfg.49.2020.12.21.17.16.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Dec 2020 17:16:19 -0800 (PST) Date: Mon, 21 Dec 2020 17:16:18 -0800 From: Peter Sanchez To: =?utf-8?B?0JjQu9GM0Y8=?= Cc: ~petersanchez/public-inbox@lists.code.netlandish.com Subject: Re: [PATCH django-impersonate] optimization | preserve request.user laziness Message-ID: <20201222011618.r4g5lhd7y5rhffsp@thinkpad> X-PGP-Key: https://petersanchez.com/publickey.txt References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ax6bvpqfd5xurhhl" Content-Disposition: inline In-Reply-To: --ax6bvpqfd5xurhhl Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Interesting catch. I have some thoughts below. On 12/21, =D0=98=D0=BB=D1=8C=D1=8F wrote: >class ImpersonateMiddleware(MiddlewareMixin): > def process_request(self, request): > request.user.is_impersonate =3D False # <----- this one > request.impersonator =3D None > > if is_authenticated(request.user) and \ # <----- and this one > '_impersonate' in request.session: > new_user_id =3D request.session['_impersonate'] > ..... > >As a quick-and-dirty fix we can just remove the first line - but in >client code we would access this param as `getattr(request.user, >'is_impersonate', False)`. The issue with changing this is it will break the "API" (if we want to call it that) of the app and no doubt break deployments for end users who are expecting this value. >The second one is solved by placing `'_impersonate' in >request.session` as a first condition. This makes sense. I've pushed this change. >I think for many people impersonated requests are a very little amount >of the whole load so it could help to improve performance. Agreed that every query counts. Though the single query probably isn't the end of the world for 99.9% of cases. --ax6bvpqfd5xurhhl Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEkJViJRZ6KPWgtGGig4/80jEiNdEFAl/hSN8ACgkQg4/80jEi NdFIRA//ecduIoUlSVfiqTHRxymXYFmaqlCvDnaDDh68iPizuQjqLXNykzdefqLK UgHKKxSl6G9Qr5u8a/j25cLLkSnEqDbcQSfXr/xu1uiJwM5T1jKztxQoIPWsFKW+ q+T9pin474yQ6xye8bexjbRqtsTuFQ32+Yd4obxVBGOsVDwBz0HrAZ26mwbAoDSJ poK7Et1MhYp4ZjAOBCFRMP2ioUQjAvR11qd2Qk8CATONj4XVBDTSCdNjKwHE0PLT hgdqlUxYlC7EXAsEC3tGAcXPJZa2QbUoTFXfWf5td3qyFtju3UIr7Jm8TLxne2zK CDNXQ8HYdh2nSLeud5ZRsTAvsWjut5V66p3/Y5K+rnDXhY+E6PYEoOGkUe1m3FKt yFgwCUcFCe7K6F7hJTpL6wdhwbSi72W5UcyEOK7AnKupQjfo6JGmalML8HdnB95R 1NuudsO4dtc9pJGdUngxlgBBvN9EcLrDfMBmNjQhcUYPVM+V1PcMf7nva+Z1RqfK BSK7OJPVqhv+y2pY/xjUyPWN6VODxceYRthmWGEKo8eJYLg1EFfJ9ejnq5wiIF0r XAmZkVLkwQz26boBS4dlAjgeTc+EhaIWyGfQsWsKFw7thxYJAxtXvCXnUo/kcPLi xbGe2DcwevD1T7u6guA5JJ07uS+lgoxxkMNZP7KGPCkxbtkrsng= =cyXu -----END PGP SIGNATURE----- --ax6bvpqfd5xurhhl--