Application of residue at infinity I am trying to figure out how to find a contour to solve for this integral
$$
\int_{-\infty}^{\infty}\frac{2x}{x^2+x+1}dx = -\frac{2\pi}{\sqrt{3}}
$$
using the residue theorem and the residue at infinity. Without using the idea of residue at infinity, which is equal to $-\frac{1}{2\pi i} \int_{C} f(z)dz$, I will just choose the upper semi-circle. My question is how to choose a contour whose interior includes all the singularities of the function $\frac{2z}{z^2+z+1}$ ($-\frac{1}{2}\pm \frac{\sqrt{3}}{2}i$) such that it also covers the whole real axis as we take appropriate radii to $0$ or $\infty$, etc. I am thinking of a contour will be some sort of keyhole, but couldn't figure out a simpler contour. It seems like using residue at infinity makes the question 10 times more complicated than necessary.
 A: As noted in the comment by @Greg Martin, this integral must be interpreted as a Cauchy Principal value. https://en.wikipedia.org/wiki/Cauchy_principal_value
No need to include all the singularities. The curve $C$ consisting of the (counterclockwise) upper semicircle together with its diameter $[-R,R]$ works well. Let $\zeta_1, \zeta_2$ be the singularities of $f$. The residue of
$$f(z)= \frac{2z}{z^2+z+1}$$ at $\zeta_1=-\frac{1}{2}+ \frac{\sqrt{3}}{2}i \;$ equals $$ \frac{2\zeta_1}{\zeta_1-\zeta_2}=\frac{-1+\sqrt{3}i}{\sqrt{3}i} \,.$$ Multiplying by $2\pi i$ gives
$$ -\frac{2\pi}{\sqrt{3}}+2\pi i \,. \tag{1}$$
From this we must subtract the integral of $f$ along the counterclockwise upper semicircle, which is changed by $O(1/R)$ if we instead integrate $2/z$, because
$$|z|=R \;\Rightarrow \; \Bigl|\frac{2z}{z^2+z+1}-\frac{2}{z}\Bigr|=O(R^{-2}) \,.$$
The counterclockwise integral of $2/z$ on the upper semicircle equals
$$2\log(-R)-2\log(R)=2\log(-1)=2\log(e^{i\pi})=2\pi i \,, \tag{2}$$  where we used a branch of log that is well defined in the closed upper half plane.
Subtracting (2) from (1) and letting $R \to \infty$ concludes the proof.
A: The only way to obtain the result you mention is to take Cauchy's Principal Value (CPV), since as Martin commented that real improper integral is divergent, for example because on $\;[1,\infty)\;$ we have:
$$\frac{2x}{x^2+x+1}\ge\frac x{3x^2}=\frac1{3x}\,,\,\;\text{and}\;\;\int_1^\infty\frac{dx}{3x}\;\;\;\text{diverges}$$
for example, by the infinite series text with the harmonic series.
Take thus the simple, closed and rectifiable contour
$$\;\Gamma:=[-R,R]\cup\gamma:=\{z\in\Bbb C\;/\;|z|=R>1\,,\,\,\text{Im}\,z>0\}\;$$
so that the only (simple) pole of the function $\;f(z)+:=\frac{2z}{z^2+z+1}\;$ is $\;z_0:=-\frac12+\frac{\sqrt3}2i\;$, with residue
$$Res(f)_{z=z_0}=\lim_{z\to z_0}\left((z-z_0)f(z)\right)=\lim_{z\to z_0}\frac{2z}{z+\frac12+\frac{\sqrt3}2i}=\frac{-1+\sqrt3i}{\sqrt3\,i}=1+\frac i{\sqrt3}$$
so that
$$2\pi i\left(1+\frac i{\sqrt3}\right)=\oint_\Gamma f(z) dz = \int_{-R}^Rf(x) dx+\int_\gamma f(z) dz$$
And now let $\;R\to\infty\;$ and compare real and imaginary parts and etc.
A: Nice explanations were posted - on how to evaluate the integral, closing the contour in the upper half-plane (and using, in fact, half of the residue at the infinity). In my opinion, this is the most adequate way to approach the integral by means of complex integration. However, if we want use a longer road and to use all residues evaluation along this road, we can switch to the keyhole contour. This way will not require the evaluation of the residue at infinity, though (speaking conceptually, the sum of all residues of a meromorphic function is zero).
$$I=\int_{-\infty}^\infty\frac{2x}{x^2+x+1}dx=\int_0^\infty\frac{2x}{x^2+x+1}dx-\int_0^\infty\frac{2x}{x^2-x+1}dx$$
$$=-\int_0^\infty\frac{4x^2}{(x^2+1)^2-x^2}dx$$
Making the substitution $t=x^2$
$$I=-2\int_0^\infty\frac{\sqrt t}{t^2+t+1}dt$$
The roots of $z^2+z+1$ are $z_{1,2}=e^{\frac{5\pi i}{6}},e^{\frac{7\pi i}{6}}$ (we have chosen all angles $\in[0;2\pi]$).
Making the cut $[0;\infty)$ along the positive part of the axis $X$ and adding a big circle of the radius $R\to\infty$ (counter-clockwise, from the upper bunk of the cut to the lower bank), a small circle (radius $r\to0$, clockwise) around $z=0$, together with the upper and lower banks of the cut, we get a keyhole contour.
Integration along this contour gives:
$$\oint\frac{2\sqrt z}{(z-e^{\frac{5\pi i}{6}})(z-e^{\frac{7\pi i}{6}})}dz=I+I_R-Ie^{\pi i}+I_r=2\pi i\underset{z=z_{1,2}}{\operatorname{Res}}\frac{2\sqrt z}{(z-e^{\frac{5\pi i}{6}})(z-e^{\frac{7\pi i}{6}})}$$
Given that $I_R, I_r\to0$,  $\,\sqrt{e^{\frac{5\pi i}{6}}}=\sqrt{e^{\pi i-\frac{\pi  i}{6}}}=ie^{-\frac{\pi  i}{12}}$ and $\sqrt{e^{\frac{7\pi i}{6}}}=ie^{\frac{\pi  i}{12}}$
$$I=2\pi i\underset{z=z_{1,2}}{\operatorname{Res}}\frac{\sqrt z}{(z-e^{\frac{5\pi i}{6}})(z-e^{\frac{7\pi i}{6}})}=2\pi i\bigg(\frac{ie^{-\frac{\pi  i}{12}}}{e^{\frac{5\pi i}{6}}-e^{\frac{7\pi i}{6}}}+\frac{ie^{\frac{\pi  i}{12}}}{e^{\frac{7\pi i}{6}}-e^{\frac{5\pi i}{6}}}\bigg)$$
$$=-2\pi\frac{\sin\frac{\pi}{12}}{\sin\frac{\pi}{6}}=-\frac{\pi}{\cos\frac{\pi}{12}}$$
Using $\cos\alpha=\sqrt{\frac{1+\cos2\alpha}{2}}$
$$I=-\,\frac{\pi}{\sqrt\frac{1+\cos\frac{\pi}{6}}{2}}=-\frac{2\pi}{\sqrt3}$$
