Documentation
¶
Overview ¶
Package cloudevents provides CloudEvents client and server implementations.
This package wraps the official CloudEvents SDK for Go, offering simplified interfaces for sending, receiving, and processing CloudEvents over HTTP and other protocols.
Features:
- HTTP client for sending and receiving CloudEvents
- Request-response pattern support
- Asynchronous event receiver with lifecycle management
- Result types for event delivery status
- CloudEvents v1.0 specification compliance
Example:
client, _ := cloudevents.NewHttp("http://localhost:8080", nil, nil)
event := cloudevents.NewEvent()
event.SetType("com.example.event")
event.SetSource("example/source")
result := client.Send(event)
Package cloudevents provides CloudEvents client and server implementations.
This package wraps the official CloudEvents SDK for Go, offering simplified interfaces for sending, receiving, and processing CloudEvents over HTTP and other protocols.
Features:
- HTTP client for sending and receiving CloudEvents
- Request-response pattern support
- Asynchronous event receiver with lifecycle management
- Result types for event delivery status
- CloudEvents v1.0 specification compliance
Example:
client, _ := cloudevents.NewHttp("http://localhost:8080", nil, nil)
event := cloudevents.NewEvent()
event.SetType("com.example.event")
event.SetSource("example/source")
result := client.Send(event)
Package cloudevents provides CloudEvents client and server implementations.
This package wraps the official CloudEvents SDK for Go, offering simplified interfaces for sending, receiving, and processing CloudEvents over HTTP and other protocols.
Features:
- HTTP client for sending and receiving CloudEvents
- Request-response pattern support
- Asynchronous event receiver with lifecycle management
- Result types for event delivery status
- CloudEvents v1.0 specification compliance
Example:
client, _ := cloudevents.NewHttp("http://localhost:8080", nil, nil)
event := cloudevents.NewEvent()
event.SetType("com.example.event")
event.SetSource("example/source")
result := client.Send(event)
Package cloudevents provides CloudEvents client and server implementations.
This package wraps the official CloudEvents SDK for Go, offering simplified interfaces for sending, receiving, and processing CloudEvents over HTTP and other protocols.
Features:
- HTTP client for sending and receiving CloudEvents
- Request-response pattern support
- Asynchronous event receiver with lifecycle management
- Result types for event delivery status
- CloudEvents v1.0 specification compliance
Example:
client, _ := cloudevents.NewHttp("http://localhost:8080", nil, nil)
event := cloudevents.NewEvent()
event.SetType("com.example.event")
event.SetSource("example/source")
result := client.Send(event)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NewEvent = v2.NewEvent
NewEvent creates a new CloudEvent with default attributes.
Returns:
- Event: A new CloudEvent instance with generated ID and timestamp
The returned event must have at minimum Type and Source set before being valid according to the CloudEvents specification.
Example:
event := cloudevents.NewEvent()
event.SetType("com.example.user.created")
event.SetSource("example/users")
event.SetData("application/json", userData)
Functions ¶
func NewHttp ¶
func NewHttp(address string, httpOption []http.Option, clientOption []cloudeventssdk_client.Option) (*client, error)
NewHttp creates and returns an HTTP client for CloudEvents.
Parameters:
- address: Target URL for sending events (e.g., "http://localhost:8080")
- httpOption: HTTP protocol options from CloudEvents SDK (e.g., WithPort, WithPath)
- clientOption: Client configuration options from CloudEvents SDK
Returns:
- *client: Configured CloudEvents client
- error: Error if client creation or protocol initialization fails
The client can be used for sending events, making request-response calls, or receiving events. For receivers, leave address empty and configure port via httpOption.
Example:
// Sender client
client, err := cloudevents.NewHttp("http://localhost:8080", nil, nil)
if err != nil {
log.Fatal(err)
}
// Receiver client
httpOpts := []http.Option{http.WithPort(8080)}
receiver, err := cloudevents.NewHttp("", httpOpts, nil)
Types ¶
type Event ¶
Event represents a CloudEvent conforming to the CloudEvents v1.0 specification.
Events have required attributes (Type, Source, ID, SpecVersion) and optional attributes (DataContentType, DataSchema, Subject, Time). Events can carry data payloads of any type.
Example:
var event cloudevents.Event
event.SetID("abc-123")
event.SetType("com.example.object.action")
event.SetSource("example/source")
event.SetData("application/json", myData)
// Access attributes
eventType := event.Type()
eventSource := event.Source()
data := event.Data()
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result is the result of event delivery.
func NewHTTPResult ¶
NewHTTPResult creates and returns an HTTP-specific CloudEvents result with status code.
Parameters:
- statusCode: HTTP status code (e.g., 200, 400, 500)
- format: Printf-style format string describing the result
- arguments: Optional format arguments
Returns:
- Result: CloudEvents result with HTTP status code and formatted message
This result type includes HTTP-specific information and can be retrieved using GetHttpStatusCode method.
Example:
// Success with 200 OK result := cloudevents.NewHTTPResult(200, "event accepted") // Client error with 400 Bad Request result = cloudevents.NewHTTPResult(400, "invalid event type: %s", eventType) // Server error with 500 Internal Server Error result = cloudevents.NewHTTPResult(500, "processing error: %v", err)
func NewResult ¶
NewResult creates and returns a generic CloudEvents result.
Parameters:
- format: Printf-style format string describing the result
- arguments: Optional format arguments
Returns:
- Result: CloudEvents result with the formatted message
Use this for protocol-agnostic results. For HTTP-specific results with status codes, use NewHTTPResult instead.
Example:
// Success result
result := cloudevents.NewResult("event processed successfully")
// Error result with details
result = cloudevents.NewResult("validation failed: %s", validationError)
func (*Result) Error ¶
Error returns the error message string from the result.
Returns:
- string: Error message, or empty string if no error
This implements the error interface, allowing Result to be used as an error type. The message includes details about why an event was NACK'd or undelivered.
Example:
result := client.Send(event)
if !result.IsACK() {
log.Printf("Event failed: %s", result.Error())
}
func (*Result) GetHttpStatusCode ¶
GetHttpStatusCode extracts the HTTP status code from an HTTP result.
Returns:
- int: HTTP status code (e.g., 200, 404, 500), or -1 if not an HTTP result
- error: Error if result is not an HTTP result, nil otherwise
This method only works with results created by NewHTTPResult or returned from HTTP-based event operations. Returns an error for non-HTTP results.
Example:
result := client.Send(event)
statusCode, err := result.GetHttpStatusCode()
if err != nil {
log.Println("Not an HTTP result")
} else {
log.Printf("HTTP Status: %d", statusCode)
if statusCode >= 400 {
log.Println("HTTP error occurred")
}
}
func (*Result) IsACK ¶
IsACK returns whether the recipient acknowledged the event.
Returns:
- bool: true if event was acknowledged (ACK), false otherwise
An ACK indicates successful delivery and processing. For HTTP, this typically corresponds to 2xx status codes.
Example:
result := client.Send(event)
if result.IsACK() {
log.Println("Event acknowledged")
}
func (*Result) IsNACK ¶
IsNACK returns whether the recipient did not acknowledge the event.
Returns:
- bool: true if event was not acknowledged (NACK), false otherwise
A NACK indicates the event was delivered but rejected or failed processing. For HTTP, this typically corresponds to 4xx or 5xx status codes.
Example:
result := client.Send(event)
if result.IsNACK() {
log.Printf("Event rejected: %s", result.Error())
}
func (*Result) IsUndelivered ¶
IsUndelivered returns whether the event could not be delivered.
Returns:
- bool: true if event was undelivered, false if delivery was attempted
Undelivered indicates a network error, connection failure, or other issue that prevented the event from reaching the recipient. This is different from NACK where the event was delivered but rejected.
Example:
result := client.Send(event)
if result.IsUndelivered() {
log.Printf("Delivery failed: %s", result.Error())
// Retry or handle connection error
}
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a struct that provides server related methods.
func (*Server) Start ¶
func (s *Server) Start(address string, handler func(Event) (*Event, Result), listenAndServeFailureFunc func(error)) error
Start starts the CloudEvents HTTP server on the specified address.
Parameters:
- address: Server address to bind to (e.g., "localhost:8080" or ":8080")
- handler: Function to process incoming events and optionally return response events
- listenAndServeFailureFunc: Function called if the HTTP server encounters a fatal error
Returns:
- error: Error if server initialization fails, nil if started successfully
The handler function receives each incoming CloudEvent and can return a response event for request-response patterns. Return nil for the event if no response is needed. The server runs asynchronously; this method returns after starting the server.
Example:
var server cloudevents.Server
handler := func(event cloudevents.Event) (*cloudevents.Event, cloudevents.Result) {
log.Printf("Received: %s", event.Type())
// Process event...
// Return response event
response := cloudevents.NewEvent()
response.SetType("com.example.response")
response.SetSource("example/server")
return &response, cloudevents.NewHTTPResult(200, "OK")
}
failureFunc := func(err error) {
log.Printf("Server error: %v", err)
}
err := server.Start(":8080", handler, failureFunc)
if err != nil {
log.Fatal(err)
}
func (*Server) Stop ¶
Stop gracefully shuts down the CloudEvents server.
Parameters:
- shutdownTimeout: Maximum duration to wait for active connections to complete
Returns:
- error: Error if shutdown fails or times out, nil if successful
The server stops accepting new connections immediately and waits for active connections to complete within the timeout period. After the timeout, the server forcefully closes remaining connections.
Example:
server.Start(":8080", handler, failureFunc)
// ... server running ...
// Graceful shutdown with 10 second timeout
err := server.Stop(10 * time.Second)
if err != nil {
log.Printf("Shutdown error: %v", err)
}
log.Println("Server stopped")