Guest User

Untitled

a guest
Mar 22nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import fs2.internal.NonFatal
  2. import java.nio.channels.AsynchronousChannelGroup
  3. import scala.concurrent.ExecutionContext
  4.  
  5. // TODO: Unfuckulate this giant mess
  6.  
  7. object backendImplicits {
  8. import fs2._
  9. import java.util.concurrent.Executors
  10.  
  11. implicit val tcpACG : AsynchronousChannelGroup = namedACG.namedACG("tcp")
  12. // hehe
  13. implicit val Sch : Scheduler = Scheduler.fromScheduledExecutorService(Executors.newScheduledThreadPool(16, threadFactoryFactoryProxyBeanFactory.mkThreadFactory("scheduler", daemon = true)))
  14. }
  15.  
  16. object namedACG {
  17.  
  18. /**
  19. Lifted verbatim from fs2 tests.
  20. I have no idea what it does, but it makes stuff work...
  21. */
  22.  
  23. import java.nio.channels.AsynchronousChannelGroup
  24. import java.lang.Thread.UncaughtExceptionHandler
  25. import java.nio.channels.spi.AsynchronousChannelProvider
  26. import java.util.concurrent.ThreadFactory
  27. import java.util.concurrent.atomic.AtomicInteger
  28.  
  29. def namedACG(name:String):AsynchronousChannelGroup = {
  30. val idx = new AtomicInteger(0)
  31. AsynchronousChannelProvider.provider().openAsynchronousChannelGroup(
  32. 16
  33. , new ThreadFactory {
  34. def newThread(r: Runnable): Thread = {
  35. val t = new Thread(r, s"fs2-AG-$name-${idx.incrementAndGet() }")
  36. t.setDaemon(true)
  37. t.setUncaughtExceptionHandler(
  38. new UncaughtExceptionHandler {
  39. def uncaughtException(t: Thread, e: Throwable): Unit = {
  40. println("----------- UNHANDLED EXCEPTION ---------")
  41. e.printStackTrace()
  42. }
  43. })
  44. t
  45. }
  46. }
  47. )
  48. }
  49. }
  50.  
  51. object threadFactoryFactoryProxyBeanFactory {
  52.  
  53. import java.lang.Thread.UncaughtExceptionHandler
  54. import java.util.concurrent.{Executors, ThreadFactory}
  55. import java.util.concurrent.atomic.AtomicInteger
  56.  
  57. def mkThreadFactory(name: String, daemon: Boolean, exitJvmOnFatalError: Boolean = true): ThreadFactory = {
  58. new ThreadFactory {
  59. val idx = new AtomicInteger(0)
  60. val defaultFactory = Executors.defaultThreadFactory()
  61. def newThread(r: Runnable): Thread = {
  62. val t = defaultFactory.newThread(r)
  63. t.setName(s"$name-${idx.incrementAndGet()}")
  64. t.setDaemon(daemon)
  65. t.setUncaughtExceptionHandler(new UncaughtExceptionHandler {
  66. def uncaughtException(t: Thread, e: Throwable): Unit = {
  67. ExecutionContext.defaultReporter(e)
  68. if (exitJvmOnFatalError) {
  69. e match {
  70. case NonFatal(_) => ()
  71. case fatal => System.exit(-1)
  72. }
  73. }
  74. }
  75. })
  76. t
  77. }
  78. }
  79. }
  80. }
Add Comment
Please, Sign In to add comment