Skip to content

Instantly share code, notes, and snippets.

package com.acme;
import com.prevoty.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
type (
RPC struct {
cache map[string]string
requests *Requests
mu *sync.RWMutex
}
CacheItem struct {
Key string
Value string
func NewRPC() *RPC {
return &RPC{
cache: make(map[string]string),
requests: &Requests{},
mu: &sync.RWMutex{},
}
}
func (r *RPC) Get(key string, resp *CacheItem) (err error) {
r.mu.RLock()
defer r.mu.RUnlock()
cacheValue, found := r.cache[key]
if !found {
return NotFoundError
}
func (r *RPC) Put(item *CacheItem, ack *bool) error {
r.mu.Lock()
defer r.mu.Unlock()
r.cache[item.Key] = item.Value
*ack = true
r.requests.Put++
return nil
}
func (r *RPC) Delete(key string, ack *bool) error {
r.mu.Lock()
defer r.mu.Unlock()
var found bool
_, found = r.cache[key]
if !found {
return NotFoundError
}
func (r *RPC) Clear(skip bool, ack *bool) error {
r.mu.Lock()
defer r.mu.Unlock()
r.cache = make(map[string]string)
*ack = true
r.requests.Clear++
return nil
}
func (r *RPC) Stats(skip bool, requests *Requests) error {
*requests = *r.requests
return nil
}
package main
import (
"log"
"net"
"net/rpc"
"runtime"
)
func init() {
type (
Client struct {
connection *rpc.Client
}
)
func NewClient(dsn string, timeout time.Duration) (*Client, error) {
connection, err := net.DialTimeout("tcp", dsn, timeout)
if err != nil {
return nil, err