all repos — honk @ 1cdcf0570ab2ce9ec07863dd7670d1e7e42173e4

my fork of honk

backend.go (view raw)

  1//
  2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
  3//
  4// Permission to use, copy, modify, and distribute this software for any
  5// purpose with or without fee is hereby granted, provided that the above
  6// copyright notice and this permission notice appear in all copies.
  7//
  8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15
 16package main
 17
 18import (
 19	"bytes"
 20	"net"
 21	"net/rpc"
 22	"os"
 23	"os/exec"
 24
 25	"humungus.tedunangst.com/r/webs/gate"
 26	"humungus.tedunangst.com/r/webs/image"
 27)
 28
 29type Shrinker struct {
 30}
 31
 32type ShrinkerArgs struct {
 33	Buf    []byte
 34	Params image.Params
 35}
 36
 37type ShrinkerResult struct {
 38	Image *image.Image
 39}
 40
 41var shrinkgate = gate.NewLimiter(4)
 42
 43func (s *Shrinker) Shrink(args *ShrinkerArgs, res *ShrinkerResult) error {
 44	shrinkgate.Start()
 45	defer shrinkgate.Finish()
 46	img, err := image.Vacuum(bytes.NewReader(args.Buf), args.Params)
 47	if err != nil {
 48		return err
 49	}
 50	res.Image = img
 51	return nil
 52}
 53
 54func backendSockname() string {
 55	return dataDir + "/backend.sock"
 56}
 57
 58func shrinkit(data []byte) (*image.Image, error) {
 59	cl, err := rpc.Dial("unix", backendSockname())
 60	if err != nil {
 61		return nil, err
 62	}
 63	defer cl.Close()
 64	var res ShrinkerResult
 65	err = cl.Call("Shrinker.Shrink", &ShrinkerArgs{
 66		Buf:    data,
 67		Params: image.Params{LimitSize: 4200 * 4200, MaxWidth: 2048, MaxHeight: 2048},
 68	}, &res)
 69	if err != nil {
 70		return nil, err
 71	}
 72	return res.Image, nil
 73}
 74
 75var backendhooks []func()
 76
 77func backendServer() {
 78	dlog.Printf("backend server running")
 79	shrinker := new(Shrinker)
 80	srv := rpc.NewServer()
 81	err := srv.Register(shrinker)
 82	if err != nil {
 83		elog.Panicf("unable to register shrinker: %s", err)
 84	}
 85
 86	sockname := backendSockname()
 87	err = os.Remove(sockname)
 88	if err != nil && !os.IsNotExist(err) {
 89		elog.Panicf("unable to unlink socket: %s", err)
 90	}
 91
 92	lis, err := net.Listen("unix", sockname)
 93	if err != nil {
 94		elog.Panicf("unable to register shrinker: %s", err)
 95	}
 96	err = setLimits()
 97	if err != nil {
 98		elog.Printf("error setting backend limits: %s", err)
 99	}
100	for _, h := range backendhooks {
101		h()
102	}
103	srv.Accept(lis)
104}
105
106func runBackendServer() {
107	proc := exec.Command(os.Args[0], reexecArgs("backend")...)
108	proc.Stdout = os.Stdout
109	proc.Stderr = os.Stderr
110	err := proc.Start()
111	if err != nil {
112		elog.Panicf("can't exec backend: %s", err)
113	}
114	err = proc.Wait()
115	elog.Printf("lost the backend: %s", err)
116}