Re: [go-nuts] Type inference based on constraints

2022-02-18 Thread Jeff Peck


On 2/16/2022 2:43 PM, Ian Lance Taylor wrote:

On Tue, Feb 15, 2022 at 2:49 PM Blackgreen  wrote:

I came across this stack overflow question:
https://stackoverflow.com/questions/71131665/generics-pass-map-with-derived-types

The OP attempted to pass different maps into this code:

func equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool { if len(m1) != 
len(m2) { return false } for k, v1 := range m1 { if v2, ok := m2[k]; !ok || v1 
!= v2 { return false } } return true }

It is clear to me why this doesn't work. However, I was studying possible fixes 
for this function. Here's a 
playground:https://gotipplay.golang.org/p/NPpau1G0Hqp

My first attempt was based on differentiating the K and V type params and came 
up with this:

func equal[M1 ~map[K1]V1, M2 ~map[K2]V2, K1, K2 ~uint32, V1, V2 ~string](m1 M1, 
m2 M2) bool {
 if len(m1) != len(m2) {
 return false
 }
 for k, v1 := range m1 {
 if v2, ok := m2[K2(k)]; !ok || V2(v1) != v2 {
 return false
 }
 }
 return true
}

I thought this should work, because now it's possible to infer K1 and K2 from M1 and M2 
respectively, and the approximate constraints allow conversion in the function body. But 
it still doesn't compile with error "K2 does not match uint32".

Instead, this works, even though K1 and K2 are defined the same way:

func equalFixed[K1, K2 ~uint32, V1, V2 ~string](m1 map[K1]V1, m2 map[K2]V2) 
bool {
}

By reading the Go 1.18 language specs, my intuition is that the first attempt fails due 
to the so-called "adjusted core type" of the constraint ~uint32, though I'm not 
100% sure of what's going on.
Can you folks confirm my intuition and/or provide some pointers?


This looks like a bug in type inference to me.  I may be missing
something but I don't see how it could be producing that error.  Would
you mind opening a bug report athttps://go.dev/issue?  Thanks.  Note
that this may not be fixed in the upcoming 1.18 release, as we are
trying to be careful about type inference.

Ian



I changed the block below to use alias declarations instead and it 
worked OK for me:



type (
    someNumericID uint32
    someStringID   string
)

to this:


type (
    someNumericID = uint32
    someStringID  = string
)

https://gotipplay.golang.org/p/diU2c31TC1b

The Go Programming Language Specification - Go 1.18 Draft - The Go 
Programming Language (golang.org) 






--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/fb67989c-15e9-edf8-43cb-c76a97b5a572%40gmail.com.


Re: [go-nuts] Why does Go ignore HTTP_PROXY and HTTPS_PROXY if the proxy address is localhost

2021-04-14 Thread Jeff Peck
Just a complete shot in the dark. Have you verified that the environment 
variables you set are indeed getting picked up inside of your 
application? Try checking the output of os.GetEnv("HTTP_PROXY"), 
os.GetEnv("HTTPS_PROXY"), and os.GetEnv("NO_PROXY"). Make sure that if 
NO_PROXY is set that it isn't overriding your local proxy.



You could also try setting the lowercase versions of these env vars:

https://github.com/golang/net/blob/master/http/httpproxy/proxy.go

func FromEnvironment() *Config {
    return &Config{
        HTTPProxy:  getEnvAny("HTTP_PROXY", "http_proxy"),
        HTTPSProxy: getEnvAny("HTTPS_PROXY", "https_proxy"),
        NoProxy:    getEnvAny("NO_PROXY", "no_proxy"),
        CGI:    os.Getenv("REQUEST_METHOD") != "",
    }
}


On 4/13/21 2:07 PM, Orson Cart wrote:

Please accept my ay apologies. I'd misunderstood the documentation.

I was looking for a reason why my go application's requests aren't 
being sent to the proxy that I've specified using HTTP_PROXY and 
HTTPS_PROXY. When I saw the comment in the documentation I thought I'd 
found an explanation even if I didn't understand the reason for it 
being that way.


So now I'm left with my original problem: I have HTTP_PROXY set to 
http://localhost: and HTTPS_PROXY set to https://localhost:. I 
also have a proxy (Fiddler) running on localhost: but requests 
from my application appear to go direct, bypassing the proxy.


I'm perplexed :(

On Tuesday, 13 April 2021 at 19:17:10 UTC+1 wagner riffel wrote:

On Tue Apr 13, 2021 at 2:14 PM -03, Orson Cart wrote:
> Can anyone explain the reasoning behind this? It rather
interferes with
> debugging traffic using a local proxy like Fiddler.
>

My guess here it's for preventing the remote from tricking the
proxy to
make request to internal services that couldn't be reached otherwise.

> I've seen suggestions to define an alternative hostname in
/etc/hosts
> but as far as I can tell this doesn't work either.
>

The code you linked doesn't seen to try that hard to block loopback
requests, I do think that an /etc/hosts entry to 127.0.0.1 should
bypass the proxy, if not I don't see any other way around other than
implementing the RoundTripper yourself, which shouldn't be hard for
such simple use.

--wagner

--
You received this message because you are subscribed to the Google 
Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to golang-nuts+unsubscr...@googlegroups.com 
.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ac6c1bae-2153-41b3-9037-110fc0eb12cfn%40googlegroups.com 
.


--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c889bc72-0a1d-72a1-83dc-ecafe5ef8458%40gmail.com.


Re: [go-nuts] Why does my code keep giving me "socket: too many open files"

2021-04-09 Thread Jeff Peck


On 4/9/21 11:57 AM, Tenjin wrote:
Basically I am making a script to query an api for some json data, if 
I run it synchronously it works fine and works as intended but it 
needs to do this give or take 15thousand times so I thought I would 
use concurrency to get the job done. It works fine for about 4 
thousand queries then it stops working and gives me this error 
"socket: too many open files" when I did research on the issue I 
implemented everything they did I am consuming the response body and 
then I am closing it once I am finished as well as letting my wait 
group know I am finished to exit the go routine.


*Right now I am making a new goroutine for each request and then 
closing it*. I am also using WSL2 to run the code as I came across a 
stack overflow post that said I need to up my ulimit on the machine 
which I cannot do and ideally do not want others to have to do.


This is most likely your problem. I can't be sure, but it sounds like 
you are possibly launching all of these goroutines at once on startup. 
If so, then they are likely all trying to open a network connection at 
the same time. Instead, I would suggest to push your requests into a 
channel and have a well defined goroutine that iterates over that 
channel to service the requests. Then you can experiment with launching 
N instances of your goroutine when your application starts up.  This 
should give you better control over the number of outstanding sockets, 
and you can play with N to get good performance.


I have been stuck on this for a few days now and no one is helping me 
thus far, fingers crossed this post works out well and I can get it 
resolved.

--
You received this message because you are subscribed to the Google 
Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to golang-nuts+unsubscr...@googlegroups.com 
.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/09e4caf4-d79c-450e-a544-2d9242167f87n%40googlegroups.com 
.


--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/185b0d53-22f9-6c6b-7cbc-739c79d1798b%40gmail.com.