Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
spiritual bypass
Feb 19, 2008

Grimey Drawer
In Java-like languages, I'm accustomed to catching exceptions, then throwing a new exception in a "chain" so that I get information about every level of the stack trace. Does Go have this capability? I haven't found one, so I've created this pattern in my modules instead:

code:
if err != nil {
		return errors.New("mail API request failed: " + err.Error())
	}
This provides an error specific to this particular function while preserving the error from the function it called. Is there a better way to do it?

Adbot
ADBOT LOVES YOU

spiritual bypass
Feb 19, 2008

Grimey Drawer
Those are helpful, thanks. I knew I couldn't be the only person who wants to do this.

spiritual bypass
Feb 19, 2008

Grimey Drawer
I don't understand why I'm getting a segfault while trying to query my database. Here's a minimal example that segfaults at conn.Exec:

code:
func Add (email string, hash string) error {
	conn, err := sql.Open("mysql", "user:pass@localhost/dbname")

	if err != nil {
		errors.Wrap(err, "could not connect to database")
	}

	_, err = conn.Exec(
		"INSERT INTO users(user_id, email, `password`, date_registered) VALUES (?, ?, ?, NOW())",
		db.MakeId(),
			email,
			hash)

	if err != nil {
		errors.Wrap(err, "could not insert user")
	}

	return err
}
Is there something obvious I've missed here?

e: turns out you have to import _ "github.com/go-sql-driver/mysql"

spiritual bypass fucked around with this message at 02:47 on Feb 26, 2018

spiritual bypass
Feb 19, 2008

Grimey Drawer
Anyone encountered a problem where pprof starts in interactive mode and then quits immediately without providing a prompt?

code:
$ go tool pprof mysql.test cpu.out           
File: mysql.test
Type: cpu
Time: Jun 10, 2018 at 10:46am (EDT)
Duration: 4.81s, Total samples = 4.60s (95.70%)
Entering interactive mode (type "help" for commands, "o" for options)
That's all I get and it quits.

I'm trying to figure out why a short set of database tests take about 1s each to perform some basic inserts and reads on an empty MySQL database. It seems absurdly slow compared to what I'd expect from other languages and how the database performs at an interactive prompt.

spiritual bypass
Feb 19, 2008

Grimey Drawer
Thanks, I wasn't even aware of that option.

e: After using trace, it turns out it wasn't even the database access. I'd written tests that all involve checking user passwords and I'd forgotten that bcrypt is, of course, very slow. In fact, it involves something called expensiveBlowfishSetup

spiritual bypass fucked around with this message at 00:44 on Jun 21, 2018

spiritual bypass
Feb 19, 2008

Grimey Drawer
Basically every natively compiled language except C compiles all the same-language libraries into the binary

spiritual bypass
Feb 19, 2008

Grimey Drawer
Are there any tools available to automatically generate a module file for an existing project? I'd really like to ditch this GOPATH stuff, but I also don't feel like enumerating my dependencies by hand.

spiritual bypass
Feb 19, 2008

Grimey Drawer

Eela6 posted:

If you're using modules, `go build` will do what you want automatically

Oh, I just need a go.mod and the compiler handles the rest? That's lovely

spiritual bypass
Feb 19, 2008

Grimey Drawer
I encountered a little issue, but it was easy, really. Took me 20 minutes to switch my project to modules. My project is located in GOPATH, so I had to set an env var to make it work. I took way more steps than this but then filtered out the ones that weren't really necessary:

  1. GO111MODULE=on go build
  2. Oh no! A dependency's latest release version is old and contains a bug. Run GO111MODULE=on go get github.com/alexedwards/scs@master to install it at the latest revision
  3. GO111MODULE=on go build

Now you're cooking with modules. Skip the GO111MODULE var if you're outside the gopath. Too bad the docs for this are all focused on theoretical bullshit instead of "here's how to use it"

spiritual bypass
Feb 19, 2008

Grimey Drawer
iirc the dep guy is a reject from the Drupal world which is funny because Drupal ignored the one thing PHP got right, dependency management with Composer. Now Go has a Composer-like solution instead of whatever the hell dep was trying to do.

spiritual bypass
Feb 19, 2008

Grimey Drawer
Can you put the definition into a git submodule?

spiritual bypass
Feb 19, 2008

Grimey Drawer
Anyone ever seen the SQL library panic under high concurrency? I'm running a load test on a reasonably simple API backend and I start getting lots of panics once I pass about 250 req/s. Seems kinda bizarre to me, but there's still more investigation to do...

spiritual bypass
Feb 19, 2008

Grimey Drawer
No, it doesn't detect anything in this case.

But it turns out doing defer rows.Close() before handing the error on the Query() means you're invoking a method on a potentially nil rows which is obviously a good time to panic :pwn:
Now it's time to move the error handling sooner in the sequence and run my load test again. Then I'll know what the actual database errors are and handle them appropriately. Feels good to get over this speedbump after crossing so many little hurdles on this particular idiot spare time project.

spiritual bypass
Feb 19, 2008

Grimey Drawer
Why can't I successfully compare two strings from a url.Hostname()?

When I run this code:
code:
if base.Host != linkUrl.Host {
	log.Printf(`%+q does not match %+q`, base.Host, fullUrl.Host)
	continue
}
I get this sort of output:
code:
2019/06/07 16:07:38 "localhost:8000" does not match "localhost:8000"
2019/06/07 16:07:38 "localhost:8000" does not match "localhost:8000"
2019/06/07 16:07:38 "localhost:8000" does not match "localhost:8000"
2019/06/07 16:07:38 "localhost:8000" does not match "bcbsdharma.org"
That string comparison ought to be pretty straightforward, right? Am I doing something stupid or is Go broken somehow? It's hard to imagine the latter :pwn:

e: welp, upon posting this I realize that linkUrl is not fullUrl

Adbot
ADBOT LOVES YOU

spiritual bypass
Feb 19, 2008

Grimey Drawer
What advantage does singleton provide over passing in the logger and DB connections (or storage providers) as args? In my opinion, it's always better for a web server to instantiate things at startup time instead of attempting to do it in a handler. This way you can write tests and benchmarks with full confidence that all of the needed pieces go in as args instead of having to remember to set up other state beforehand.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply