Skip to content

RPC with a pool of providers each connected via a web-socket. Written in Go

License

Notifications You must be signed in to change notification settings

pavrorov/wsrpcpool

Repository files navigation

wsrpcpool

RPC with a pool of providers each connected via a web-socket. Written in Go.

The library provides an implementation of a rather inside-out client-server RPC architecture. Multiple backend clients connect to a (pool) server which then invokes backend methods via an RPC protocol. Basic call load-balancing and call-backs included.

The architecture is intended for a public service with a number of private providers (dynamic IPs, NAT).

Basic example

// Public part
pool := NewPool()
pool.Bind("/pool")
pool.ListenAndUse(":8080")

var ret string
err := pool.Call("Backend.Get", "object_id-1", &ret)
// Private part
rpc.Register(&Backend{})
p, err := NewProvider()
pc, err := p.ConnectAndServe("ws://pool.my:8080/pool")

HTTPS/WSS encryption

pool, err := NewPoolTLS("server.crt", "server.key")
go pool.ListenAndUseTLS(":8443")
// Specify one or more custom CA certs to verify the pool cert if necessary
p, err := NewProvider("rootCA.crt")
pc, err := p.ConnectAndServe("wss://pool.my:8443/pool")

Client certificate authentication

// Add one or more custom CA certs to verify a client cert if necessary
pool, err := NewPoolTLSAuth("server.crt", "server.key", "rootCA.crt")
go pool.ListenAndUseTLS(":8443")
// Add one or more custom CA certs to verify the pool cert if necessary
p, err := NewProviderTLSAuth("client.crt", "client.key", "rootCA.crt")
pc, err := p.ConnectAndServe("wss://pool.my:8443/pool")

Call-backs

// Pool side
rpc.Register(&PoolSide{})
pool.BindIn("/cb")
pc, err := p.ConnectAndUse("wss://pool.my:8443/cb")

var ret string
err := pc.Call("PoolSide.Notify", "event_id-1", &ret)

Use different URLs for different providers

pool.Bind("/") // the default path
pool.Bind("/db", "Database")
pool.Bind("/files", "Filesystem")
go pool.ListenAndUseTLS(":8443")

var ret string
pool.Call("Database.Select", "object_id-1", &ret) // goes to providers on /db
pool.Call("Filesystem.Open", "file_id-1", &ret) // goes to providers on /files
rpc.Register(&Database{})
p.ConnectAdServe("wss://pool.my:8443/db")
rpc.Register(&Filesystem{})
p.ConnectAdServe("wss://pool.my:8443/files")

Signals

go pool.ListenAndUseTLS(":8443")
<-pool.Listening
// Now the pool server is listening for incoming connections
pc, err := p.ConnectAndServe("wss://pool.my:8443/pool")

select {
case <-pc.Connected:
    // now connected or re-connected
case <-pc.Disconnected:
    // disconnected, may re-connect again
case <-pc.Closed:
    // closed by pc.Close() or MaxAttempts is exceeded
}

err := pc.Close()

JSON-RPC

pool.BindWith("/pool", jsonrpc.NewClient)
pool.BindInWith("/cb", jsonrpc.ServeConn)
pc.ConnectAndServeWith("wss://pool.my:8443/pool", jsonrpc.NewClient)
pc.ConnectAndUseWith("wss://pool.my:8443/pool", jsonrpc.ServeConn)

About

RPC with a pool of providers each connected via a web-socket. Written in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages