Search code examples
godaemon

Go: Making a daemon that is callable from other Go apps


I'm working on a giant dictionary of words -> language, the data for which I have, but what I need is to have one thread running a daemon, written in Go, which keeps all of this in memory (yes, I have that much memory too) and is "callable" by other Go apps.

I'm sure this is a standard type of thing to do, but to be honest I've never attempted such a thing before and am not familiar enough to know where to find information on how to do this.

Having it run as a daemon is easy. My problem is what is an efficient way of calling this app from another Go app, which will need to be done many millions of times.

I'm thinking something along the lines of:

connection, err := InitateConnectionToApp()
for _, someword := range mysliceofstrings {
    languageofword := connection.FindIt(someword)
}

Then the daemon somehow receives this request, looks up the value in its map and delivers it back.

I hope that makes sense. I have tried looking on Google but there is nothing I can find specific to Go.

If anyone can tell me where to start that would be great.


Solution

  • You could use RPC Go's standard Remote Procedure Call package.

    Just Expose your api and then create a client to invoke the method remotely.

    Simple example copy pasted from the docs:

    package server
    
    type Args struct {
        A, B int
    }
    
    type Quotient struct {
        Quo, Rem int
    }
    
    type Arith int
    
    func (t *Arith) Multiply(args *Args, reply *int) error {
        *reply = args.A * args.B
        return nil
    }
    
    func (t *Arith) Divide(args *Args, quo *Quotient) error {
        if args.B == 0 {
            return errors.New("divide by zero")
        }
        quo.Quo = args.A / args.B
        quo.Rem = args.A % args.B
        return nil
    }
    
    func main() {
     arith := new(Arith)
     rpc.Register(arith)
     rpc.HandleHTTP()
     l, e := net.Listen("tcp", ":1234")
     if e != nil {
        log.Fatal("listen error:", e)
     }
     go http.Serve(l, nil)
    }