# hashicorp/go-plugin architecture

> Architecture diagram of the GitHub repository hashicorp/go-plugin, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hashicorp/go-plugin
- Repository: https://github.com/hashicorp/go-plugin

## Overview

##### Main Components

a) Host Process (Main Application)
- This as a primary container at the top
- Subcomponents:
- Plugin Client Manager
- RPC/gRPC Client Interface
- MuxBroker (for multiplexing connections)
- Logger Integration

b) Plugin Process (Subprocess)
- This as a parallel container
- Subcomponents:
- Plugin Server
- RPC/gRPC Server Interface
- Interface Implementation

c) Communication Layer
- Bidirectional channels between Host and Plugin
- Both supported protocols:
- net/rpc channel
- gRPC channel (HTTP/2)
- Multiplexed connections

##### Key Relationships to Highlight

- Host Process → Plugin Launch
- Plugin Registration
- Interface Discovery
- Bidirectional Communication

- Security Components:
- TLS Connection (optional)
- Checksum Verification
- Protocol Version Validation

##### Specific Implementation Details

- Blue rectangles for primary processes (Host/Plugin)
- Green ovals for communication protocols
- Yellow diamonds for security components
- Orange rectangles for brokers/multiplexers
- Purple rectangles for interface implementations

##### Special Features

- Stdout/stderr syncing mechanism
- Logging bridge
- Demonstrate the reattachment capability
- TTY preservation system

##### Architectural Patterns to Emphasize

- Process Isolation boundary
- Multiplexing pattern
- Interface-based communication
- Security boundaries

**[Host Process Layer]**
- ↕
**[Communication/Security Layer]**
- ↕
**[Plugin Process Layer]**

Important Implementation Notes:
- Use the file structure from internal/plugin/ for protocol definitions
- Relationship between grpc_broker.go and mux_broker.go
- How the cmdrunner package manages plugin processes
- Demonstrate the relationship between client.go and server.go

Remember to:
##### Plugin discovery mechanisms

- Process isolation
- Cross-language support
- Bidirectional communication
- Security measures
- Logging system
- Protocol versioning
- TTY handling
- Reattachment capabilities

This will provide a comprehensive view of the plugin system while maintaining clarity about the separation of concerns and communication patterns.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Host
        PCM[Plugin Client Manager]
        RPCC[RPC Client Interface]
        GRPCC[gRPC Client Interface]
        MB[MuxBroker]
        LOG[Logger System]
        PVM[Protocol Version Manager]
        TLS[TLS Security]
        PD[Plugin Discovery]
        PPC[Plugin Process Control]
        EH[Error Handler]
    end

    subgraph Communication
        direction TB
        PROT[Protocol Definitions]
        MS[Multiplexing System]
        SH[Stream Handler]
        GB[gRPC Broker]
    end

    subgraph Plugin
        PS[Plugin Server]
        RPCS[RPC Server Interface]
        GRPCS[gRPC Server Interface]
        II[Interface Implementation]
        PM[Process Management]
    end

    %% Host Process Relationships
    PCM --> MB
    PCM --> RPCC
    PCM --> GRPCC
    MB --> GB
    LOG --> SH

    %% Communication Layer
    RPCC <--> PROT
    GRPCC <--> PROT
    PROT <--> MS
    MS <--> GB
    GB <--> SH

    %% Plugin Process Relationships
    PS --> RPCS
    PS --> GRPCS
    RPCS --> II
    GRPCS --> II
    PM --> PS

    %% Security and Control Flow
    PVM --> PROT
    TLS --> PROT
    PD --> PCM
    PPC --> PM
    EH --> SH

    %% Styling
    classDef host fill:#a8d1f0
    classDef comm fill:#90EE90
    classDef plugin fill:#FFB6C1
    classDef security fill:#FFD700
    classDef error fill:#FF6B6B

    class PCM,RPCC,GRPCC,MB,LOG host
    class PROT,MS,SH,GB comm
    class PS,RPCS,GRPCS,II,PM plugin
    class PVM,TLS,PD security
    class EH error

    %% Click Events
    click PCM "https://github.com/hashicorp/go-plugin/blob/main/client.go"
    click RPCC "https://github.com/hashicorp/go-plugin/blob/main/rpc_client.go"
    click GRPCC "https://github.com/hashicorp/go-plugin/blob/main/grpc_client.go"
    click MB "https://github.com/hashicorp/go-plugin/blob/main/mux_broker.go"
    click LOG "https://github.com/hashicorp/go-plugin/blob/main/log_entry.go"
    click PS "https://github.com/hashicorp/go-plugin/blob/main/server.go"
    click RPCS "https://github.com/hashicorp/go-plugin/blob/main/rpc_server.go"
    click GRPCS "https://github.com/hashicorp/go-plugin/blob/main/grpc_server.go"
    click PROT "https://github.com/hashicorp/go-plugin/tree/main/internal/plugin/"
    click MS "https://github.com/hashicorp/go-plugin/tree/main/internal/grpcmux/"
    click PM "https://github.com/hashicorp/go-plugin/tree/main/internal/cmdrunner/"
    click TLS "https://github.com/hashicorp/go-plugin/blob/main/mtls.go"
    click PVM "https://github.com/hashicorp/go-plugin/blob/main/protocol.go"
    click GB "https://github.com/hashicorp/go-plugin/blob/main/grpc_broker.go"
    click SH "https://github.com/hashicorp/go-plugin/blob/main/stream.go"
    click EH "https://github.com/hashicorp/go-plugin/blob/main/error.go"
    click PD "https://github.com/hashicorp/go-plugin/blob/main/discover.go"
    click PPC "https://github.com/hashicorp/go-plugin/blob/main/process.go"
```
