2017-10-09

(sph server)

a generic socket data processing server that uses a thread-pool for parallel request processing.

listens for requests from the internet or local sockets via http or any other, possibly custom, protocol

supports all kinds of sockets and offers a utility procedure to create tcp and local unix sockets

automatically resumes on exceptions to be long running

exchangeable listen-loops and sockets

sets up signal, pipe, socket, connection and exception handling

see (sph server fibers) for a listen-loop that uses fibers instead of a thread-pool

part of sph-lib

example

(import (sph server))

(server-listen
  (lambda (client-port) (let ((incoming-data (read client-port))) (write "got it" client-port)))
  (server-socket "::1" #:port 8080))

exit with sigint or C-c

can be tested with

echo data | netcat "::1" 8080

performance example

a local test of a sph-web-app http/scgi scheme application through nginx on a phenom 2 x2 565 processor (produced until 2012)

1000 requests, in batches of 100 concurrent requests

ab -n 1000 -c 100 $url
Requests per second: 10296.19 [#/sec] (mean)

library description

starting the server with server-listen makes it listen on an existing or newly created socket.

if there is a new connection on the socket, a user supplied procedure is called with a client port to receive and send data

import name

(sph server)

exports

server-default-port

variable

server-listen

procedure

signature

handle-request socket #:parallelism #:exception-key ->

procedure:{port:client -> unspecified} port:socket [#:key parallelism integer/false] -> unspecified

description

listen for new connections on socket and call handle-request with a input/output port for the client.

handle-request is called in the next free thread in a thread-pool.

the server is stopped when it receives the signal SIGINT or SIGTERM.

currently all exceptions are catched, printed and the server continues listening

server-socket

procedure

signature

address #:port #:type #:protocol #:set-options #:non-blocking ->

description

create a socket with default options. the socket type is inferred

from the address, which can be an ip4 or ip6 address (tcp) or a filesystem path (unix socket)

sph-server-description

variable

tags: programming guile documentation library scheme sph-lib q1 server highlight sph-server