To implement a graceful shutdown for a GOCACHEPROG subprocess, listen for the close command in the incoming JSON stream and exit immediately after responding. The go command sends a Request with Command set to CmdClose ("close"), and the subprocess must reply with a Response containing the matching ID before terminating.
package main
import (
"encoding/json"
"os"
"cmd/go/internal/cacheprog"
)
func main() {
for {
var req cacheprog.Request
if err := json.NewDecoder(os.Stdin).Decode(&req); err != nil {
return
}
if req.Command == cacheprog.CmdClose {
resp := cacheprog.Response{ID: req.ID}
json.NewEncoder(os.Stdout).Encode(resp)
os.Exit(0)
}
// Handle other commands (put, get) here
}
}