如何从清漆子程序调用Go程序

Say "I'm running varnish and I want to call Go code from varnish subroutines". that will run for every vcl_recv subroutine?

From what I gather form the Varnish docs, an official way to have FFI in Varnish is using VMODsVarnish Modules.

It appears to be presupposed one uses C to write VMODs (maybe with the little help of vmodtool.py which is able to translate VCC to plain C).

Still, you could explore two approaches to hook Go into that mix:

  • Compile your Go code using the -buildmode=c-shared mode to produce a .so file with the C-compatible interface.

    This approach will create a "free-standing" Varnish module, which does not rely on libc, just exports a C-compatible API from the library.

    But while this is doable, you'd need to "mirror" certain data structures provided by Varnish and maybe you'll need to call into Varnish API back from your module. So the next approach seems to be more feasible.

  • Use cgo to wire up your Go code with C API exported by Varnish. You will supposedly still need to use -buildmode=c-shared.

All-in-all, you'd better first play with the native VMODs as documented in the Varnish book I linked to.


I'd also make the following minor points:

  • I have no personal experience with VMODs. It's quite possible they're supposed to be linked-in statically when building Varnish from the source.

    If so, you'd need to use -buildmode=c-archive.

  • It appears that doing what you're after would be an uphill battle for you, so I'd seriously consider porting whatever algorithm you have implemented in Go to that Varnish-specific VCC.