Although the result channel send operation and context cancel are
guarded by the same mutex and context is cancelled by the main thread
when it receives a result message from the channnel, there is absolutely
no guarantee that one goroutine can't write result to the channel after
the first goroutine did the same and before the main thread cancelled
the context. This is the following scenario:
- Worker #1 acquires mutex, writes to the result channel
- Worker #2 acquires mutex, writes to the result channel
- Main thread reads from the result channel, acquires mutex, cancels the
context
Since our result channel (and error channel) had buffer of size 1, the
Worker #2 was hanging indefinitely. Setting the buffer size to the
number of goroutines fixed the problem.
Alternative scenario was:
- Worker #1 acquires mutex, writes to the result channel
- Main thread acquires mutex, reads from the result channel, cancels the
context
- Worker #2 receives cancel notification so it completes without
yielding the result
That's why deadlock was happening only from time to time.
Initially, we did not wait for all goroutines to finish which was fine
as long as we did not run all tests with `go test` and observe deadlock
in 50% of cases. This is interesting, since deadlock never occurred when
running safe_prime_generator_test.go. Anyway, waiting for all go
routines to finish shouldn't harm the performance a lot and it gives a
guarantee we did not miss anything.
How fast we generate a prime number is mostly a matter of luck. It
varies from miliseconds to seconds and depends how lucky we are with
drawing bytes.
With today's multicore processors, we can execute the process on
multiple cores concurrently, accept the first valid result and cancel
the rest of work. This way, with the same algorithm, we get the result
faster.
I've adapted a Go code for generating random numbers by inserting some
optimisations that will allow us to generate safe primes faster than
with the previous, naive approach.
First of all, having q which can be prime, we first check whether q%3=1.
If that's true, there is no chance p=2q+1 is prime. It lets us to reject
candidate numbers quicker without running an expensive primality tests.
Also, before we run a primality test for q, we may check p=2q+1 against
the primes between 3-53 (We are limited by Go's uint64 range).
If all those conditions are met and we know p is prime, it's enough to
check Pocklington criterion for q instead of running an expensive
primality test for it.