r/java • • Jan 16 '23

Building a PostgreSQL Wire Protocol Server using Vanilla, Modern Java 21 (shows use of: Records, Sealed Types, Pattern Matching for Switch, Virtual Threads, and Panama FFM)

https://gavinray97.github.io/blog/postgres-wire-protocol-jdk-21
224 Upvotes

23 comments sorted by

View all comments

3

u/bytedonor Jan 16 '23 edited Jan 16 '23

Why not just use ‘ServerSocket’, accept connections on main and pass the open socket to a VT executor task? I did something like this recently and was able to pump data across 500K web sockets in my test

7

u/GavinRayDev Jan 16 '23

No particular reason, outside of the fact that I tried to write it originally with an async socket for performance, couldn't find any good examples online:

There's one article which shows some super basic usage and then a bunch of regurgitated articles with questionable code patterns

So I did some digging in Github for usages of the API and tried to put something together that was a bit more full-fledged. Now there exists a better reference article for it.

I'm not familiar enough with the internals of Loom to know whether the Virtual Threads make a huge impact in this async context (maybe /u/pron98 can comment) but I had thought that since it's used for the channel group executor service it should be alright.

9

u/pron98 Jan 17 '23

Using an ordinary ServerSocket polled in a loop on a virtual thread should be about the same if not better. All sockets used on virtual threads automatically become effectively asynchronous from the OS's perspective anyway.

1

u/kaperni Jan 17 '23

Would it make sense to run ServerSocker::accept in a dedicated platform thread? Or is it same-same?

1

u/rbygrave Jan 18 '23

Just noting that with Nima yes it does use a platform thread for that. To me that makes sense - dedicate a platform thread to that task.