# Advanced configuration

This guide provides comprehensive documentation for configuring the Lithnet Google Apps Management Agent through the `miiserver.exe.config` file.

## Overview

The Lithnet Google Apps Management Agent supports advanced configuration options to optimize performance, control API rate limits, and manage threading behavior. These settings are configured in the **`miiserver.exe.config`** file which is located in the MIM Synchronization Service installation directory.

Most settings will not need to be changed from their defaults, but this guide details all available options for advanced users who need to fine-tune their configuration.

## Configuration Structure

### Declaring the Configuration Section

First, add the `lithnet-google-ma` configuration `<section>` declaration within the `<configSections>` element, if it does not already exist:

```xml
<configuration>
  <configSections>
    <section name="lithnet-google-ma" 
             type="Lithnet.GoogleApps.MA.MAConfigurationSection, Lithnet.GoogleApps.MA" />
  </configSections>
```

> **Note:** If your `miiserver.exe.config` file already has a `<configSections>` element, simply add the `<section>` declaration to the existing element instead of creating a new one.

### Main Configuration Element

All settings are contained within the `<lithnet-google-ma>` element, which should be placed in the body of the `<configuration>` element (after `</configSections>`):

```xml
<configuration>
  <configSections>
    <section name="lithnet-google-ma" 
             type="Lithnet.GoogleApps.MA.MAConfigurationSection, Lithnet.GoogleApps.MA" />
    <!-- other section declarations here -->
  </configSections>

  <lithnet-google-ma 
      export-threads="30"
      import-threads="50">
    <!-- API-specific configurations go here -->
  </lithnet-google-ma>
</configuration>
```

## Global Settings

These attributes are set directly on the `<lithnet-google-ma>` element:

| Attribute        | Type    | Default | Description                                             |
| ---------------- | ------- | ------- | ------------------------------------------------------- |
| `export-threads` | integer | `30`    | Maximum number of threads used during export operations |
| `import-threads` | integer | `50`    | Maximum number of threads used during import operations |

**Example:**

```xml
<lithnet-google-ma export-threads="40" import-threads="60">
```

## API-Specific Configuration

### Directory API

The Directory API is used for user, group, and organizational unit operations.

```xml
<directory-api 
    rate-limit="1500"
    pool-size="30"
    export-threads-group-member="5"
    import-threads-group-member="10"
    batch-size-group-member="100"
    concurrent-operations-group-member="10" />
```

| Attribute                            | Type    | Default | Description                                             |
| ------------------------------------ | ------- | ------- | ------------------------------------------------------- |
| `rate-limit`                         | integer | `1500`  | Maximum API requests per 100 seconds                    |
| `pool-size`                          | integer | `30`    | Connection pool size for API service instances          |
| `export-threads-group-member`        | integer | `5`     | Threads for exporting group membership data             |
| `import-threads-group-member`        | integer | `10`    | Threads for importing group membership data             |
| `batch-size-group-member`            | integer | `100`   | Number of operations per batch for group member changes |
| `concurrent-operations-group-member` | integer | `10`    | Maximum concurrent group member operations              |

### Groups Settings API

The Groups Settings API manages group configuration and settings.

```xml
<groupssettings-api 
    rate-limit="500"
    pool-size="30"
    import-threads-group-settings="30" />
```

| Attribute                       | Type    | Default | Description                                    |
| ------------------------------- | ------- | ------- | ---------------------------------------------- |
| `rate-limit`                    | integer | `500`   | Maximum API requests per 100 seconds           |
| `pool-size`                     | integer | `30`    | Connection pool size for API service instances |
| `import-threads-group-settings` | integer | `30`    | Threads for importing group settings           |

### Contacts API

The Contacts API manages domain shared contacts.

```xml
<contacts-api 
    rate-limit="1500"
    pool-size="30"
    page-size="1000" />
```

| Attribute    | Type    | Default | Description                                                    |
| ------------ | ------- | ------- | -------------------------------------------------------------- |
| `rate-limit` | integer | `1500`  | Maximum API requests per 100 seconds                           |
| `pool-size`  | integer | `30`    | Connection pool size for API service instances                 |
| `page-size`  | integer | `1000`  | Number of contacts retrieved per API page request (minimum: 1) |

### Calendar API

The Calendar API manages calendar resources and events.

```xml
<calendar-api 
    rate-limit="1500"
    pool-size="30" />
```

| Attribute    | Type    | Default | Description                                    |
| ------------ | ------- | ------- | ---------------------------------------------- |
| `rate-limit` | integer | `1500`  | Maximum API requests per 100 seconds           |
| `pool-size`  | integer | `30`    | Connection pool size for API service instances |

### Gmail API

The Gmail API manages Gmail settings and configurations.

```xml
<gmail-api 
    rate-limit="250" />
```

| Attribute    | Type    | Default | Description                        |
| ------------ | ------- | ------- | ---------------------------------- |
| `rate-limit` | integer | `250`   | Maximum API requests per 1 seconds |

### Classroom API

The Classroom API manages Google Classroom courses and memberships.

```xml
<classroom-api 
    rate-limit="50"
    pool-size="30"
    import-threads-course-member="10" />
```

| Attribute                      | Type    | Default | Description                                    |
| ------------------------------ | ------- | ------- | ---------------------------------------------- |
| `rate-limit`                   | integer | `50`    | Maximum API requests per 10 seconds            |
| `pool-size`                    | integer | `30`    | Connection pool size for API service instances |
| `import-threads-course-member` | integer | `10`    | Threads for importing course membership data   |

## Complete Configuration Example

```xml
<configuration>
  <configSections>
    <section name="lithnet-google-ma" 
             type="Lithnet.GoogleApps.MA.MAConfigurationSection, Lithnet.GoogleApps.MA" />
  </configSections>

  <lithnet-google-ma 
      export-threads="30"
      import-threads="50">
    
    <directory-api 
        rate-limit="1500"
        pool-size="30"
        export-threads-group-member="5"
        import-threads-group-member="10"
        batch-size-group-member="100"
        concurrent-operations-group-member="10" />
    
    <groupssettings-api 
        rate-limit="500"
        pool-size="30"
        import-threads-group-settings="30" />
    
    <contacts-api 
        rate-limit="1500"
        pool-size="30"
        page-size="1000" />
    
    <calendar-api 
        rate-limit="1500"
        pool-size="30" />
    
    <gmail-api 
        rate-limit="250" />
    
    <classroom-api 
        rate-limit="50"
        pool-size="30"
        import-threads-course-member="10" />
    
  </lithnet-google-ma>

  <!-- Other configuration sections -->
  
</configuration>
```

## Performance Tuning Guidelines

### Rate Limits

Rate limits are specified as requests per 100 seconds and should align with Google's API quotas for your organization:

* **Higher rate limits** = Faster synchronization but higher risk of hitting quota limits
* **Lower rate limits** = Slower synchronization but more stable operation
* Monitor Google API console for quota usage and adjust accordingly

### Threading

Thread counts control parallel operations:

* **More threads** = Better performance with adequate system resources
* **Fewer threads** = Lower resource usage, better for constrained environments
* Balance against available CPU cores and memory

### Connection Pools

Pool sizes determine how many API service instances are maintained:

* Larger pools support more concurrent operations
* Should generally match or exceed thread counts for optimal performance
* Each pool member consumes memory

## Troubleshooting

### Contact API Timeouts

If experiencing timeouts with contacts, reduce `page-size` from the default 1000 to 100 or lower.

### Rate Limit Errors

If encountering rate limit errors from Google APIs:

1. Reduce the `rate-limit` value for the affected API
2. Decrease thread counts for related operations
3. Check your Google API Console quota settings

## Important Notes

* All configuration settings are **optional** and use sensible defaults if omitted
* Changes require restarting the **MIIServer service** to take effect
* Rate limits are enforced per 100-second rolling window
* Thread and pool settings affect memory consumption and system resources
* Configuration is read from `miiserver.exe.config`

## Related Resources

* [Google API Quotas and Limits](https://developers.google.com/admin-sdk/directory/v1/limits)
* [Google Workspace Admin SDK](https://developers.google.com/admin-sdk)
* [MIM Server Configuration](https://docs.microsoft.com/en-us/microsoft-identity-manager/)
